@@ -106,6 +106,12 @@ pub fn (t Time) smonth() string {
106106// unix returns the UNIX time with second resolution.
107107@[inline]
108108pub fn (t Time) unix () i64 {
109+ return time_with_unix (t.local_to_utc ()).unix
110+ }
111+
112+ // local_unix returns the UNIX local time with second resolution.
113+ @[inline]
114+ pub fn (t Time) local_unix () i64 {
109115 return time_with_unix (t).unix
110116}
111117
@@ -135,14 +141,26 @@ pub fn (t Time) add(duration_in_nanosecond Duration) Time {
135141 // ... so instead, handle the addition manually in parts ¯\_(ツ)_/¯
136142 mut increased_time_nanosecond := i64 (t.nanosecond) + duration_in_nanosecond.nanoseconds ()
137143 // increased_time_second
138- mut increased_time_second := t.unix () + (increased_time_nanosecond / second)
144+ mut increased_time_second := t.local_unix () + (increased_time_nanosecond / second)
139145 increased_time_nanosecond = increased_time_nanosecond % second
140146 if increased_time_nanosecond < 0 {
141147 increased_time_second--
142148 increased_time_nanosecond + = second
143149 }
144150 res := unix_nanosecond (increased_time_second, int (increased_time_nanosecond))
145- return if t.is_local { res.as_local () } else { res }
151+
152+ if t.is_local {
153+ // we need to reset unix to 0, because we don't know the offset
154+ // and we can't calculate it without it without causing infinite recursion
155+ // so unfortunately we need to recalculate unix next time it is needed
156+ return Time{
157+ ...res
158+ is_local: true
159+ unix: 0
160+ }
161+ }
162+
163+ return res
146164}
147165
148166// add_seconds returns a new time struct with an added number of seconds.
@@ -177,7 +195,7 @@ pub fn since(t Time) Duration {
177195// ```
178196pub fn (t Time) relative () string {
179197 znow := now ()
180- mut secs := znow.unix - t.unix ()
198+ mut secs := znow.unix () - t.unix ()
181199 mut prefix := ''
182200 mut suffix := ''
183201 if secs < 0 {
@@ -239,7 +257,7 @@ pub fn (t Time) relative() string {
239257// ```
240258pub fn (t Time) relative_short () string {
241259 znow := now ()
242- mut secs := znow.unix - t.unix ()
260+ mut secs := znow.unix () - t.unix ()
243261 mut prefix := ''
244262 mut suffix := ''
245263 if secs < 0 {
@@ -364,9 +382,9 @@ pub fn days_in_month(month int, year int) !int {
364382 return res
365383}
366384
367- // debug returns detailed breakdown of time (`Time{ year: YYYY month: MM day: dd hour: HH: minute: mm second: ss nanosecond: nanos unix: unix }`).
385+ // 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 }`).
368386pub fn (t Time) debug () string {
369- 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} }'
387+ 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} }'
370388}
371389
372390// offset returns time zone UTC offset in seconds.
0 commit comments