commit b725a89aa9d6debde9d6b7a34dce24e977ea8e35 Author: Jim Infield Date: Wed Mar 20 20:20:20 2019 -0600 initial commit diff --git a/.exercism/metadata.json b/.exercism/metadata.json new file mode 100644 index 0000000..8c1b331 --- /dev/null +++ b/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"go","exercise":"gigasecond","id":"2b46974c00074dedabcd72416fcb0371","url":"https://exercism.io/my/solutions/2b46974c00074dedabcd72416fcb0371","handle":"jinfield","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5dbad66 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Gigasecond + +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](http://exercism.io/languages/go/resources). + +## Source + +Chapter 9 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=09](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. diff --git a/cases_test.go b/cases_test.go new file mode 100644 index 0000000..ba6c58c --- /dev/null +++ b/cases_test.go @@ -0,0 +1,38 @@ +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", + }, +} diff --git a/gigasecond.go b/gigasecond.go new file mode 100644 index 0000000..f3e7819 --- /dev/null +++ b/gigasecond.go @@ -0,0 +1,10 @@ +package gigasecond + +import "time" + +const testVersion = 4 + +// AddGigasecond adds 1 billion seconds to the initial time +func AddGigasecond(t time.Time) time.Time { + return t.Add(1e9 * time.Second) +} \ No newline at end of file diff --git a/gigasecond_test.go b/gigasecond_test.go new file mode 100644 index 0000000..ae90d69 --- /dev/null +++ b/gigasecond_test.go @@ -0,0 +1,58 @@ +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{}) + } +}