Prepare for a backend job by mastering computer science fundamentals

Announcing Go-TinyTime, Go-TinyDate’s Sister Package

By Lane Wagner on Apr 2, 2020

time.Time is the perfect choice for handling times in Go in most cases, it even comes in the standard library! The problem is that the time.Time{} struct uses more than 24 bytes of memory under most conditions. Go-TinyTime solves this problem by restricting the available dates to the range between 1970 - 2106, and only supporting UTC timezones. This brings data usage down to just 4 bytes of memory.

Go-TinyDate is its sister package that allows for a much larger date range but doesn’t get more than day precision. Between time.Time, go-tinydate, and go-tinytime all of our time problems can be solved using the same API.

Don’t forget to ⭐ the Github

How Does It Work?

A normal time.Time object takes at least 16 bytes of memory:

type Time struct {
	wall uint64 // 8 bytes
	ext  int64 // b bytes
	loc *Location // 8 bytes if not nil, plus location memory
}

If there is a location set (which there usually is), then this can be higher, usually about 24 bytes.

TinyTime, on the other hand, uses only 4 bytes.

type TinyTime struct {
	unix uint32
}

We sacrifice timezones and dates older than the unix epoch of 1970, but if these are acceptable tradeoffs, we can save a lot of memory.

A simple path to your career in backend development

The pace of Boot.dev's JavaScript, Python and Go courses has been perfect for me. The diverse community in Discord is a blast, and other members are quick to help out with detailed answers and explanations.

- Daniel Gerep from Cassia, Brasil

When Should It Be Used?

As the TinyTime Readme states, if you aren’t hurting for resources, better to stick with the standard time.Time. The following situations can be good reasons to use to TinyTime:

API

The tinytime.TinyTime API largely mirrors that of time.Time. The only methods missing are the ones that make no sense without timezone support. You can swap out the vast majority without any changes. Check out the godoc for reference: https://godoc.org/github.com/wagslane/go-tinytime.

Get backend development jobs by learning CS

I was a field service engineer and I wanted to become a backend developer, but work and family limited my options. Since completing the backend-focused computer science track on Boot.dev, I now have a job offer in hand and I'm starting my career as a software developer!

- Özgür Yildirim from Germany

TinyDate

If you need a larger date range, be sure to check out the intro to Go-TinyDate.

tiny fragile box

Become a backend engineer by building real projects

Related Reading