| 1 | import time |
| 2 | |
| 3 | fn test_as_utc() { |
| 4 | t := time.now() |
| 5 | u := t.as_utc() |
| 6 | dump(u) |
| 7 | assert u.is_local == false |
| 8 | } |
| 9 | |
| 10 | fn test_as_local() { |
| 11 | t := time.now() |
| 12 | l := t.as_local() |
| 13 | dump(l) |
| 14 | assert l.is_local == true |
| 15 | } |
| 16 | |
| 17 | fn test_local_to_utc() { |
| 18 | n := time.now() |
| 19 | u := n.local_to_utc() |
| 20 | dump(u) |
| 21 | o := time.offset() |
| 22 | dump(o) |
| 23 | if o != 0 { |
| 24 | assert n != u |
| 25 | back := u.utc_to_local() // convert it back to local time |
| 26 | assert n == back, 'the converted original local->utc->local time, should be the same as the original local time' |
| 27 | |
| 28 | double_u := u.local_to_utc() |
| 29 | assert u == double_u, 'calling t.local_to_utc().local_to_utc() several times, should not change the time' |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn test_utc_to_local() { |
| 34 | z := time.Time{} |
| 35 | assert z.is_local == false, 'simply constructing a time instance, should construct an UTC time' |
| 36 | l := z.utc_to_local() |
| 37 | dump(l) |
| 38 | o := time.offset() |
| 39 | dump(o) |
| 40 | if o != 0 { |
| 41 | assert z != l, 'when there is a time offset, the local time and the utc time should be different' |
| 42 | assert l == l.utc_to_local(), 'converting a local to local time should not change the time' |
| 43 | assert l == l.utc_to_local().utc_to_local(), 'double converting a local to local time to local time, should not change the time' |
| 44 | } |
| 45 | sz := z.format_rfc3339() |
| 46 | dump(sz) |
| 47 | assert sz == '0000-01-01T00:00:00.000Z' |
| 48 | } |
| 49 | |