Skip to Content
Go Cookbook
book

Go Cookbook

by Sau Sheong Chang
September 2023
Intermediate to advanced
367 pages
7h 23m
English
O'Reilly Media, Inc.
Content preview from Go Cookbook

Chapter 11. Date and Time Recipes

11.0 Introduction

Time (and as an extension of that, date) manipulation is an important part of any programming language. We use it to keep track of time, calculate the elapsed duration between times, format dates and times for representation, and much more. However, time manipulation is often not as straightforward as it seems.

Our computers have two different types of clocks—a wall clock and a monotonic clock. The wall clock is what we’re used to and is literally a wall clock that we look at to determine the time of day. The wall clock is usually synchronized with a Network Time Protocol (NTP) server to set the correct time on the computer. As a result, the wall clock can sometimes jump back and forth.

Also, the clock itself might be adjusted by a user or another program, so if you’re using the wall clock to measure duration, the timing might not be accurate. For example, if the clock is changed while you’re trying to measure how long a task takes, the results might become a negative number!

A monotonic clock, on the other hand, provides a time that is always going forward. With the same example, you won’t be affected by clock changes or adjustments. In general, you should be using the wall clock to tell the time, and the monotonic clock to measure duration.

Like many programming languages, Go has a pretty good package to help with time and it’s called (no surprise here) the time package.

11.1 Telling Time

Problem

You want to know the current ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go Cookbook

Go Cookbook

Aaron Torres
React Cookbook

React Cookbook

David Griffiths, Dawn Griffiths
GitOps Cookbook

GitOps Cookbook

Natale Vinto, Alex Soto Bueno

Publisher Resources

ISBN: 9781098122102Errata Page