What is the maximum time. Go time?

I am looking for documentation on maximum time. Travel time.

Other languages ​​make this explicit, for example, in C #: http://msdn.microsoft.com/en-us/library/system.datetime.maxvalue(v=vs.110).aspx

public static readonly DateTime MaxValue 

The value of this constant is 23: 59: 59.9999999, December 31, 9999, exactly one 100-nanosecond tick until 00:00:00, January 1, 10000.

What is the maximum time. Go time? Is it documented somewhere?

+6
source share
2 answers

Travel time is stored as int64 plus a 32-bit Nanosec value (currently uintptr for technical reasons), so there is no real concern about ending up.

 t := time.Unix(1<<63-1, 0) fmt.Println(t.UTC()) 

prints 219250468-12-04 15:30:07 +0000 UTC

If for any reason you want to use a useful maximum time (see @cce answer ), you can use:

 maxTime := time.Unix(1<<63-62135596801, 999999999) 
+12
source

time.Time in go is stored as int64 plus a 32-bit nanosecond value, but if you use @JimB's answer, you will cause an integer overflow in the sec component, and comparisons such as time.Before() will not work.

This is because time.Unix(sec, nsec) adds an offset from 62135596800 seconds to sec , which represents the number of seconds between year 1 (zero time in Go) and 1970 (zero time in Unix).

@twotwotwo playground makes this clear at http://play.golang.org/p/i6S_T4-X3v , but here is the distilled version.

 // number of seconds between Year 1 and 1970 (62135596800 seconds) unixToInternal := int64((1969*365 + 1969/4 - 1969/100 + 1969/400) * 24 * 60 * 60) // max1 gets time.Time struct: {-9223371974719179009 999999999} max1 := time.Unix(1<<63-1, 999999999) // max2 gets time.Time struct: {9223372036854775807 999999999} max2 := time.Unix(1<<63-1-unixToInternal, 999999999) // t0 is definitely before the year 292277026596 t0 := time.Date(2015, 9, 16, 19, 17, 23, 0, time.UTC) // t0 < max1 doesn't work: prints false fmt.Println(t0.Before(max1)) // max1 < t0 doesn't work: prints true fmt.Println(t0.After(max1)) fmt.Println(max1.Before(t0)) // t0 < max2 works: prints true fmt.Println(t0.Before(max2)) // max2 < t0 works: prints false fmt.Println(t0.After(max2)) fmt.Println(max2.Before(t0)) 

So, although this is a bit of a pain, you can use time.Unix(1<<63-62135596801, 999999999) if you want to use max time.Time , which is useful for comparisons such as finding the minimum value in a range of times.

+11
source

Source: https://habr.com/ru/post/973183/


All Articles