dt — Go’s missing datetime package

Emir Ribic
Oct 11, 2019 · 2 min read

Go’s standard library contains a single date package — time. The type provided by it, Time, contains date, time and location information. More often than not we don’t need location info, or we need to represent date/time only. dt provides exactly that, a time-zone-independent representation of time that follows the rules of the proleptic Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second minutes.

Repo available at: https://github.com/ribice/dt

Most, if not all of the applications being built require some info about time. Whether it’s timestamp of object creation/update, user’s birthdate or some schedules.

Go’s time package represents a unique point in time, a timestamp. But a birthday doesn’t need to be represented by a timestamp, a simple date is enough. And same goes for many other usages.

Prior to dt, I would use Go’s time package and then strip date or time info depending on my needs. I realized that’s not the best way, thus I built dt.

dt provides a time-zone-independent representation of time that follows the rules of the proleptic Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second minutes.

What is provided?

dt provides three types to work with:

  • Time: Contains time info: HH:mm
  • Date: Contains date info: YYYY-MM-DD
  • DateTime: Contains date and time information: YYYY-MM-DDTHH:mm

Unlike time.Time these types contain an additional Valid field representing whether the data inside it was scanned/marshaled. This prevents situations like saving default date in a database when nothing was received or responding via JSON with default date even though the date was empty.

Types provided in dt represent sql types time, date and timestamp.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade