Skip to content

Commit a754e8e

Browse files
authored
Merge pull request #262 from abo64/issue-259-gigasecond
gigasecond: Switch to java.time library
2 parents c3a728a + 337af13 commit a754e8e

File tree

5 files changed

+60
-48
lines changed

5 files changed

+60
-48
lines changed

config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
"slug": "gigasecond",
114114
"difficulty": 1,
115115
"topics": [
116+
"Dates",
117+
"Time",
118+
"Function Overloading"
116119
]
117120
},
118121
{

exercises/gigasecond/HINTS.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
## Hints
2-
Note that `GregorianCalendar` is a mutable object.
3-
You should not change this mutable object to avoid any side effects!
2+
Note that `addGigaseconds` accepts two different types of input.

exercises/gigasecond/build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
scalaVersion := "2.11.7"
1+
scalaVersion := "2.12.1"
22

3-
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.5" % "test"
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

exercises/gigasecond/example.scala

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import java.util.{Calendar, GregorianCalendar}
1+
import java.time.LocalDate
2+
import java.time.LocalDateTime
3+
import java.time.LocalTime
24

3-
case class Gigasecond(initial: Calendar) {
4-
val date = {
5-
val copy = initial.clone.asInstanceOf[Calendar]
6-
copy.add(Calendar.SECOND, 1000000000)
7-
copy
5+
object Gigasecond {
6+
private type Seconds = Long
7+
8+
def addGigaseconds(startDate: LocalDate): LocalDateTime = {
9+
val startDateTime = LocalDateTime.of(startDate, LocalTime.of(0, 0))
10+
addGigaseconds(startDateTime)
811
}
12+
13+
def addGigaseconds(startDateTime: LocalDateTime): LocalDateTime =
14+
startDateTime.plusSeconds(OneGigasecond)
15+
16+
private val OneGigasecond: Seconds = math.pow(10, 9) toLong
917
}
Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
1-
import org.scalatest._
2-
import java.util.{TimeZone, GregorianCalendar}
1+
import java.time.LocalDate
2+
import java.time.LocalDateTime
3+
import java.time.format.DateTimeFormatter
4+
5+
import org.scalatest.FunSuite
6+
import org.scalatest.Matchers
37

48
class GigasecondTests extends FunSuite with Matchers {
5-
test ("1") {
6-
// Note: Months are 0-indexed. 3 = April
7-
val cal = new GregorianCalendar(2011, 3, 25)
8-
cal.setTimeZone(TimeZone.getTimeZone("GMT"))
9-
val gs = Gigasecond(cal)
10-
11-
val expected = new GregorianCalendar(2043, 0, 1, 1, 46, 40)
12-
expected.setTimeZone(TimeZone.getTimeZone("GMT"))
13-
gs.date should be (expected)
14-
}
159

16-
test ("2") {
17-
pending
18-
val cal = new GregorianCalendar(1977, 5, 13)
19-
cal.setTimeZone(TimeZone.getTimeZone("GMT"))
20-
val gs = Gigasecond(cal)
10+
private def dateTime(str: String): LocalDateTime =
11+
LocalDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(str))
12+
13+
private def date(str: String): LocalDate =
14+
LocalDate.from(DateTimeFormatter.ISO_DATE.parse(str))
15+
2116

22-
val expected = new GregorianCalendar(2009, 1, 19, 1, 46, 40)
23-
expected.setTimeZone(TimeZone.getTimeZone("GMT"))
24-
gs.date should be (expected)
17+
test("modern time") {
18+
val input = date("2011-04-25")
19+
val expected = dateTime("2043-01-01T01:46:40")
20+
Gigasecond.addGigaseconds(input) should be (expected)
2521
}
2622

27-
test ("3") {
23+
test("after epoch time") {
2824
pending
29-
val cal = new GregorianCalendar(1959, 6, 19)
30-
cal.setTimeZone(TimeZone.getTimeZone("GMT"))
31-
val gs = Gigasecond(cal)
25+
val input = date("1977-06-13")
26+
val expected = dateTime("2009-02-19T01:46:40")
27+
Gigasecond.addGigaseconds(input) should be (expected)
28+
}
3229

33-
val expected = new GregorianCalendar(1991, 2, 27, 1, 46, 40)
34-
expected.setTimeZone(TimeZone.getTimeZone("GMT"))
35-
gs.date should be (expected)
30+
test("before epoch time") {
31+
pending
32+
val input = date("1959-07-19")
33+
val expected = dateTime("1991-03-27T01:46:40")
34+
Gigasecond.addGigaseconds(input) should be (expected)
3635
}
3736

38-
test ("4") {
37+
test("full time specified") {
3938
pending
40-
val cal = new GregorianCalendar(2015, 0, 24, 23, 59, 59)
41-
cal.setTimeZone(TimeZone.getTimeZone("GMT"))
42-
val gs = Gigasecond(cal)
39+
val input = dateTime("2015-01-24T22:00:00")
40+
val expected = dateTime("2046-10-02T23:46:40")
41+
Gigasecond.addGigaseconds(input) should be (expected)
42+
}
4343

44-
val expected = new GregorianCalendar(2046, 9, 3, 1, 46, 39)
45-
expected.setTimeZone(TimeZone.getTimeZone("GMT"))
46-
gs.date should be (expected)
44+
test("full time with day roll-over") {
45+
pending
46+
val input = dateTime("2015-01-24T23:59:59")
47+
val expected = dateTime("2046-10-03T01:46:39")
48+
Gigasecond.addGigaseconds(input) should be (expected)
4749
}
4850

49-
test ("yourself") {
51+
test("your birthday") {
5052
pending
51-
// val yourBirthday = new GregorianCalendar(year, month-1, day)
52-
// val gs = Gigasecond(yourBirthday)
53-
// gs.date should be (new GregorianCalendar(2009, 0, 31, 0, 46, 40))
53+
val yourBirthday = date(???)
54+
val expected = dateTime(???)
55+
Gigasecond.addGigaseconds(yourBirthday) should be (expected)
5456
}
5557
}

0 commit comments

Comments
 (0)