RegressionTests__TimeAndDateTest.st
changeset 1924 cc644916d874
parent 1886 6deb0e9fa0d2
child 1927 ac82d7231d95
--- a/RegressionTests__TimeAndDateTest.st	Thu May 10 14:52:38 2018 +0200
+++ b/RegressionTests__TimeAndDateTest.st	Thu May 10 23:55:38 2018 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "{ Package: 'stx:goodies/regression' }"
 
 "{ NameSpace: RegressionTests }"
@@ -81,6 +83,8 @@
 !
 
 test_01c_ReadingTimeStamps
+    |t|
+
     self assert:(Timestamp readFrom:'2017-11-09 10:18:03.236') = (Timestamp year:2017 month:11 day:9 hour:10 minute:18 second:3 millisecond:236).
 
     "/ standard format
@@ -99,6 +103,66 @@
     "/ explicit format
     self assert:(Timestamp readFrom:'9 nov 2017') = (Timestamp year:2017 month:11 day:9 hour:0 minute:0 second:0).
 
+    "/ milliseconds
+    t := Timestamp readFrom:'2017-11-09 10:18:03.123'.
+    self assert:(t milliseconds = 123).
+
+    "/ microseconds
+    t := Timestamp readFrom:'2017-11-09 10:18:03.123456'.
+    self assert:(t microseconds = 123456).
+
+    "/ nanoseconds
+    t := Timestamp readFrom:'2017-11-09 10:18:03.123456789'.
+    self assert:(t nanoseconds = 123456789).
+
+    "/ picoseconds
+    t := Timestamp readFrom:'2017-11-09 10:18:03.123456789012'.
+    self assert:(t picoseconds = 123456789012).
+
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06' format:'%day-%month-%year %h:%m:%s' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:0). 
+    self assert:(t := Timestamp readFrom:'20021995131106' format:'%2d%2month%4y%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6  millisecond:0). 
+    self assert:(t := Timestamp readFrom:'200295131106' format:'%2d%2month%2y%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:95 month:2 day:20 hour:13 minute:11 second:6  millisecond:0). 
+    self assert:(t := Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1900)%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:1960 month:2 day:20 hour:13 minute:11 second:6 millisecond:0). 
+    self assert:(t := Timestamp readFrom:'200260131106' format:'%2d%2month%2(y2000)%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:2060 month:2 day:20 hour:13 minute:11 second:6 millisecond:0). 
+    self assert:(t := Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1950)%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:1960 month:2 day:20 hour:13 minute:11 second:6 millisecond:0). 
+    self assert:(t := Timestamp readFrom:'200260131106' format:'%2d%2month%2(y1980)%2h%2m%2s' language:nil onError:[self error]
+                ) = (Timestamp year:2060 month:2 day:20 hour:13 minute:11 second:6 millisecond:0). 
+    self assert:(t := Timestamp readFrom:'March 7 2009 2:30pm ' format:'%monthName %day %year %u:%m%a' language:#en onError:[self error]
+                ) = (Timestamp year:2009 month:3 day:7 hour:14 minute:30 second:0 millisecond:0). "/ EST is 5hrs behind
+    self assert:(t := Timestamp readFrom:'March 7 2009 2:30am ' format:'%monthName %day %year %u:%m%a' language:#en onError:[self error]
+                ) = (Timestamp year:2009 month:3 day:7 hour:2 minute:30 second:0 millisecond:0). "/ EST is 5hrs behind
+
+    self assert:(t := Timestamp readFrom:'March 7 2009 2:30pm EST' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self error]
+                ) = (UtcTimestamp year:2009 month:3 day:7 hour:19 minute:30 second:0 millisecond:0). "/ EST is 5hrs behind
+
+    self assert:(t := Timestamp readFrom:'March 7 2009 7:30pm UTC' format:'%monthName %day %year %u:%m%a %tz' language:#en onError:[self error]
+                ) = (Timestamp year:2009 month:3 day:7 hour:19 minute:30 second:0 millisecond:0). 
+
+    self assert:(t := Timestamp readFrom:'2015103' format:'%4y%3dayOfYear' onError:[self error]
+                ) = (Timestamp year:2015 month:3 day:10 hour:0 minute:0 second:0 millisecond:0). 
+
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.999' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:999). 
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.100' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:100). 
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.010' format:'%day-%month-%year %h:%m:%s.%i' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:100). 
+
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.1' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:100). 
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.01' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:10). 
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.001' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 millisecond:1). 
+    self assert:(t := Timestamp readFrom:'20-2-1995 13:11:06.12345' format:'%day-%month-%year %h:%m:%s.%f' language:nil onError:[self error]
+                ) = (Timestamp year:1995 month:2 day:20 hour:13 minute:11 second:6 microsecond:123450). 
+
     "
      self new test_01c_ReadingTimeStamps
     "
@@ -545,7 +609,11 @@
 
     ts := UtcTimestamp  year: 2005 month: 6 day: 15 hour: 17 minute: 37 second: 0 millisecond: 30.
     self assert: ts = (reader read: '05-0615T17:3700.03' withClass:UtcTimestamp).
-    self assert: ts = (reader read: '2005-06-15T17:37:00.0305486-00:00' withClass:UtcTimestamp).
+    self assert: ts = (reader read: '2005-06-15T17:37:00.030' withClass:UtcTimestamp).
+
+    ts := UtcTimestamp  year: 2005 month: 6 day: 15 hour: 17 minute: 37 second: 0 microsecond:300.
+    self assert: ts = (reader read: '05-0615T17:3700.0003' withClass:UtcTimestamp).
+    self assert: ts = (reader read: '2005-06-15T17:37:00.0003-00:00' withClass:UtcTimestamp).
 
     "
      self new test_10_readingISO8601_time
@@ -830,6 +898,128 @@
     "
 
     "Created: / 09-11-2017 / 10:00:30 / cg"
+!
+
+test_18_moreTimestampArithmetic
+    |t1 t2|
+
+    t1 := Timestamp year:2000 month:1 day:1.
+    t2 := t1 addMilliseconds:1.
+    self assert:t1 seconds = 0.
+    self assert:t2 seconds = 0.
+    self assert:t1 exactSeconds = 0.
+    self assert:t2 exactSeconds = 0.001.
+    self assert:t1 milliseconds = 0.
+    self assert:t2 milliseconds = 1.
+    self assert:t1 microseconds = 0.
+    self assert:t2 microseconds = 1000.
+    self assert:t1 nanoseconds = 0.
+    self assert:t2 nanoseconds = 1000000.
+    self assert:t1 picoseconds = 0.
+    self assert:t2 picoseconds = 1000000000.
+
+    self assert:(t2 - t1) milliseconds = 1.
+    self assert:(t2 - t1) microseconds = 1000.
+    self assert:(t2 - t1) nanoseconds = 1000000.
+    self assert:(t2 - t1) picoseconds = 1000000000.
+
+    "
+     self new test_18_moreArithmetic
+    "
+!
+
+test_19_comparing
+    |t1 t2|
+
+    t1 := Time now.
+    t2 := t1 addMilliseconds:1.
+
+    self assert:t2 > t1.
+    self assert:t2 >= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 <= t1) not.
+    self assert:(t2 < t1) not.
+
+    t1 := Time now.
+    t2 := t1 subtractMilliseconds:1.
+
+    self assert:t2 < t1.
+    self assert:t2 <= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 >= t1) not.
+    self assert:(t2 > t1) not.
+
+    t1 := Time now.
+    t2 := t1 + 1 milliseconds.
+    self assert:t2 = (t1 addMilliseconds:1).
+
+    t2 := t1 - 1 milliseconds.
+    self assert:t2 = (t1 addMilliseconds:-1).
+    self assert:t2 = (t1 subtractMilliseconds:1).
+
+    "
+     self new test_19_comparing
+    "
+!
+
+test_20_comparing
+    |t1 t2|
+
+    t1 := Timestamp now.
+    t2 := t1 addMilliseconds:1.
+
+    self assert:t2 > t1.
+    self assert:t2 >= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 <= t1) not.
+    self assert:(t2 < t1) not.
+
+    t1 := Timestamp now.
+    t2 := t1 subtractMilliseconds:1.
+
+    self assert:t2 < t1.
+    self assert:t2 <= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 >= t1) not.
+    self assert:(t2 > t1) not.
+
+    t1 := Timestamp now.
+    t2 := t1 + 1 milliseconds.
+    self assert:t2 = (t1 addMilliseconds:1).
+
+    t2 := t1 - 1 milliseconds.
+    self assert:t2 = (t1 addMilliseconds:-1).
+    self assert:t2 = (t1 subtractMilliseconds:1).
+
+    t1 := Timestamp now.
+    t2 := t1 + 1 microseconds.
+
+    self assert:t2 > t1.
+    self assert:t2 >= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 <= t1) not.
+    self assert:(t2 < t1) not.
+
+    t1 := Timestamp now.
+    t2 := t1 - 1 microseconds.
+
+    self assert:t2 < t1.
+    self assert:t2 <= t1.
+    self assert:(t2 = t1) not.
+    self assert:(t2 >= t1) not.
+    self assert:(t2 > t1) not.
+
+    t1 := Timestamp now.
+    t2 := t1 + 1 microseconds.
+    self assert:t2 = (t1 addMilliseconds:0.001).
+
+    t2 := t1 - 1 microseconds.
+    self assert:t2 = (t1 addMilliseconds:-0.001).
+    self assert:t2 = (t1 subtractMilliseconds:0.001).
+
+    "
+     self new test_20_comparing
+    "
 ! !
 
 !TimeAndDateTest class methodsFor:'documentation'!