Avatar of jpcweb

jpcweb's solution

to Gigasecond in the Scala Track

Published at Oct 08 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.

Hints

Note that addGigaseconds accepts two different types of input.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

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.

GigasecondTest.scala

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class GigasecondTest extends FunSuite with Matchers {

  private def dateTime(str: String): LocalDateTime =
    LocalDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(str))
  private def date(str: String): LocalDate =
    LocalDate.from(DateTimeFormatter.ISO_DATE.parse(str))

  test("date only specification of time") {
    val input = date("2011-04-25")
    val expected = dateTime("2043-01-01T01:46:40")
    Gigasecond.add(input) should be (expected)
  }

  test("second test for date only specification of time") {
    pending
    val input = date("1977-06-13")
    val expected = dateTime("2009-02-19T01:46:40")
    Gigasecond.add(input) should be (expected)
  }

  test("third test for date only specification of time") {
    pending
    val input = date("1959-07-19")
    val expected = dateTime("1991-03-27T01:46:40")
    Gigasecond.add(input) should be (expected)
  }

  test("full time specified") {
    pending
    val input = dateTime("2015-01-24T22:00:00")
    val expected = dateTime("2046-10-02T23:46:40")
    Gigasecond.add(input) should be (expected)
  }

  test("full time with day roll-over") {
    pending
    val input = dateTime("2015-01-24T23:59:59")
    val expected = dateTime("2046-10-03T01:46:39")
    Gigasecond.add(input) should be (expected)
  }
}
import java.time.LocalDate
import java.time.LocalDateTime

object Gigasecond {
  private val gigaSec = 1000000000
  def add(date:LocalDate): LocalDateTime = {
    val d = date.atStartOfDay
    d.plusSeconds(gigaSec)
  }
  def add(date:LocalDateTime): LocalDateTime = {
    date.plusSeconds(gigaSec)
  }
}

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?