How many bytes are in a golang time object - go

I am having to store a time object in an array of bytes in a go project I am working on and have to declare the size of the array up front. I can not find the length in bytes referenced anywhere. At this point, I am planning on using the time.MarshalBinary() from the time library to convert it to bytes and manually figuring it out. But I wanted to know if anyone has any reference to the number of bytes this is and if time.MarshalBinary() is the best method to use for converting to bytes.

The answer to this question is not as straight forward as it might seem. It depends a lot on how much detail you need to preserve in your marshaling.
As pointed out in another answer, you can simply use unsafe.Sizeof() to determine the in-memory size of a time object, but this has little resemblance to the actual marshaled size, for the simple reason that it contains a pointer. If we look at the definition of time.Time we see:
type Time struct {
// wall and ext encode the wall time seconds, wall time nanoseconds,
// and optional monotonic clock reading in nanoseconds.
//
// From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
// a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
// The nanoseconds field is in the range [0, 999999999].
// If the hasMonotonic bit is 0, then the 33-bit field must be zero
// and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
// If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
// unsigned wall seconds since Jan 1 year 1885, and ext holds a
// signed 64-bit monotonic clock reading, nanoseconds since process start.
wall uint64
ext int64
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// The nil location means UTC.
// All UTC times are represented with loc==nil, never loc==&utcLoc.
loc *Location
}
Whether you care about the timezone info stored in loc, is application dependent. If you always store UTC times (usually the best approach), then you can ignore this bit entirely, which means you can get by storing just the two uint64s.
But even these two fields depend on whether or not you're using a monotonic clock. When marshaling data, you almost certainly do not care about the monotonic clock, whether or not it's encoded in those bits.
What this means is that, in most cases, you should be able to store a full time object in 64 bits (8 bytes), plus a timezone indicator, if necessary.
Further, depending on the precision you need, you may be able to store only the seconds field (discarding sub-second precision), which needs only 33 bits. If you only care about minutes or days, you could use even less space.

You can use usafe.Sizeof to get the size in bytes of a variable. I did this
package main
import (
"fmt"
"time"
"unsafe"
)
func main() {
t := time.Now()
fmt.Printf("a: %T, %d\n", t, unsafe.Sizeof(t))
}
Looks like its 24 bytes! :)
Also MarshalBinary looks like it works, although it depends on where you are sending it and how you want to unmarshal it. It may be easier to simply convert it to a string then use that if you are using it in Javascript or something.

Related

time.Sub() returns 1 second despite the difference exceeding couple of years

I am trying to write a piece of code that will react to system time change due to synchronisation. Here's a rather simple code that is running inside of goroutine:
var start, end time.Time
var start_ts, end_ts int64
var diff_ts time.Duration
var diff time.Duration
for {
start = time.Now()
start_ts = start.Unix()
fmt.Printf("Now: => %v (%d);\n", start, start_ts)
time.Sleep(1 * time.Second)
end = time.Now()
end_ts = end.Unix()
fmt.Printf("New Now: %v (%d);\n", end, end_ts)
diff = end.Sub(start)
diff_ts = time.Duration(end_ts-start_ts) * time.Second
fmt.Printf("Measured time duration: %v (%v) %f (%f)\n", diff, diff_ts, diff.Seconds(), diff_ts.Seconds())
}
my problem is that when I change system time in another console, the time is read correctly, however the "original" time difference is incorrect and I have to resort to constructing the time difference manually. Here's the excerpt from the logs:
Now: => 2020-02-26 12:29:42.778827718 +0000 UTC m=+21.776791756 (1582720182);
New Now: 2017-01-01 01:02:03.391215325 +0000 UTC m=+22.777003266 (1483232523);
Measured time duration: 1.00021151s (-27635h27m39s) 1.000212 (-99487659.000000)
how come the diff object returns 1 second even though the difference is clearlly greater than that?
go's time package uses both "wall clock" (what you are trying to change) and a monotonic clock. From the docs:
Operating systems provide both a “wall clock,” which is subject to
changes for clock synchronization, and a “monotonic clock,” which is
not. The general rule is that the wall clock is for telling time and
the monotonic clock is for measuring time. Rather than split the API,
in this package the Time returned by time.Now contains both a wall
clock reading and a monotonic clock reading; later time-telling
operations use the wall clock reading, but later time-measuring
operations, specifically comparisons and subtractions, use the
monotonic clock reading.
[...]
If Times t and u both contain monotonic clock readings, the operations t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out using the monotonic clock readings alone, ignoring the wall clock readings.
This is specifically designed to prevent deviant app behavior when a clock-sync (ntp etc.) occurs (and pushes the clock back). go's time package ensures the monotonic clock reading always moves forward (when comparing or subtraction operations).

Length of time representation in Go

Under Unix, I'm working on a program that needs to behave differently depending on whether time is 32-bit (will wrap in 2038) or 64-bit.
I presume Go time is not magic and will wrap in 2038 on a platform with a 32-bit time_t. If this is false and it is somehow always 64-bit, clue me in because that will prevent much grief.
What's the simplest way in Go to write a test for the platform's time_t size? Is there any way simpler than the obvious hack with cgo?
If you really want to find the size of time_t, you can use cgo to link to time.h. Then the sizeof time_t will be available as C.sizeof_time_t. It doesn't get much simpler.
package main
// #include <time.h>
import "C"
import (
"fmt"
)
func main() {
fmt.Println(C.sizeof_time_t);
}
Other than trying to set the system time to increasingly distant dates, which is not very polite to anything else running on that system, I don't know of any way to directly query the limits of the hardware clock in a portable fashion in any programming language. C simply hard codes the size of time_t in a file provided by the operating system (on OS X it's /usr/include/i386/_types.h), so you're probably best off taking advantage of that information by querying the size of time_t via cgo.
But there's very few reasons to do this. Go does not use time_t and does not appear to suffer from 2038 issues unless you actually plan to have code running on a 32-bit machine in 2038. If that's your plan, I'd suggest finding a better plan.
I presume Go time is not magic and will wrap in 2038 on a platform with a 32-bit time_t. If this is false and it is somehow always 64-bit, clue me in because that will prevent much grief.
Most of the the Year 2038 Problem is programs assuming that the time since 1970 will fit in a 32-bit signed integer. This effects time and date functions, as well as network and data formats which choose to represent time as a 32-bit signed integer since 1970. This is not some hardware limit (except if it's actually 2038, see below), but rather a design limitation of older programming languages and protocols. There's nothing stopping you from using 64 bit integers to represent time, or choosing a different epoch. And that's exactly what newer programming languages do, no magic required.
Go was first released in 2009 long after issues such as Unicode, concurrency, and 32-bit time (ie. the Year 2038 Problem) were acknowledged as issues any programming language would have to tackle. Given how many issues there are with C's time library, I highly doubt that Go is using it at all. A quick skim of the source code confirms.
While I can't find any explicit mention in the Go documentation of the limits of its Time representation, it appears to be completely disconnected from C's time.h structures such as time_t. Since Time uses 64 bit integers, it seems to be clear of 2038 problems unless you're asking for actual clock time.
Digging into the Go docs for Time we find their 0 is well outside the range of a 32-bit time_t which ranges from 1901 to 2038.
The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC
time.Unix takes seconds and nanoseconds as 64 bit integers leaving no doubt that it is divorced from the size of time_t.
time.Parse will parse a year "in the range 0000..9999", again well outside the range of a 32-bit time_t.
And so on. The only limitation I could find is that a Duration is limited to 290 years because it has a nanosecond accuracy and 290 years is about 63 bits worth of nanoseconds.
Of course, you should test your code on a machine with a 32-bit time_t.
One side issue of the 2038 Problem is time zones. Computers calculate time zone information from a time zone database, usually the IANA time zone database. This allows one to get the time offset for a certain location at a certain time.
Computers have their own copy of the time zone database installed. Unfortunately its difficult to know where they are located or when they were last updated. To avoid this issue, most programming languages supply their own copy of the time zone database. Go does as well.
The only real limitation on a machine with 32-bit time is the limits of its hardware clock. This tells the software what time it is right now. A 32-bit clock only becomes an issue if your program is still running on a 32-bit machine in 2038. There isn't much point to mitigating that because everything on that machine will have the same problem and its unlikely they took it into account. You're better off decommissioning that hardware before 2038.
Ordinarily, time.Time uses 63 bits to represent wall clock seconds elapsed since January 1, year 1 00:00:00 UTC, up through 219250468-12-04 15:30:09.147483647 +0000 UTC. For example,
package main
import (
"fmt"
"time"
)
func main() {
var t time.Time
fmt.Println(t)
t = time.Unix(1<<63-1, 1<<31-1)
fmt.Println(t)
}
Playground: https://play.golang.org/p/QPs1m6eMPH
Output:
0001-01-01 00:00:00 +0000 UTC
219250468-12-04 15:30:09.147483647 +0000 UTC
If time.Time is monotonic (derived from time.Now()), time.Time uses 33 bits to represent wall clock seconds, covering the years 1885 through 2157.
References:
Package time
Proposal: Monotonic Elapsed Time Measurements in Go

Data type of time_Span and time in Ada

What is the data type of time and time_span in Ada language. And how the variables of this type are stored in memory?
Just looking at Ada.Real_Time.Time_Span, ARM D.8(5) says type Time_Span is private; and then (17) says the full declaration isn't defined by the language.
Further, (20) says
Values of the type Time_Span represent length of real time duration.
(31) says
Time_Span_First shall be no greater than –3600 seconds, and Time_Span_Last shall be no less than 3600 seconds.
and those statements are all that a portable Ada program can rely on.
Now, specifically for GNAT, we can look at the actual implementation on Github, where in the private part we find
-- Time and Time_Span are represented in 64-bit Duration value in
-- nanoseconds. For example, 1 second and 1 nanosecond is represented
-- as the stored integer 1_000_000_001. This is for the 64-bit Duration
-- case, not clear if this also is used for 32-bit Duration values.
type Time is new Duration;
Time_First : constant Time := Time'First;
Time_Last : constant Time := Time'Last;
type Time_Span is new Duration;
(note, this is for the desktop system: for embedded systems the types and values may be different).
So the answer is, on desktop GNAT, that Time and Time_Span are both stored as 64-bit values with least significant bit 1 nanosecond.
There is defined a type named Time in the Language Reference Manual. The memory layout of this type is implementation defined.
From Ada Reference Manual
A value of the type Time in package Calendar, or of some other time type, represents a time as reported by a corresponding clock.
Values of the type Time_Span represent length of real time duration.
In short terms Time type represent timestamp, Time_Span type represent duration of time period.

Why does dividing two time.durations in Go result in another time.duration?

I don't understand what it means to divide a time.Duration in Go.
For example, this is super lovely:
d,_ := time.ParseDuration("4s")
fmt.Println(d/4)
print 1s. Which is ace, because (naively) 4 seconds divided by 4 is 1 second.
It gets a little confusing though when we find out that the 4 in the denominator has to be a duration. So although:
d1 := time.Duration(4)
fmt.Println(d/d1)
also prints 1s, we know that d1 is actually 4ns and I'm entirely unconvinced that 4 seconds divided by 4 nanoseconds is 1 second.
I'm confused because a duration divided by duration should be dimensionless (I think, right?), whereas a duration divided by a dimensionless number should have units of time.
And I know that type != unit, but I'm clearly misunderstanding something, or quite possibly a set of things. Any help to clear this up would be most appreciated!
Here is a go playground of the above examples. https://play.golang.org/p/Ny2_ENRlX6. And just for context, I'm trying to calculate the average time between events. I can fall back to using floats for seconds, but am trying to stay in time.Duration land.
Mathematically, you're correct: dividing two time.Durations should result in a dimensionless quantity. But that's not how go's type system works. Any mathematical operation results in a value of the same type as the inputs. You'll have to explicitly cast the result of the division to an int64 to get an "untyped" quantity.
It is so because time.Duration is int64. See documentation of time package.
You make a division of 4000000000 (4s) by 4 (4ns) and you get 1000000000 (1s). You should look at the operations as they where integers not typed values. Type Duration make it look like a physical value but for division operation it is just a number.
There are no units attached to a time.Duration. A time.Duration represents the physical concept of a duration (measured in seconds and having a unit) by providing a distinct type, namely the time.Duration type. But technically it is just a uint64.
If you try to attach actual units to types you'll enter unit-hell: What would a (time.Duration * time.Duration)/acceleration.Radial * mass.MetricTon be? Undefined most probably.

Determining Millisecond Time Intervals In Cocoa

Just as background, I'm building an application in Cocoa. This application existed originally in C++ in another environment. I'd like to do as much as possible in Objective-C.
My questions are:
1)
How do I compute, as an integer, the number of milliseconds between now and the previous time I remembered as now?
2)
When used in an objective-C program, including time.h, what are the units of
clock()
Thank you for your help.
You can use CFAbsoluteTimeGetCurrent() but bear in mind the clock can change between two calls and can screw you over. If you want to protect against that you should use CACurrentMediaTime().
The return type of these is CFAbsoluteTime and CFTimeInterval respectively, which are both double by default. So they return the number of seconds with double precision. If you really want an integer you can use mach_absolute_time() found in #include <mach/mach_time.h> which returns a 64 bit integer. This needs a bit of unit conversion, so check out this link for example code. This is what CACurrentMediaTime() uses internally so it's probably best to stick with that.
Computing the difference between two calls is obviously just a subtraction, use a variable to remember the last value.
For the clock function see the documentation here: clock(). Basically you need to divide the return value by CLOCKS_PER_SEC to get the actual time.
How do I compute, as an integer, the number of milliseconds between now and the previous time I remembered as now?
Is there any reason you need it as an integral number of milliseconds? Asking NSDate for the time interval since another date will give you a floating-point number of seconds. If you really do need milliseconds, you can simply multiply by that by 1000 to get a floating-point number of milliseconds. If you really do need an integer, you can round or truncate the floating-point value.
If you'd like to do it with integers from start to finish, use either UpTime or mach_absolute_time to get the current time in absolute units, then use AbsoluteToNanoseconds to convert that to a real-world unit. Obviously, you'll have to divide that by 1,000,000 to get milliseconds.
QA1398 suggests mach_absolute_time, but UpTime is easier, since it returns the same type AbsoluteToNanoseconds uses (no “pointer fun” as shown in the technote).
AbsoluteToNanoseconds returns an UnsignedWide, which is a structure. (This stuff dates back to before Mac machines could handle scalar 64-bit values.) Use the UnsignedWideToUInt64 function to convert it to a scalar. That just leaves the subtraction, which you'll do the normal way.

Resources