Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,110 @@
|
||||
"""UTC 毫秒日期时间往返;独立 Chrono/Go time 实现,不共享 Dever runtime。"""
|
||||
|
||||
|
||||
def source(language):
|
||||
if language == 'dever':
|
||||
return '''roundtrip(start: DateTime, offset: Int) (out: Int) {
|
||||
span = dever.time.duration(offset)
|
||||
changed = dever.time.add(start, span)
|
||||
text = dever.time.formatDatetime(changed)
|
||||
restored = dever.time.parseDatetime(text)
|
||||
check dever.time.difference(restored, start) == span else Invalid
|
||||
out = 1
|
||||
}
|
||||
loop count(start: DateTime, n: Int) (out: Int)
|
||||
| each offset in range(0, n) | result |
|
||||
| out | sum roundtrip(start, offset) |
|
||||
measure(n: Int) (out: Int) {
|
||||
out = count(dever.time.parseDatetime("2024-02-29T12:34:56.12+08:00"), n)
|
||||
}
|
||||
cmd bench = app.measure
|
||||
'''
|
||||
if language == 'rust':
|
||||
return '''use chrono::{DateTime, Utc, Datelike, Timelike};
|
||||
use std::fmt::Write;
|
||||
fn calendar(value: i64) -> DateTime<Utc> {
|
||||
DateTime::from_timestamp_millis(value).filter(|date| (0..=9999).contains(&date.year())).expect("calendar range")
|
||||
}
|
||||
fn parse(text: &str) -> i64 {
|
||||
if let Some(fraction) = text.get(19..).and_then(|tail| tail.strip_prefix('.')) {
|
||||
assert!(fraction.bytes().take_while(u8::is_ascii_digit).count() <= 3);
|
||||
}
|
||||
let value = DateTime::parse_from_rfc3339(text).unwrap();
|
||||
assert!(value.nanosecond() < 1_000_000_000 && value.nanosecond() % 1_000_000 == 0);
|
||||
let millis = value.timestamp_millis();
|
||||
calendar(millis);
|
||||
millis
|
||||
}
|
||||
fn format(millis: i64) -> String {
|
||||
let value = calendar(millis);
|
||||
let spec = if value.timestamp_subsec_millis() == 0 { "%Y-%m-%dT%H:%M:%SZ" } else { "%Y-%m-%dT%H:%M:%S%.3fZ" };
|
||||
let mut text = String::with_capacity(24);
|
||||
write!(text, "{}", value.naive_utc().format(spec)).unwrap();
|
||||
text
|
||||
}
|
||||
fn roundtrip(start: i64, offset: i64) -> i64 {
|
||||
let changed = start.checked_add(offset).unwrap();
|
||||
let restored = parse(&format(changed));
|
||||
assert_eq!(restored.checked_sub(start).unwrap(), offset);
|
||||
1
|
||||
}
|
||||
fn main() {
|
||||
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
|
||||
let start = parse("2024-02-29T12:34:56.12+08:00");
|
||||
let mut total = 0i64;
|
||||
for offset in 0..n { total = total.checked_add(roundtrip(start, offset)).unwrap(); }
|
||||
println!("{total}");
|
||||
}
|
||||
'''
|
||||
return '''package main
|
||||
import ("fmt"; "os"; "strconv"; "strings"; "time")
|
||||
func calendar(value int64) time.Time {
|
||||
date := time.UnixMilli(value).UTC()
|
||||
if date.Year() < 0 || date.Year() > 9999 { panic("calendar range") }
|
||||
return date
|
||||
}
|
||||
func parse(text string) int64 {
|
||||
if len(text) > 19 && strings.HasPrefix(text[19:], ".") {
|
||||
count := 0
|
||||
for _, digit := range text[20:] {
|
||||
if digit < '0' || digit > '9' { break }; count++
|
||||
}
|
||||
if count > 3 { panic("millisecond precision") }
|
||||
}
|
||||
value, err := time.Parse(time.RFC3339Nano, text)
|
||||
if err != nil { panic(err) }
|
||||
if value.Nanosecond() % 1000000 != 0 { panic("millisecond precision") }
|
||||
millis := value.UnixMilli()
|
||||
calendar(millis)
|
||||
return millis
|
||||
}
|
||||
func format(millis int64) string {
|
||||
value := calendar(millis)
|
||||
if value.Nanosecond() == 0 { return value.Format("2006-01-02T15:04:05Z") }
|
||||
return value.Format("2006-01-02T15:04:05.000Z")
|
||||
}
|
||||
func add(left, right int64) int64 {
|
||||
sum := left + right
|
||||
if (right > 0 && sum < left) || (right < 0 && sum > left) { panic("overflow") }
|
||||
return sum
|
||||
}
|
||||
func subtract(left, right int64) int64 {
|
||||
difference := left - right
|
||||
if (right > 0 && difference > left) || (right < 0 && difference < left) { panic("overflow") }
|
||||
return difference
|
||||
}
|
||||
func roundtrip(start, offset int64) int64 {
|
||||
changed := add(start, offset)
|
||||
restored := parse(format(changed))
|
||||
if subtract(restored, start) != offset { panic("wrong result") }
|
||||
return 1
|
||||
}
|
||||
func main() {
|
||||
n, err := strconv.ParseInt(os.Args[1], 10, 64)
|
||||
if err != nil { panic(err) }
|
||||
start := parse("2024-02-29T12:34:56.12+08:00")
|
||||
total := int64(0)
|
||||
for offset := int64(0); offset < n; offset++ { total = add(total, roundtrip(start, offset)) }
|
||||
fmt.Println(total)
|
||||
}
|
||||
'''
|
||||
Reference in new issue
Block a user