ekingery's solution

to Gigasecond in the Go Track

Published at Nov 21 2019 · 0 comments
Instructions
Test suite
Solution

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.

Source

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.

cases_test.go

``````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.
description string
in          string
want        string
}{
{
"date only specification of time",
"2011-04-25",
"2043-01-01T01:46:40",
},
{
"second test for date only specification of time",
"1977-06-13",
"2009-02-19T01:46:40",
},
{
"third test for date only specification of time",
"1959-07-19",
"1991-03-27T01:46:40",
},
{
"full time specified",
"2015-01-24T22:00:00",
"2046-10-02T23:46:40",
},
{
"full time with day roll-over",
"2015-01-24T23:59:59",
"2046-10-03T01:46:39",
},
}``````

gigasecond_test.go

``````package gigasecond

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

import (
"os"
"testing"
"time"
)

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

for _, tc := range addCases {
in := parse(tc.in, t)
want := parse(tc.want, t)
if !got.Equal(want) {
t.Fatalf(`FAIL: %s
= %s
want %s`, tc.description, in, got, want)
}
t.Log("PASS:", tc.description)
}
}

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") > "" {
t.Fatal(err)
} else {
t.Log(err)
t.Skip("(This is not your fault, and is unexpected.  " +
"Please file an issue at https://github.com/exercism/go.)")
}
}
return tt
}

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
}``````