time: v fmt (#7160)
parent
43ff93c25f
commit
0c50f0c9dc
|
@ -105,7 +105,7 @@ pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
|
|||
.hhmm24 { '${t.hour:02d}:${t.minute:02d}' }
|
||||
.hhmmss12 { '$hour:${t.minute:02d}:${t.second:02d} $tp' }
|
||||
.hhmmss24 { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}' }
|
||||
.hhmmss24_milli { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond/1000):03d}' }
|
||||
.hhmmss24_milli { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond / 1000):03d}' }
|
||||
.hhmmss24_micro { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.microsecond:06d}' }
|
||||
else { 'unknown enumeration $fmt_time' }
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import time
|
|||
const (
|
||||
start_time_unix = time.now().unix
|
||||
)
|
||||
|
||||
// random returns a random time struct in *the past*.
|
||||
pub fn random() time.Time {
|
||||
return time.unix(int(rand.u64n(start_time_unix)))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module time
|
||||
|
||||
fn assert_greater_time(ms int, t1 Time) {
|
||||
time.sleep_ms(ms)
|
||||
sleep_ms(ms)
|
||||
t2 := now()
|
||||
assert t2.gt(t1)
|
||||
}
|
||||
|
@ -32,8 +32,7 @@ fn test_now_always_results_in_greater_time() {
|
|||
}
|
||||
|
||||
fn test_time1_should_be_same_as_time2() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -42,8 +41,7 @@ fn test_time1_should_be_same_as_time2() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -52,13 +50,11 @@ fn test_time1_should_be_same_as_time2() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
assert t1.eq(t2)
|
||||
}
|
||||
|
||||
fn test_time1_should_not_be_same_as_time2() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -67,9 +63,8 @@ fn test_time1_should_not_be_same_as_time2() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -78,8 +73,7 @@ fn test_time1_should_not_be_same_as_time2() {
|
|||
second: 3
|
||||
microsecond: 101
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -88,9 +82,8 @@ fn test_time1_should_not_be_same_as_time2() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -99,14 +92,12 @@ fn test_time1_should_not_be_same_as_time2() {
|
|||
second: 4
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.ne(t2)
|
||||
assert t3.ne(t4)
|
||||
}
|
||||
|
||||
fn test_time1_should_be_greater_than_time2() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -115,9 +106,8 @@ fn test_time1_should_be_greater_than_time2() {
|
|||
second: 3
|
||||
microsecond: 102
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -126,8 +116,7 @@ fn test_time1_should_be_greater_than_time2() {
|
|||
second: 3
|
||||
microsecond: 101
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -136,9 +125,8 @@ fn test_time1_should_be_greater_than_time2() {
|
|||
second: 5
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -147,15 +135,12 @@ fn test_time1_should_be_greater_than_time2() {
|
|||
second: 4
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.gt(t2)
|
||||
assert t3.gt(t4)
|
||||
}
|
||||
|
||||
|
||||
fn test_time2_should_be_less_than_time1() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -164,9 +149,8 @@ fn test_time2_should_be_less_than_time1() {
|
|||
second: 3
|
||||
microsecond: 102
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -175,8 +159,7 @@ fn test_time2_should_be_less_than_time1() {
|
|||
second: 3
|
||||
microsecond: 101
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -185,9 +168,8 @@ fn test_time2_should_be_less_than_time1() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -196,14 +178,12 @@ fn test_time2_should_be_less_than_time1() {
|
|||
second: 2
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t2.lt(t1)
|
||||
assert t4.lt(t3)
|
||||
}
|
||||
|
||||
fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -212,9 +192,8 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
|||
second: 3
|
||||
microsecond: 102
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -223,8 +202,7 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
|||
second: 3
|
||||
microsecond: 101
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -233,9 +211,8 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
|||
second: 5
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -244,14 +221,12 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
|
|||
second: 4
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.ge(t2)
|
||||
assert t3.ge(t4)
|
||||
}
|
||||
|
||||
fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -260,9 +235,8 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -271,8 +245,7 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -281,9 +254,8 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -292,14 +264,12 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.ge(t2)
|
||||
assert t3.ge(t4)
|
||||
}
|
||||
|
||||
fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -308,9 +278,8 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -319,8 +288,7 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
|||
second: 3
|
||||
microsecond: 101
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -329,9 +297,8 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -340,14 +307,12 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
|
|||
second: 4
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.le(t2)
|
||||
assert t3.le(t4)
|
||||
}
|
||||
|
||||
fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
||||
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -356,9 +321,8 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
// Difference is one microsecond
|
||||
t2 := new_time( Time {
|
||||
t2 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -367,8 +331,7 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 100
|
||||
})
|
||||
|
||||
t3 := new_time( Time {
|
||||
t3 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -377,9 +340,8 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
// Difference is one second
|
||||
t4 := new_time( Time {
|
||||
t4 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -388,13 +350,12 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
|
|||
second: 3
|
||||
microsecond: 0
|
||||
})
|
||||
|
||||
assert t1.le(t2)
|
||||
assert t3.le(t4)
|
||||
}
|
||||
|
||||
fn test_time2_copied_from_time1_should_be_equal() {
|
||||
t1 := new_time( Time {
|
||||
t1 := new_time(Time{
|
||||
year: 2000
|
||||
month: 5
|
||||
day: 10
|
||||
|
@ -404,6 +365,5 @@ fn test_time2_copied_from_time1_should_be_equal() {
|
|||
microsecond: 100
|
||||
})
|
||||
t2 := new_time(t1)
|
||||
|
||||
assert t2.eq(t1)
|
||||
}
|
||||
|
|
|
@ -66,8 +66,8 @@ pub fn parse_iso8601(s string) ?Time {
|
|||
offset_hour := 0
|
||||
offset_min := 0
|
||||
count := unsafe {C.sscanf(charptr(s.str), '%4d-%2d-%2d%c%2d:%2d:%2d.%6d%c%2d:%2d',
|
||||
&year, &month, &day, charptr(&time_char), &hour, &minute, &second, &mic_second, charptr(&plus_min_z), &offset_hour,
|
||||
&offset_min)}
|
||||
&year, &month, &day, charptr(&time_char), &hour, &minute, &second, &mic_second, charptr(&plus_min_z),
|
||||
&offset_hour, &offset_min)}
|
||||
is_local_time := plus_min_z == `a` && count == 8
|
||||
is_utc := plus_min_z == `Z` && count == 9
|
||||
if count != 11 && !is_local_time && !is_utc {
|
||||
|
@ -91,7 +91,6 @@ pub fn parse_iso8601(s string) ?Time {
|
|||
if is_local_time {
|
||||
return to_local_time(t)
|
||||
}
|
||||
|
||||
mut unix_time := t.unix
|
||||
mut unix_offset := int(0)
|
||||
if offset_hour > 0 {
|
||||
|
|
|
@ -6,7 +6,8 @@ fn test_parse() {
|
|||
assert false
|
||||
return
|
||||
}
|
||||
assert t.year == 2018 && t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
|
||||
assert t.year == 2018 &&
|
||||
t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
|
||||
assert t.unix == 1517057314
|
||||
}
|
||||
|
||||
|
@ -25,14 +26,16 @@ fn test_parse_rfc2822() {
|
|||
assert false
|
||||
return
|
||||
}
|
||||
assert t1.year == 2019 && t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
|
||||
assert t1.year == 2019 &&
|
||||
t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
|
||||
assert t1.unix == 1576130865
|
||||
s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
|
||||
t2 := time.parse_rfc2822(s2) or {
|
||||
assert false
|
||||
return
|
||||
}
|
||||
assert t2.year == 2019 && t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
|
||||
assert t2.year == 2019 &&
|
||||
t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
|
||||
assert t2.unix == 1576130865
|
||||
}
|
||||
|
||||
|
@ -46,32 +49,35 @@ fn test_parse_rfc2822_invalid() {
|
|||
}
|
||||
|
||||
fn test_iso8601_parse_utc() {
|
||||
format_utc := '2020-06-05T15:38:06.015959Z'
|
||||
t_utc := time.parse_iso8601(format_utc) or {panic(err)}
|
||||
|
||||
assert t_utc.year == 2020
|
||||
format_utc := '2020-06-05T15:38:06.015959Z'
|
||||
t_utc := time.parse_iso8601(format_utc) or {
|
||||
panic(err)
|
||||
}
|
||||
assert t_utc.year == 2020
|
||||
assert t_utc.month == 6
|
||||
assert t_utc.day == 5
|
||||
|
||||
}
|
||||
|
||||
fn test_iso8601_parse_loacl() {
|
||||
format_utc := '2020-06-05T15:38:06.015959'
|
||||
t_utc := time.parse_iso8601(format_utc) or {panic(err)}
|
||||
|
||||
assert t_utc.year == 2020
|
||||
format_utc := '2020-06-05T15:38:06.015959'
|
||||
t_utc := time.parse_iso8601(format_utc) or {
|
||||
panic(err)
|
||||
}
|
||||
assert t_utc.year == 2020
|
||||
assert t_utc.month == 6
|
||||
assert t_utc.day == 5
|
||||
}
|
||||
|
||||
fn test_iso8601_parse_utc_diff() {
|
||||
format_utc := '2020-06-05T15:38:06.015959+00:00'
|
||||
format_utc := '2020-06-05T15:38:06.015959+00:00'
|
||||
format_cest := '2020-06-05T15:38:06.015959+02:00'
|
||||
|
||||
t_utc := time.parse_iso8601(format_utc) or {panic(err)}
|
||||
t_cest := time.parse_iso8601(format_cest) or {panic(err)}
|
||||
|
||||
assert t_utc.year == 2020
|
||||
t_utc := time.parse_iso8601(format_utc) or {
|
||||
panic(err)
|
||||
}
|
||||
t_cest := time.parse_iso8601(format_cest) or {
|
||||
panic(err)
|
||||
}
|
||||
assert t_utc.year == 2020
|
||||
assert t_cest.year == 2020
|
||||
assert t_utc.month == 6
|
||||
assert t_cest.month == 6
|
||||
|
|
|
@ -5,12 +5,9 @@ module time
|
|||
fn test_new_is_same_as_old_for_all_platforms() {
|
||||
t := C.time(0)
|
||||
tm := C.localtime(&t)
|
||||
|
||||
old_time := time.convert_ctime(tm, 0)
|
||||
new_time := time.now()
|
||||
|
||||
old_time := convert_ctime(tm, 0)
|
||||
new_time := now()
|
||||
diff := new_time.unix - old_time.unix
|
||||
|
||||
// could in very rare cases be that the second changed between calls
|
||||
assert (diff >=0 && diff <=1) == true
|
||||
assert (diff >= 0 && diff <= 1) == true
|
||||
}
|
|
@ -17,39 +17,40 @@ pub mut:
|
|||
|
||||
pub fn new_stopwatch(opts StopWatchOptions) StopWatch {
|
||||
mut initial := u64(0)
|
||||
|
||||
if opts.auto_start {
|
||||
initial = time.sys_mono_now()
|
||||
initial = sys_mono_now()
|
||||
}
|
||||
return StopWatch{
|
||||
elapsed: 0
|
||||
start: initial
|
||||
end: 0
|
||||
}
|
||||
|
||||
return StopWatch{elapsed: 0, start: initial, end: 0}
|
||||
}
|
||||
|
||||
// start Starts the timer. If the timer was paused, restarts counting.
|
||||
pub fn (mut t StopWatch) start() {
|
||||
t.start = time.sys_mono_now()
|
||||
t.start = sys_mono_now()
|
||||
t.end = 0
|
||||
}
|
||||
|
||||
pub fn (mut t StopWatch) restart() {
|
||||
t.start = time.sys_mono_now()
|
||||
t.start = sys_mono_now()
|
||||
t.end = 0
|
||||
t.elapsed = 0
|
||||
}
|
||||
|
||||
pub fn (mut t StopWatch) stop() {
|
||||
t.end = time.sys_mono_now()
|
||||
t.end = sys_mono_now()
|
||||
}
|
||||
|
||||
pub fn (mut t StopWatch) pause() {
|
||||
if t.start > 0 {
|
||||
if t.end == 0 {
|
||||
t.elapsed += time.sys_mono_now() - t.start
|
||||
t.elapsed += sys_mono_now() - t.start
|
||||
} else {
|
||||
t.elapsed += t.end - t.start
|
||||
}
|
||||
}
|
||||
|
||||
t.start = 0
|
||||
}
|
||||
|
||||
|
@ -57,11 +58,10 @@ pub fn (mut t StopWatch) pause() {
|
|||
pub fn (t StopWatch) elapsed() Duration {
|
||||
if t.start > 0 {
|
||||
if t.end == 0 {
|
||||
return Duration(i64(time.sys_mono_now() - t.start + t.elapsed))
|
||||
return Duration(i64(sys_mono_now() - t.start + t.elapsed))
|
||||
} else {
|
||||
return Duration(i64(t.end - t.start + t.elapsed))
|
||||
}
|
||||
}
|
||||
|
||||
return Duration(i64(t.elapsed))
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
module time
|
||||
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
const (
|
||||
// start_time is needed on Darwin and Windows because of potential overflows
|
||||
start_time = C.mach_absolute_time()
|
||||
time_base = init_time_base()
|
||||
time_base = init_time_base()
|
||||
)
|
||||
|
||||
[typedef]
|
||||
|
@ -15,7 +14,9 @@ struct C.mach_timebase_info_data_t {
|
|||
}
|
||||
|
||||
fn C.mach_absolute_time() u64
|
||||
|
||||
fn C.mach_timebase_info(&C.mach_timebase_info_data_t)
|
||||
|
||||
fn C.clock_gettime_nsec_np(int) u64
|
||||
|
||||
struct InternalTimeBase {
|
||||
|
@ -31,7 +32,10 @@ pub struct C.timeval {
|
|||
fn init_time_base() C.mach_timebase_info_data_t {
|
||||
tb := C.mach_timebase_info_data_t{}
|
||||
C.mach_timebase_info(&tb)
|
||||
return C.mach_timebase_info_data_t{numer:tb.numer, denom:tb.denom}
|
||||
return C.mach_timebase_info_data_t{
|
||||
numer: tb.numer
|
||||
denom: tb.denom
|
||||
}
|
||||
}
|
||||
|
||||
fn sys_mono_now_darwin() u64 {
|
||||
|
@ -59,14 +63,11 @@ fn vpc_now_darwin() u64 {
|
|||
// the microseconds seconds part and converts to local time
|
||||
[inline]
|
||||
fn darwin_now() Time {
|
||||
|
||||
// get the high precision time as UTC clock
|
||||
tv := C.timeval{}
|
||||
C.gettimeofday(&tv, 0)
|
||||
|
||||
loc_tm := C.tm{}
|
||||
C.localtime_r(&tv.tv_sec, &loc_tm)
|
||||
|
||||
return convert_ctime(loc_tm, int(tv.tv_usec))
|
||||
}
|
||||
|
||||
|
@ -76,10 +77,8 @@ fn darwin_now() Time {
|
|||
// the microseconds seconds part and normal local time to get correct local time
|
||||
[inline]
|
||||
fn darwin_utc() Time {
|
||||
|
||||
// get the high precision time as UTC clock
|
||||
tv := C.timeval{}
|
||||
C.gettimeofday(&tv, 0)
|
||||
|
||||
return unix2(int(tv.tv_sec), int(tv.tv_usec))
|
||||
}
|
||||
|
|
|
@ -4,21 +4,21 @@
|
|||
module time
|
||||
|
||||
#include <time.h>
|
||||
|
||||
struct C.tm {
|
||||
tm_sec int
|
||||
tm_min int
|
||||
tm_hour int
|
||||
tm_mday int
|
||||
tm_mon int
|
||||
tm_year int
|
||||
tm_wday int
|
||||
tm_yday int
|
||||
tm_sec int
|
||||
tm_min int
|
||||
tm_hour int
|
||||
tm_mday int
|
||||
tm_mon int
|
||||
tm_year int
|
||||
tm_wday int
|
||||
tm_yday int
|
||||
tm_isdst int
|
||||
}
|
||||
|
||||
fn C.timegm(&tm) time_t
|
||||
fn C.localtime_r(t &C.time_t, tm &C.tm )
|
||||
|
||||
fn C.localtime_r(t &C.time_t, tm &C.tm)
|
||||
|
||||
fn make_unix_time(t C.tm) int {
|
||||
return int(C.timegm(&t))
|
||||
|
@ -27,7 +27,6 @@ fn make_unix_time(t C.tm) int {
|
|||
fn to_local_time(t Time) Time {
|
||||
loc_tm := C.tm{}
|
||||
C.localtime_r(time_t(&t.unix), &loc_tm)
|
||||
|
||||
return convert_ctime(loc_tm, t.microsecond)
|
||||
}
|
||||
|
||||
|
@ -49,7 +48,7 @@ pub fn sys_mono_now() u64 {
|
|||
} $else {
|
||||
ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_MONOTONIC, &ts)
|
||||
return u64(ts.tv_sec) * 1_000_000_000 + u64(ts.tv_nsec)
|
||||
return u64(ts.tv_sec) * 1000000000 + u64(ts.tv_nsec)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,27 +58,23 @@ pub fn sys_mono_now() u64 {
|
|||
fn vpc_now() u64 {
|
||||
ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_MONOTONIC, &ts)
|
||||
return u64(ts.tv_sec) * 1_000_000_000 + u64(ts.tv_nsec)
|
||||
return u64(ts.tv_sec) * 1000000000 + u64(ts.tv_nsec)
|
||||
}
|
||||
|
||||
// The linux_* functions are placed here, since they're used on Android as well
|
||||
// TODO: should `$if linux {}` be parsed on Android as well? (Android runs under the Linux kernel)
|
||||
|
||||
// linux_now returns the local time with high precision for most os:es
|
||||
// this should be implemented properly with support for leap seconds.
|
||||
// It uses the realtime clock to get and converts it to local time
|
||||
[inline]
|
||||
fn linux_now() Time {
|
||||
|
||||
// get the high precision time as UTC realtime clock
|
||||
// and use the nanoseconds part
|
||||
mut ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_REALTIME, &ts)
|
||||
|
||||
loc_tm := C.tm{}
|
||||
C.localtime_r(&ts.tv_sec, &loc_tm)
|
||||
|
||||
return convert_ctime(loc_tm, int(ts.tv_nsec/1000))
|
||||
return convert_ctime(loc_tm, int(ts.tv_nsec / 1000))
|
||||
}
|
||||
|
||||
[inline]
|
||||
|
@ -88,8 +83,7 @@ fn linux_utc() Time {
|
|||
// and use the nanoseconds part
|
||||
mut ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_REALTIME, &ts)
|
||||
|
||||
return unix2(int(ts.tv_sec), int(ts.tv_nsec/1000))
|
||||
return unix2(int(ts.tv_sec), int(ts.tv_nsec / 1000))
|
||||
}
|
||||
|
||||
// dummy to compile with all compilers
|
||||
|
@ -126,7 +120,7 @@ pub fn (d Duration) timespec() C.timespec {
|
|||
// return timespec of 1970/1/1
|
||||
pub fn zero_timespec() C.timespec {
|
||||
ts := C.timespec{
|
||||
tv_sec: 0
|
||||
tv_sec: 0
|
||||
tv_nsec: 0
|
||||
}
|
||||
return ts
|
||||
|
|
|
@ -5,16 +5,13 @@ module time
|
|||
// It uses the realtime clock to get and converts it to local time
|
||||
[inline]
|
||||
fn solaris_now() Time {
|
||||
|
||||
// get the high precision time as UTC realtime clock
|
||||
// and use the nanoseconds part
|
||||
mut ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_REALTIME, &ts)
|
||||
|
||||
loc_tm := C.tm{}
|
||||
C.localtime_r(&ts.tv_sec, &loc_tm)
|
||||
|
||||
return convert_ctime(loc_tm, int(ts.tv_nsec/1000))
|
||||
return convert_ctime(loc_tm, int(ts.tv_nsec / 1000))
|
||||
}
|
||||
|
||||
[inline]
|
||||
|
@ -23,8 +20,7 @@ fn solaris_utc() Time {
|
|||
// and use the nanoseconds part
|
||||
mut ts := C.timespec{}
|
||||
C.clock_gettime(C.CLOCK_REALTIME, &ts)
|
||||
|
||||
return unix2(int(ts.tv_sec), int(ts.tv_nsec/1000))
|
||||
return unix2(int(ts.tv_sec), int(ts.tv_nsec / 1000))
|
||||
}
|
||||
|
||||
// dummy to compile with all compilers
|
||||
|
|
|
@ -5,7 +5,6 @@ module time
|
|||
|
||||
#include <time.h>
|
||||
// #include <sysinfoapi.h>
|
||||
|
||||
struct C.tm {
|
||||
tm_year int
|
||||
tm_mon int
|
||||
|
@ -15,29 +14,33 @@ struct C.tm {
|
|||
tm_sec int
|
||||
}
|
||||
|
||||
struct C._FILETIME
|
||||
struct C._FILETIME {
|
||||
}
|
||||
|
||||
struct SystemTime {
|
||||
year u16
|
||||
month u16
|
||||
day_of_week u16
|
||||
day u16
|
||||
hour u16
|
||||
minute u16
|
||||
second u16
|
||||
millisecond u16
|
||||
year u16
|
||||
month u16
|
||||
day_of_week u16
|
||||
day u16
|
||||
hour u16
|
||||
minute u16
|
||||
second u16
|
||||
millisecond u16
|
||||
}
|
||||
|
||||
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime C._FILETIME)
|
||||
|
||||
fn C.FileTimeToSystemTime()
|
||||
|
||||
fn C.SystemTimeToTzSpecificLocalTime()
|
||||
fn C.localtime_s(t &C.time_t, tm &C.tm )
|
||||
|
||||
fn C.localtime_s(t &C.time_t, tm &C.tm)
|
||||
|
||||
const (
|
||||
// start_time is needed on Darwin and Windows because of potential overflows
|
||||
start_time = init_win_time_start()
|
||||
freq_time = init_win_time_freq()
|
||||
start_local_time = local_as_unix_time()
|
||||
start_time = init_win_time_start()
|
||||
freq_time = init_win_time_freq()
|
||||
start_local_time = local_as_unix_time()
|
||||
)
|
||||
|
||||
// in most systems, these are __quad_t, which is an i64
|
||||
|
@ -46,7 +49,6 @@ struct C.timespec {
|
|||
tv_nsec i64
|
||||
}
|
||||
|
||||
|
||||
fn C._mkgmtime(&C.tm) time_t
|
||||
|
||||
fn C.QueryPerformanceCounter(&u64) C.BOOL
|
||||
|
@ -72,7 +74,7 @@ fn init_win_time_start() u64 {
|
|||
pub fn sys_mono_now() u64 {
|
||||
tm := u64(0)
|
||||
C.QueryPerformanceCounter(&tm) // XP or later never fail
|
||||
return (tm - start_time) * 1_000_000_000 / freq_time
|
||||
return (tm - start_time) * 1000000000 / freq_time
|
||||
}
|
||||
|
||||
// NB: vpc_now is used by `v -profile` .
|
||||
|
@ -88,7 +90,6 @@ fn vpc_now() u64 {
|
|||
fn local_as_unix_time() int {
|
||||
t := C.time(0)
|
||||
tm := C.localtime(&t)
|
||||
|
||||
return make_unix_time(tm)
|
||||
}
|
||||
|
||||
|
@ -103,17 +104,15 @@ fn to_local_time(t Time) Time {
|
|||
}
|
||||
st_local := SystemTime{}
|
||||
C.SystemTimeToTzSpecificLocalTime(voidptr(0), &st_utc, &st_local)
|
||||
|
||||
t_local := Time {
|
||||
year: st_local.year
|
||||
month: st_local.month
|
||||
day: st_local.day
|
||||
hour: st_local.hour
|
||||
minute: st_local.minute
|
||||
second: st_local.second
|
||||
// These are the same
|
||||
microsecond: t.microsecond
|
||||
unix: t.unix
|
||||
t_local := Time{
|
||||
year: st_local.year
|
||||
month: st_local.month
|
||||
day: st_local.day
|
||||
hour: st_local.hour
|
||||
minute: st_local.minute
|
||||
second: st_local.second // These are the same
|
||||
microsecond: t.microsecond
|
||||
unix: t.unix
|
||||
}
|
||||
return t_local
|
||||
}
|
||||
|
@ -123,27 +122,22 @@ fn to_local_time(t Time) Time {
|
|||
// down to millisecond. Other more precice methods can be implemented in the future
|
||||
[inline]
|
||||
fn win_now() Time {
|
||||
|
||||
ft_utc := C._FILETIME{}
|
||||
C.GetSystemTimeAsFileTime(&ft_utc)
|
||||
|
||||
st_utc := SystemTime{}
|
||||
C.FileTimeToSystemTime(&ft_utc, &st_utc)
|
||||
|
||||
st_local := SystemTime{}
|
||||
C.SystemTimeToTzSpecificLocalTime(voidptr(0), &st_utc, &st_local)
|
||||
|
||||
t := Time {
|
||||
t := Time{
|
||||
year: st_local.year
|
||||
month: st_local.month
|
||||
day: st_local.day
|
||||
hour: st_local.hour
|
||||
minute: st_local.minute
|
||||
second: st_local.second
|
||||
microsecond: st_local.millisecond*1000
|
||||
microsecond: st_local.millisecond * 1000
|
||||
unix: u64(st_local.unix_time())
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
|
@ -152,24 +146,20 @@ fn win_now() Time {
|
|||
// other more precice methods can be implemented in the future
|
||||
[inline]
|
||||
fn win_utc() Time {
|
||||
|
||||
ft_utc := C._FILETIME{}
|
||||
C.GetSystemTimeAsFileTime(&ft_utc)
|
||||
|
||||
st_utc := SystemTime{}
|
||||
C.FileTimeToSystemTime(&ft_utc, &st_utc)
|
||||
|
||||
t := Time {
|
||||
t := Time{
|
||||
year: st_utc.year
|
||||
month: st_utc.month
|
||||
day: st_utc.day
|
||||
hour: st_utc.hour
|
||||
minute: st_utc.minute
|
||||
second: st_utc.second
|
||||
microsecond: st_utc.millisecond*1000
|
||||
microsecond: st_utc.millisecond * 1000
|
||||
unix: u64(st_utc.unix_time())
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
|
@ -215,7 +205,6 @@ pub fn solaris_utc() Time {
|
|||
return Time{}
|
||||
}
|
||||
|
||||
|
||||
// dummy to compile with all compilers
|
||||
pub struct C.timeval {
|
||||
tv_sec u64
|
||||
|
|
|
@ -11,8 +11,8 @@ pub fn unix(abs int) Time {
|
|||
// Compensate for round towards zero on integers as we want floored instead
|
||||
day_offset--
|
||||
}
|
||||
year,month,day := calculate_date_from_offset(day_offset)
|
||||
hr,min,sec := calculate_time_from_offset(abs % seconds_per_day)
|
||||
year, month, day := calculate_date_from_offset(day_offset)
|
||||
hr, min, sec := calculate_time_from_offset(abs % seconds_per_day)
|
||||
return Time{
|
||||
year: year
|
||||
month: month
|
||||
|
@ -32,8 +32,8 @@ pub fn unix2(abs int, microsecond int) Time {
|
|||
// Compensate for round towards zero on integers as we want floored instead
|
||||
day_offset--
|
||||
}
|
||||
year,month,day := calculate_date_from_offset(day_offset)
|
||||
hr,min,sec := calculate_time_from_offset(abs % seconds_per_day)
|
||||
year, month, day := calculate_date_from_offset(day_offset)
|
||||
hr, min, sec := calculate_time_from_offset(abs % seconds_per_day)
|
||||
return Time{
|
||||
year: year
|
||||
month: month
|
||||
|
@ -46,9 +46,8 @@ pub fn unix2(abs int, microsecond int) Time {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
[inline]
|
||||
fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
||||
fn calculate_date_from_offset(day_offset_ int) (int, int, int) {
|
||||
mut day_offset := day_offset_
|
||||
// Move offset to year 2001 as it's the start of a new 400-year cycle
|
||||
// Code below this rely on the fact that the day_offset is lined up with the 400-year cycle
|
||||
|
@ -62,8 +61,7 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
if day_offset == days_per_100_years * 4 {
|
||||
year += 300
|
||||
day_offset -= days_per_100_years * 3
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
year += (day_offset / days_per_100_years) * 100
|
||||
day_offset %= days_per_100_years
|
||||
}
|
||||
|
@ -71,8 +69,7 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
if day_offset == days_per_4_years * 25 {
|
||||
year += 96
|
||||
day_offset -= days_per_4_years * 24
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
year += (day_offset / days_per_4_years) * 4
|
||||
day_offset %= days_per_4_years
|
||||
}
|
||||
|
@ -80,8 +77,7 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
if day_offset == 365 * 4 {
|
||||
year += 3
|
||||
day_offset -= 365 * 3
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
year += (day_offset / 365)
|
||||
day_offset %= 365
|
||||
}
|
||||
|
@ -89,8 +85,7 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
year--
|
||||
if is_leap_year(year) {
|
||||
day_offset += 366
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
day_offset += 365
|
||||
}
|
||||
}
|
||||
|
@ -98,10 +93,9 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
if day_offset > 31 + 29 - 1 {
|
||||
// After leap day; pretend it wasn't there.
|
||||
day_offset--
|
||||
}
|
||||
else if day_offset == 31 + 29 - 1 {
|
||||
} else if day_offset == 31 + 29 - 1 {
|
||||
// Leap day.
|
||||
return year,2,29
|
||||
return year, 2, 29
|
||||
}
|
||||
}
|
||||
mut estimated_month := day_offset / 31
|
||||
|
@ -115,11 +109,11 @@ fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
|||
estimated_month--
|
||||
}
|
||||
day_offset -= days_before[estimated_month]
|
||||
return year,estimated_month + 1,day_offset + 1
|
||||
return year, estimated_month + 1, day_offset + 1
|
||||
}
|
||||
|
||||
[inline]
|
||||
fn calculate_time_from_offset(second_offset_ int) (int,int,int) {
|
||||
fn calculate_time_from_offset(second_offset_ int) (int, int, int) {
|
||||
mut second_offset := second_offset_
|
||||
if second_offset < 0 {
|
||||
second_offset += seconds_per_day
|
||||
|
@ -128,5 +122,5 @@ fn calculate_time_from_offset(second_offset_ int) (int,int,int) {
|
|||
second_offset %= seconds_per_hour
|
||||
min := second_offset / seconds_per_minute
|
||||
second_offset %= seconds_per_minute
|
||||
return hour,min,second_offset
|
||||
return hour, min, second_offset
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue