time: v fmt (#7160)

pull/7163/head
Takahiro Yaota 2020-12-06 23:19:39 +09:00 committed by GitHub
parent 43ff93c25f
commit 0c50f0c9dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 147 additions and 212 deletions

View File

@ -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)))

View File

@ -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,7 +32,6 @@ fn test_now_always_results_in_greater_time() {
}
fn test_time1_should_be_same_as_time2() {
t1 := new_time(Time{
year: 2000
month: 5
@ -42,7 +41,6 @@ fn test_time1_should_be_same_as_time2() {
second: 3
microsecond: 100
})
t2 := new_time(Time{
year: 2000
month: 5
@ -52,12 +50,10 @@ 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{
year: 2000
month: 5
@ -67,7 +63,6 @@ fn test_time1_should_not_be_same_as_time2() {
second: 3
microsecond: 100
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -78,7 +73,6 @@ fn test_time1_should_not_be_same_as_time2() {
second: 3
microsecond: 101
})
t3 := new_time(Time{
year: 2000
month: 5
@ -88,7 +82,6 @@ fn test_time1_should_not_be_same_as_time2() {
second: 3
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -99,13 +92,11 @@ 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{
year: 2000
month: 5
@ -115,7 +106,6 @@ fn test_time1_should_be_greater_than_time2() {
second: 3
microsecond: 102
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -126,7 +116,6 @@ fn test_time1_should_be_greater_than_time2() {
second: 3
microsecond: 101
})
t3 := new_time(Time{
year: 2000
month: 5
@ -136,7 +125,6 @@ fn test_time1_should_be_greater_than_time2() {
second: 5
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -147,14 +135,11 @@ 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{
year: 2000
month: 5
@ -164,7 +149,6 @@ fn test_time2_should_be_less_than_time1() {
second: 3
microsecond: 102
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -175,7 +159,6 @@ fn test_time2_should_be_less_than_time1() {
second: 3
microsecond: 101
})
t3 := new_time(Time{
year: 2000
month: 5
@ -185,7 +168,6 @@ fn test_time2_should_be_less_than_time1() {
second: 3
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -196,13 +178,11 @@ 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{
year: 2000
month: 5
@ -212,7 +192,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
second: 3
microsecond: 102
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -223,7 +202,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
second: 3
microsecond: 101
})
t3 := new_time(Time{
year: 2000
month: 5
@ -233,7 +211,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_gt() {
second: 5
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -244,13 +221,11 @@ 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{
year: 2000
month: 5
@ -260,7 +235,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
second: 3
microsecond: 100
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -271,7 +245,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
second: 3
microsecond: 100
})
t3 := new_time(Time{
year: 2000
month: 5
@ -281,7 +254,6 @@ fn test_time1_should_be_greater_or_equal_to_time2_when_eq() {
second: 3
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -292,13 +264,11 @@ 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{
year: 2000
month: 5
@ -308,7 +278,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
second: 3
microsecond: 100
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -319,7 +288,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
second: 3
microsecond: 101
})
t3 := new_time(Time{
year: 2000
month: 5
@ -329,7 +297,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_lt() {
second: 3
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -340,13 +307,11 @@ 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{
year: 2000
month: 5
@ -356,7 +321,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
second: 3
microsecond: 100
})
// Difference is one microsecond
t2 := new_time(Time{
year: 2000
@ -367,7 +331,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
second: 3
microsecond: 100
})
t3 := new_time(Time{
year: 2000
month: 5
@ -377,7 +340,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
second: 3
microsecond: 0
})
// Difference is one second
t4 := new_time(Time{
year: 2000
@ -388,7 +350,6 @@ fn test_time1_should_be_less_or_equal_to_time2_when_eq() {
second: 3
microsecond: 0
})
assert t1.le(t2)
assert t3.le(t4)
}
@ -404,6 +365,5 @@ fn test_time2_copied_from_time1_should_be_equal() {
microsecond: 100
})
t2 := new_time(t1)
assert t2.eq(t1)
}

View File

@ -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 {

View File

@ -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
}
@ -47,18 +50,19 @@ 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)}
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)}
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
@ -67,10 +71,12 @@ fn test_iso8601_parse_loacl() {
fn test_iso8601_parse_utc_diff() {
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)}
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

View File

@ -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
}

View File

@ -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))
}

View File

@ -1,7 +1,6 @@
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()
@ -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))
}

View File

@ -4,7 +4,6 @@
module time
#include <time.h>
struct C.tm {
tm_sec int
tm_min int
@ -18,6 +17,7 @@ struct C.tm {
}
fn C.timegm(&tm) time_t
fn C.localtime_r(t &C.time_t, tm &C.tm)
fn make_unix_time(t C.tm) int {
@ -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,26 +58,22 @@ 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))
}
@ -88,7 +83,6 @@ 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))
}

View File

@ -5,15 +5,12 @@ 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))
}
@ -23,7 +20,6 @@ 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))
}

View File

@ -5,7 +5,6 @@ module time
#include <time.h>
// #include <sysinfoapi.h>
struct C.tm {
tm_year int
tm_mon int
@ -15,7 +14,8 @@ struct C.tm {
tm_sec int
}
struct C._FILETIME
struct C._FILETIME {
}
struct SystemTime {
year u16
@ -29,8 +29,11 @@ struct SystemTime {
}
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime C._FILETIME)
fn C.FileTimeToSystemTime()
fn C.SystemTimeToTzSpecificLocalTime()
fn C.localtime_s(t &C.time_t, tm &C.tm)
const (
@ -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,15 +104,13 @@ 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
second: st_local.second // These are the same
microsecond: t.microsecond
unix: t.unix
}
@ -123,16 +122,12 @@ 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{
year: st_local.year
month: st_local.month
@ -143,7 +138,6 @@ fn win_now() Time {
microsecond: st_local.millisecond * 1000
unix: u64(st_local.unix_time())
}
return t
}
@ -152,13 +146,10 @@ 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{
year: st_utc.year
month: st_utc.month
@ -169,7 +160,6 @@ fn win_utc() Time {
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

View File

@ -46,7 +46,6 @@ pub fn unix2(abs int, microsecond int) Time {
}
}
[inline]
fn calculate_date_from_offset(day_offset_ int) (int, int, int) {
mut day_offset := day_offset_
@ -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,8 +93,7 @@ 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
}