mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
make time.unix/0 always return a utc timestamp
This commit is contained in:
parent
f6b60e4d9f
commit
d71e469dca
6 changed files with 30 additions and 41 deletions
|
@ -3,13 +3,15 @@ module time
|
||||||
// operator `==` returns true if provided time is equal to time
|
// operator `==` returns true if provided time is equal to time
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn (t1 Time) == (t2 Time) bool {
|
pub fn (t1 Time) == (t2 Time) bool {
|
||||||
return t1.unix() == t2.unix() && t1.nanosecond == t2.nanosecond
|
return t1.is_local == t2.is_local && t1.local_unix() == t2.local_unix() && t1.nanosecond == t2.nanosecond
|
||||||
}
|
}
|
||||||
|
|
||||||
// operator `<` returns true if provided time is less than time
|
// operator `<` returns true if provided time is less than time
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn (t1 Time) < (t2 Time) bool {
|
pub fn (t1 Time) < (t2 Time) bool {
|
||||||
return t1.unix() < t2.unix() || (t1.unix() == t2.unix() && t1.nanosecond < t2.nanosecond)
|
t1u := t1.unix()
|
||||||
|
t2u := t2.unix()
|
||||||
|
return t1u < t2u || (t1u == t2u && t1.nanosecond < t2.nanosecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time subtract using operator overloading.
|
// Time subtract using operator overloading.
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
// tests that use and test private functions
|
|
||||||
module time
|
|
||||||
|
|
||||||
// test the old behavior is same as new, the unix time should always be local time
|
|
||||||
fn test_new_is_same_as_old_for_all_platforms() {
|
|
||||||
t := C.time(0)
|
|
||||||
tm := C.localtime(&t)
|
|
||||||
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
|
|
||||||
dump(diff)
|
|
||||||
assert (diff >= -2 && diff <= 2) == true
|
|
||||||
}
|
|
|
@ -106,6 +106,12 @@ pub fn (t Time) smonth() string {
|
||||||
// unix returns the UNIX time with second resolution.
|
// unix returns the UNIX time with second resolution.
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn (t Time) unix() i64 {
|
pub fn (t Time) unix() i64 {
|
||||||
|
return time_with_unix(t.local_to_utc()).unix
|
||||||
|
}
|
||||||
|
|
||||||
|
// unix returns the UNIX local time with second resolution.
|
||||||
|
@[inline]
|
||||||
|
pub fn (t Time) local_unix() i64 {
|
||||||
return time_with_unix(t).unix
|
return time_with_unix(t).unix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,14 +141,26 @@ pub fn (t Time) add(duration_in_nanosecond Duration) Time {
|
||||||
// ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯
|
// ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯
|
||||||
mut increased_time_nanosecond := i64(t.nanosecond) + duration_in_nanosecond.nanoseconds()
|
mut increased_time_nanosecond := i64(t.nanosecond) + duration_in_nanosecond.nanoseconds()
|
||||||
// increased_time_second
|
// increased_time_second
|
||||||
mut increased_time_second := t.unix() + (increased_time_nanosecond / second)
|
mut increased_time_second := t.local_unix() + (increased_time_nanosecond / second)
|
||||||
increased_time_nanosecond = increased_time_nanosecond % second
|
increased_time_nanosecond = increased_time_nanosecond % second
|
||||||
if increased_time_nanosecond < 0 {
|
if increased_time_nanosecond < 0 {
|
||||||
increased_time_second--
|
increased_time_second--
|
||||||
increased_time_nanosecond += second
|
increased_time_nanosecond += second
|
||||||
}
|
}
|
||||||
res := unix_nanosecond(increased_time_second, int(increased_time_nanosecond))
|
res := unix_nanosecond(increased_time_second, int(increased_time_nanosecond))
|
||||||
return if t.is_local { res.as_local() } else { res }
|
|
||||||
|
if t.is_local {
|
||||||
|
// we need to reset unix to 0, because we don't know the offset
|
||||||
|
// and we can't calculate it without it without causing infinite recursion
|
||||||
|
// so unfortunately we need to recalculate unix next time it is needed
|
||||||
|
return Time{
|
||||||
|
...res
|
||||||
|
is_local: true
|
||||||
|
unix: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// add_seconds returns a new time struct with an added number of seconds.
|
// add_seconds returns a new time struct with an added number of seconds.
|
||||||
|
@ -177,7 +195,7 @@ pub fn since(t Time) Duration {
|
||||||
// ```
|
// ```
|
||||||
pub fn (t Time) relative() string {
|
pub fn (t Time) relative() string {
|
||||||
znow := now()
|
znow := now()
|
||||||
mut secs := znow.unix - t.unix()
|
mut secs := znow.unix() - t.unix()
|
||||||
mut prefix := ''
|
mut prefix := ''
|
||||||
mut suffix := ''
|
mut suffix := ''
|
||||||
if secs < 0 {
|
if secs < 0 {
|
||||||
|
@ -239,7 +257,7 @@ pub fn (t Time) relative() string {
|
||||||
// ```
|
// ```
|
||||||
pub fn (t Time) relative_short() string {
|
pub fn (t Time) relative_short() string {
|
||||||
znow := now()
|
znow := now()
|
||||||
mut secs := znow.unix - t.unix()
|
mut secs := znow.unix() - t.unix()
|
||||||
mut prefix := ''
|
mut prefix := ''
|
||||||
mut suffix := ''
|
mut suffix := ''
|
||||||
if secs < 0 {
|
if secs < 0 {
|
||||||
|
@ -364,9 +382,9 @@ pub fn days_in_month(month int, year int) !int {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug returns detailed breakdown of time (`Time{ year: YYYY month: MM day: dd hour: HH: minute: mm second: ss nanosecond: nanos unix: unix }`).
|
// debug returns detailed breakdown of time (`Time{ year: YYYY month: MM day: dd hour: HH: minute: mm second: ss nanosecond: nanos unix: unix is_local: false }`).
|
||||||
pub fn (t Time) debug() string {
|
pub fn (t Time) debug() string {
|
||||||
return 'Time{ year: ${t.year:04} month: ${t.month:02} day: ${t.day:02} hour: ${t.hour:02} minute: ${t.minute:02} second: ${t.second:02} nanosecond: ${t.nanosecond:09} unix: ${t.unix:07} }'
|
return 'Time{ year: ${t.year:04} month: ${t.month:02} day: ${t.day:02} hour: ${t.hour:02} minute: ${t.minute:02} second: ${t.second:02} nanosecond: ${t.nanosecond:09} unix: ${t.unix:07} is_local: ${t.is_local} }'
|
||||||
}
|
}
|
||||||
|
|
||||||
// offset returns time zone UTC offset in seconds.
|
// offset returns time zone UTC offset in seconds.
|
||||||
|
|
|
@ -12,7 +12,7 @@ const time_to_test = time.Time{
|
||||||
fn test_now_format() {
|
fn test_now_format() {
|
||||||
t := time.now()
|
t := time.now()
|
||||||
u := t.unix()
|
u := t.unix()
|
||||||
assert t.format() == time.unix(int(u)).format()
|
assert t.format() == time.unix(int(u)).utc_to_local().format()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_format() {
|
fn test_format() {
|
||||||
|
|
|
@ -15,7 +15,7 @@ fn test_tm_gmtoff() {
|
||||||
dump(t2)
|
dump(t2)
|
||||||
dump(t1.nanosecond)
|
dump(t1.nanosecond)
|
||||||
dump(t2.nanosecond)
|
dump(t2.nanosecond)
|
||||||
diff := int(t1.unix() - t2.unix())
|
diff := int(t1.local_unix() - t2.unix())
|
||||||
dump(diff)
|
dump(diff)
|
||||||
dump(info.tm_gmtoff)
|
dump(info.tm_gmtoff)
|
||||||
assert diff in [info.tm_gmtoff - 1, info.tm_gmtoff, info.tm_gmtoff + 1]
|
assert diff in [info.tm_gmtoff - 1, info.tm_gmtoff, info.tm_gmtoff + 1]
|
||||||
|
|
|
@ -90,17 +90,10 @@ fn test_unix() {
|
||||||
assert t6.hour == 6
|
assert t6.hour == 6
|
||||||
assert t6.minute == 9
|
assert t6.minute == 9
|
||||||
assert t6.second == 29
|
assert t6.second == 29
|
||||||
assert local_time_to_test.unix() == 332198622
|
|
||||||
assert utc_time_to_test.unix() == 332198622
|
assert utc_time_to_test.unix() == 332198622
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_format_rfc3339() {
|
fn test_format_rfc3339() {
|
||||||
// assert '1980-07-11T19:23:42.123Z'
|
|
||||||
res := local_time_to_test.format_rfc3339()
|
|
||||||
assert res.ends_with('23:42.123Z')
|
|
||||||
assert res.starts_with('1980-07-1')
|
|
||||||
assert res.contains('T')
|
|
||||||
|
|
||||||
// assert '1980-07-11T19:23:42.123Z'
|
// assert '1980-07-11T19:23:42.123Z'
|
||||||
utc_res := utc_time_to_test.format_rfc3339()
|
utc_res := utc_time_to_test.format_rfc3339()
|
||||||
assert utc_res.ends_with('23:42.123Z')
|
assert utc_res.ends_with('23:42.123Z')
|
||||||
|
@ -109,11 +102,6 @@ fn test_format_rfc3339() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_format_rfc3339_micro() {
|
fn test_format_rfc3339_micro() {
|
||||||
res := local_time_to_test.format_rfc3339_micro()
|
|
||||||
assert res.ends_with('23:42.123456Z')
|
|
||||||
assert res.starts_with('1980-07-1')
|
|
||||||
assert res.contains('T')
|
|
||||||
|
|
||||||
utc_res := utc_time_to_test.format_rfc3339_micro()
|
utc_res := utc_time_to_test.format_rfc3339_micro()
|
||||||
assert utc_res.ends_with('23:42.123456Z')
|
assert utc_res.ends_with('23:42.123456Z')
|
||||||
assert utc_res.starts_with('1980-07-1')
|
assert utc_res.starts_with('1980-07-1')
|
||||||
|
@ -121,11 +109,6 @@ fn test_format_rfc3339_micro() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_format_rfc3339_nano() {
|
fn test_format_rfc3339_nano() {
|
||||||
res := local_time_to_test.format_rfc3339_nano()
|
|
||||||
assert res.ends_with('23:42.123456789Z')
|
|
||||||
assert res.starts_with('1980-07-1')
|
|
||||||
assert res.contains('T')
|
|
||||||
|
|
||||||
utc_res := utc_time_to_test.format_rfc3339_nano()
|
utc_res := utc_time_to_test.format_rfc3339_nano()
|
||||||
assert utc_res.ends_with('23:42.123456789Z')
|
assert utc_res.ends_with('23:42.123456789Z')
|
||||||
assert utc_res.starts_with('1980-07-1')
|
assert utc_res.starts_with('1980-07-1')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue