🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ekingery

ekingery's solution

to Gigasecond in the Go Track

Published at Nov 21 2019 · 0 comments
Test suite

Calculate the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.


Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


package gigasecond

// Source: exercism/problem-specifications
// Commit: 5506bac gigasecond: Apply new "input" policy
// Problem Specifications Version: 1.1.0

// Add one gigasecond to the input.
var addCases = []struct {
	description string
	in          string
	want        string
		"date only specification of time",
		"second test for date only specification of time",
		"third test for date only specification of time",
		"full time specified",
		"full time with day roll-over",


package gigasecond

// Write a function AddGigasecond that works with time.Time.

import (

// date formats used in test data
const (
	fmtD  = "2006-01-02"
	fmtDT = "2006-01-02T15:04:05"

func TestAddGigasecond(t *testing.T) {
	for _, tc := range addCases {
		in := parse(tc.in, t)
		want := parse(tc.want, t)
		got := AddGigasecond(in)
		if !got.Equal(want) {
			t.Fatalf(`FAIL: %s
   = %s
want %s`, tc.description, in, got, want)
		t.Log("PASS:", tc.description)
	t.Log("Tested", len(addCases), "cases.")

func parse(s string, t *testing.T) time.Time {
	tt, err := time.Parse(fmtDT, s) // try full date time format first
	if err != nil {
		tt, err = time.Parse(fmtD, s) // also allow just date
	if err != nil {
		// can't run tests if input won't parse.  if this seems to be a
		// development or ci environment, raise an error.  if this condition
		// makes it to the solver though, ask for a bug report.
		_, statErr := os.Stat("example_gen.go")
		if statErr == nil || os.Getenv("TRAVIS_GO_VERSION") > "" {
		} else {
			t.Skip("(This is not your fault, and is unexpected.  " +
				"Please file an issue at https://github.com/exercism/go.)")
	return tt

func BenchmarkAddGigasecond(b *testing.B) {
	for i := 0; i < b.N; i++ {
// https://exercism.io/tracks/go/exercises/gigasecond
package gigasecond

// import path for the time package from the standard library
import "time"

const Gigasecond = time.Second * 1e9

// Add a Gigasecond (10^9) to the input time
func AddGigasecond(t time.Time) time.Time {
	return t.Add(Gigasecond)

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?