[Go] what is a time in go

Time is an agreement

We often take time for granted, considering it as omnipresent and essential as air. Yet, when it comes to programming, handling time can be surprisingly complex. The Go programming language provides a robust time package to manage these complexities efficiently.

Let’s take a simple look at the ‘Time’ struct.

type Time struct {
    wall uint64 // Encodes the wall time and the monotonic clock flag
    ext  int64  // Additional data depending on the wall field
    loc  *Location // Time zone information
}

The wall field encodes multiple pieces of information:

  • The hasMonotonic flag indicates whether the time includes a monotonic clock, and it is stored in the 63rd bit.
  • The wall clock time, which consists of seconds and nanoseconds, is encoded as follows: Bits 62 to 30 denote the seconds. Bits 0 to 29 denote the nanoseconds.

What is a monotonic flag? Monotonic time is dependent on our system and is set when the operating system boots. It is independent of other time standards like network time servers. We need this time as a consistent standard within our system because the system clock can be adjusted and synchronized with external servers. If the system time changes, it can disrupt functions that rely on duration calculations, such as Since and others.

Let’s say you were trying to calculate the time interval of your database query. However, your system time has changed because daylight saving time has just ended! This change can cause the calculated time interval to become negative, as the system time is now set to an earlier time.

To prevent this, we need monotonic time, which depends only on the system’s internal clock. Monotonic time is never synchronized with external time servers or adjusted for daylight saving time, ensuring consistent and reliable time interval calculations.

Now, let’s see how wall and ext fields are used in this context.

If the monotonic flag is set to 0, 62~30 bit of wall field are set to 0, and 0~29 bit of records nanoseconds. The ext field is used as a signed timestamp which starts from January 1, year 1.

If the flag is set to 1, 62~30 bit of wall field is used as unsigned timestamp since Jan 1 year 1885 and 0~29 bit as nanoseconds. The ext fields holds a signed 64 bits of monotonic clock reading, which is a nanoseconds since process started.

loc field is a field used to change time with timezones

In conclusion, time in programming is a nuanced and intricate concept that goes beyond its everyday simplicity. The Go programming language, through its time package, provides a sophisticated means of handling time, incorporating both wall clock and monotonic clock representations. This dual approach ensures that our programs can handle time consistently and accurately, even in the face of changes to the system clock or external time adjustments. By understanding and leveraging these structures, we can write more reliable and precise code, acknowledging that indeed, time is an agreement—a complex yet fundamental aspect of our programming endeavors.

Dive deeper

// sec returns the time's seconds since Jan 1 year 1.
func (t *Time) sec() int64 {
	if t.wall&hasMonotonic != 0 {
		return wallToInternal + int64(t.wall<<1>>(nsecShift+1))
	}
	return t.ext
}

// unixSec returns the time's seconds since Jan 1 1970 (Unix time).
func (t *Time) unixSec() int64 { return t.sec() + internalToUnix }

sec function returns the seconds since Jan 1 year 1. However, if the monotonic flas is set, you can’t represent time before Jan 1 year 1885. Since the wall is set to unsigned timestamp and ext field is used as monotonic time.

The shift operation is <<1>>(nsecShift +1) so that the flag can be erased.

wallToInternal is set to (1884*365 + 1884/4 – 1884/100 + 1884/400) * secondsPerDay

As you can see, time is just bunch of agreements. There is no nature in it. It is somtimes very tricky to understand.

Therefore you need to sweat a lot to get used to using time with Go. If you need more knowledge about time see the official docs of time package.

Leave a Reply

Your email address will not be published. Required fields are marked *