Avatar of artemkorsakov

artemkorsakov's solution

to Gigasecond in the Go Track

Published at Jan 31 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.
var addCases = []struct {
	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"
)

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
AddGigasecond(%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") > "" {
			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
}

func BenchmarkAddGigasecond(b *testing.B) {
	for i := 0; i < b.N; i++ {
		AddGigasecond(time.Time{})
	}
}
package gigasecond

import "time"

func AddGigasecond(t time.Time) time.Time {
	return t.Add(time.Duration(1000000000) * time.Second)
}

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?