Avatar of shamsartem

shamsartem's solution

to Gigasecond in the Elm Track

Published at Oct 04 2019 · 0 comments
Instructions
Test suite
Solution

Given a moment, determine the moment that would be after a gigasecond has passed.

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

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import Gigasecond exposing (add)
import Iso8601
import Parser
import Test exposing (..)
import Time


tests : Test
tests =
    describe "Gigasecond"
        [ describe "add"
            [ test "2011-04-25" <|
                \() ->
                    Expect.equal "2043-01-01T01:46:40.000Z"
                        (gigasecond "2011-04-25")
            , skip <|
                test "1977-06-13" <|
                    \() ->
                        Expect.equal "2009-02-19T01:46:40.000Z"
                            (gigasecond "1977-06-13")
            , skip <|
                test "1959-07-19" <|
                    \() ->
                        Expect.equal "1991-03-27T01:46:40.000Z"
                            (gigasecond "1959-07-19")
            , skip <|
                test "full time specified" <|
                    \() ->
                        Expect.equal "2046-10-02T23:46:40.000Z"
                            (gigasecond "2015-01-24T22:00:00.000Z")
            , skip <|
                test "full time with day roll-over" <|
                    \() ->
                        Expect.equal "2046-10-03T01:46:39.000Z"
                            (gigasecond "2015-01-24T23:59:59.000Z")
            ]
        ]


gigasecond : String -> String
gigasecond date =
    case Iso8601.toTime date of
        Ok posix ->
            posix
                |> Gigasecond.add
                |> Iso8601.fromTime

        _ ->
            ""
module Gigasecond exposing (add)

import Time


add : Time.Posix -> Time.Posix
add timestamp =
    timestamp
        |> Time.posixToMillis
        |> (\millis -> millis + 10 ^ 12)
        |> Time.millisToPosix

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?