Short history of time
Tomasz Nurkiewicz
 | 
2013­07­09T16:35:50.090Z
nurkiewicz@gmail.com @tnurkiewicz
Tomasz Nurkiewicz
 | 
6+ years with Java
Scala, JavaScript, Clojure...
Back­end, data analysis and visualization
 | 
Used ...
Bad day at Microsoft
 
blogs.msdn.com/b/windowsazure/archive/2012/03/09/summary­of­windows­azure­service­disruption­on­feb...
Program result?
Java:
C#:
Calendar cal = new GregorianCalendar(2012, FEBRUARY, 29, 15, 0);
cal.add(YEAR, 1);
System.out.pr...
February 28th, 2013, 15:00
February 29th, 2013, 15:00
March 1st, 2013, 00:00
March 1st, 2013, 15:00
IllegalArgumentExcepti...
True story
“[...] in accordance with Terms and Conditions [...] interest is paid
for actual number of days funds were on a...
Bad second for Linux
 
www.greenprophet.com/2012/07/leap­second­bug­consumes­megawatts­of­electricity/
Leap seconds
 
en.wikipedia.org/wiki/Leap_second
Inne błędy
Problem of year 2000 (Y2K)
Problem of year 2011 (Taiwan)
Problem of year 2038 (Integer.MAX_VALUE)
Problem of ye...
Understand the domain
...of every problem
Date representation
 
www.edali.org/persistence­of­memory.jsp
Seconds since arbitrary moment in time
Calendar system
Time axis
Time "0"?
Date Used in
0. January 0 MATLAB
1. January 0 Symbian, Turbo DB
1. January 1 Microsoft .NET, Go
1. January 1601 ...
java.util.Date
:
vs.
:
en.wikipedia.org/wiki/Calendar_date
“A date in a calendar is a reference to a particular day
repres...
Time zones
java.util.Timezone
Time difference between Warsaw and Sydney?
 
www.travel.com.hk/region/timezone.htm
DST (Daylight saving time)
 
en.wikipedia.org/wiki/Daylight_saving_time
Daylight saving time
Winter → Summer Summer → Winter
en.wikipedia.org/wiki/Daylight_saving_time
...so?
 
www.travel.com.hk/region/timezone.htm
Representation
WRONG!
final TimeZone tz = TimeZone.getTimeZone("Europe/Warsaw");
TimeZone.getTimeZone("GMT+01:00");
TimeZo...
Calendar date
java.util.Calendar
Leap years - WRONG!
def leapYear(year: Int): Boolean =
year % 4 == 0
Leap years - poor
def leapYear(year: Int): Boolean =
(year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0)
Leap years
def leapYear(year: Int): Boolean =
new GregorianCalendar(year, JANUARY, 1).
getActualMaximum(DAY_OF_YEAR) > 365
Puzzle
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR));
We have (Gregorian) year 2013
Meanwhile in Thailand...
...and in Japan...
$ java ...
2013
$ java -Duser.country=TH -Duser....
Better:
Even better:
Just in case...
Calendar c = new GregorianCalendar();
Calendar c = new GregorianCalendar(timeZone);
C...
Datevs. Calendar?
"...after some another event"
"...within 10 seconds"
"...within an hour"
"...within 24 hours"
"...within...
Practice
Joda Time
final DateTime yearLater = new DateTime(2012, 2, 29, 15, 0).plusYears(1);
joda­time.sourceforge.net
Joda Time and JAX-WS
import org.joda.time.DateTime;
import javax.xml.bind.DatatypeConverter;
public class XsdJodaTimeConve...
JAX-WS: .xjbfile
<bindings version="1.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchem...
Joda Time and JPA 2.1
import org.joda.time.Instant;
import javax.persistence.AttributeConverter;
import javax.persistence....
Testing - tools
Controlled time source ( )
"Exotic" default time zone
Don't sleep! (Thread.sleep()), 
ScalaCheck
fake syst...
Testing - edge cases
Beginning/end of month/year
Weekends
29th of February
Time zones, DST
ScalaCheck and ScalaTest
Negative result
implicit override val generatorDrivenConfig =
PropertyCheckConfig(minSuccessful =...
Events in the future
,  , ...
Quartz
JMS
Activiti jBPM
Quartz scheduler
newTrigger()
.startAt(futureDate(1, YEAR))
.build();
quartz­scheduler.org/documentation/quartz­2.1.x/tuto...
JMS with delay
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMes...
Bonus / Computus
(defn easter [year]
(let [
a (mod year 19)
b (Math/floor (/ year 100))
c (mod year 100)
d (Math/floor (/ ...
Bugs, more bugs...
1.  "Due to the lack of [time] synchronization [...] a car bomb went off [...] one hour earlier than
ex...
Interesting and useful
1.   ­ everything about time
2.   ­ UNIX time
3.   ­ foundation for this presentation
4.   ­ defini...
Thank you for your... time!
 
Twitter: 
nurkiewicz@gmail.com
@tnurkiewicz
nurkiewicz.github.io/talks/confitura2013
Upcoming SlideShare
Loading in …5
×

Short history of time - Confitura 2013

1,798 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,798
On SlideShare
0
From Embeds
0
Number of Embeds
67
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Short history of time - Confitura 2013

  1. 1. Short history of time Tomasz Nurkiewicz  |  2013­07­09T16:35:50.090Z nurkiewicz@gmail.com @tnurkiewicz
  2. 2. Tomasz Nurkiewicz  |  6+ years with Java Scala, JavaScript, Clojure... Back­end, data analysis and visualization  |  Used to be active on  Third time speaker on Javarsovia/Confitura Working in Oslo ( ) nurkiewicz.blogspot.com nurkiewicz@gmail.com @tnurkiewicz scala.net.pl github.com/nurkiewicz StackOverflow we are hiring!
  3. 3. Bad day at Microsoft   blogs.msdn.com/b/windowsazure/archive/2012/03/09/summary­of­windows­azure­service­disruption­on­feb­29th­2012.aspx
  4. 4. Program result? Java: C#: Calendar cal = new GregorianCalendar(2012, FEBRUARY, 29, 15, 0); cal.add(YEAR, 1); System.out.println(cal.getTime()); DateTime cal = new DateTime(2012, 2, 29, 15, 0, 0).AddYears(1); Console.WriteLine(cal);
  5. 5. February 28th, 2013, 15:00 February 29th, 2013, 15:00 March 1st, 2013, 00:00 March 1st, 2013, 15:00 IllegalArgumentException
  6. 6. True story “[...] in accordance with Terms and Conditions [...] interest is paid for actual number of days funds were on account [...] however it is assumed that year consists of 365 days. Year 2012 has 366 days, thus  interest is not paid for 29th of February. Yours faithfully, [some] Bank” samcik.blox.pl/2012/03/Dzien­ktorego­nie­ma­Sprawdz­co­bank­wykreslil.html
  7. 7. Bad second for Linux   www.greenprophet.com/2012/07/leap­second­bug­consumes­megawatts­of­electricity/
  8. 8. Leap seconds   en.wikipedia.org/wiki/Leap_second
  9. 9. Inne błędy Problem of year 2000 (Y2K) Problem of year 2011 (Taiwan) Problem of year 2038 (Integer.MAX_VALUE) Problem of year 2042 (IBM S/370) Problem of year 2107 (MS­DOS FAT) Problem of September 9th, '99 (9/9/99) en.wikipedia.org/wiki/Time_formatting_and_storage_bugs
  10. 10. Understand the domain ...of every problem
  11. 11. Date representation   www.edali.org/persistence­of­memory.jsp
  12. 12. Seconds since arbitrary moment in time Calendar system
  13. 13. Time axis
  14. 14. Time "0"? Date Used in 0. January 0 MATLAB 1. January 0 Symbian, Turbo DB 1. January 1 Microsoft .NET, Go 1. January 1601 NTFS, COBOL, Win32/Win64 1. January 1753 Microsoft SQL Server 31. December 1840 MUMPS 17. November 1858 VMS, United States Naval Observatory, DVB SI, astronomia 30. December 1899 Microsoft COM DATE, Object Pascal 0. January 1900 Microsoft Excel, Lotus 1­2­3 1. January 1900 NTP, IBM CICS, Mathematica, RISC OS, Common Lisp 1. January 1904 LabVIEW, Mac OS 9, Palm OS, MP4 1. January 1950 SEGA Dreamcast Date Used in 1. January 1960 S­Plus, SAS 31. December 1967 Pick OS 1. January 1970 Linux, Mac OS X, C, Java, JavaScript, Perl, PHP, Python, Tcl, ActionScript 1. January 1978 AmigaOS 1. January 1980 DOS, OS/2, FAT16 I FAT32, VOS 6. January 1980 Qualcomm BREW, GPS 1. January 1981 Acorn NetFS 1. January 1984 CiA® CANopen® 22. August 1999 Satelita Galileo 1. January 2000 PostgreSQL, AppleSingle, AppleDouble 1. January 2001 Apple Cocoa en.wikipedia.org/wiki/Epoch_date
  15. 15. java.util.Date : vs. : en.wikipedia.org/wiki/Calendar_date “A date in a calendar is a reference to a particular day represented within a calendar system. [...] A particular day may be represented by a different date in another calendar” docs.oracle.com/javase/7/docs/api/java/util/Date.html “The class Date represents a specific instant in time, with millisecond precision.”
  16. 16. Time zones java.util.Timezone
  17. 17. Time difference between Warsaw and Sydney?   www.travel.com.hk/region/timezone.htm
  18. 18. DST (Daylight saving time)   en.wikipedia.org/wiki/Daylight_saving_time
  19. 19. Daylight saving time Winter → Summer Summer → Winter en.wikipedia.org/wiki/Daylight_saving_time
  20. 20. ...so?   www.travel.com.hk/region/timezone.htm
  21. 21. Representation WRONG! final TimeZone tz = TimeZone.getTimeZone("Europe/Warsaw"); TimeZone.getTimeZone("GMT+01:00"); TimeZone.getTimeZone("Europe/warsaw");
  22. 22. Calendar date java.util.Calendar
  23. 23. Leap years - WRONG! def leapYear(year: Int): Boolean = year % 4 == 0
  24. 24. Leap years - poor def leapYear(year: Int): Boolean = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
  25. 25. Leap years def leapYear(year: Int): Boolean = new GregorianCalendar(year, JANUARY, 1). getActualMaximum(DAY_OF_YEAR) > 365
  26. 26. Puzzle Calendar c = Calendar.getInstance(); System.out.println(c.get(Calendar.YEAR));
  27. 27. We have (Gregorian) year 2013 Meanwhile in Thailand... ...and in Japan... $ java ... 2013 $ java -Duser.country=TH -Duser.language=th ... 2556 $ java -Duser.country=JP -Duser.language=ja -Duser.variant=JP ... 25
  28. 28. Better: Even better: Just in case... Calendar c = new GregorianCalendar(); Calendar c = new GregorianCalendar(timeZone); Calendar c = new GregorianCalendar(timeZone, locale);
  29. 29. Datevs. Calendar? "...after some another event" "...within 10 seconds" "...within an hour" "...within 24 hours" "...within one day" "...in 2013" "...after 17:00" "...on Friday"
  30. 30. Practice
  31. 31. Joda Time final DateTime yearLater = new DateTime(2012, 2, 29, 15, 0).plusYears(1); joda­time.sourceforge.net
  32. 32. Joda Time and JAX-WS import org.joda.time.DateTime; import javax.xml.bind.DatatypeConverter; public class XsdJodaTimeConverter { public static DateTime unmarshal(String dateTime) { final long millis = DatatypeConverter. parseDate(dateTime). getTimeInMillis(); return new DateTime(millis); } public static String marshal(DateTime calendar) { return DatatypeConverter.printDate( calendar.toGregorianCalendar() ); } }
  33. 33. JAX-WS: .xjbfile <bindings version="1.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <globalBindings> <javaType xmlType="xs:dateTime" name="org.joda.time.DateTime" parseMethod="XsdJodaTimeConverter.unmarshal" printMethod="XsdJodaTimeConverter.marshal"/> </globalBindings> </bindings>
  34. 34. Joda Time and JPA 2.1 import org.joda.time.Instant; import javax.persistence.AttributeConverter; import javax.persistence.Converter; import java.util.Date; @Converter(autoApply = true) public class JodaTimeConverter implements AttributeConverter<Instant, Date> { @Override public Date convertToDatabaseColumn(Instant attr) { return attr != null? attr.toDate(): null; } @Override public Instant convertToEntityAttribute(Date dbData) { return dbData != null? new Instant(dbData): null; } }
  35. 35. Testing - tools Controlled time source ( ) "Exotic" default time zone Don't sleep! (Thread.sleep()),  ScalaCheck fake system clock Awaitility
  36. 36. Testing - edge cases Beginning/end of month/year Weekends 29th of February Time zones, DST
  37. 37. ScalaCheck and ScalaTest Negative result implicit override val generatorDrivenConfig = PropertyCheckConfig(minSuccessful = 10000, workers = 4) test("any date +1 year and -1 year should yield same date back") { check { random: Date => { val plusMinusYear = new GregorianCalendar plusMinusYear.setTime(random) plusMinusYear.add(YEAR, 1) plusMinusYear.add(YEAR, -1) random == plusMinusYear.getTime } } } Falsified after 2665 passed tests: arg0 = Mon Feb 29 03:21:22 CET 73843340
  38. 38. Events in the future ,  , ... Quartz JMS Activiti jBPM
  39. 39. Quartz scheduler newTrigger() .startAt(futureDate(1, YEAR)) .build(); quartz­scheduler.org/documentation/quartz­2.1.x/tutorials/tutorial­lesson­05
  40. 40. JMS with delay MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage("...hello, delayed!"); message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, oneYearMillis); producer.send(message); http://activemq.apache.org/delay­and­schedule­message­delivery.html
  41. 41. Bonus / Computus (defn easter [year] (let [ a (mod year 19) b (Math/floor (/ year 100)) c (mod year 100) d (Math/floor (/ b 4)) e (mod b 4) f (Math/floor (/ (+ b 8) 25)) g (Math/floor (/ (inc (- b f)) 3)) h (mod (+ (- (+ (* 19 a) b) d g) 15) 30) i (Math/floor (/ c 4)) k (mod c 4) L (mod (- (+ 32 (* 2 e) (* 2 i)) h k) 7) m (Math/floor (/ (+ a (* 11 h) (* 22 L)) 451)) n (- (+ h L 114) (* 7 m)) month (dec (Math/floor (/ n 31))) day (inc (mod n 31))] (java.util.GregorianCalendar. year month day))) en.wikipedia.org/wiki/Computus
  42. 42. Bugs, more bugs... 1.  "Due to the lack of [time] synchronization [...] a car bomb went off [...] one hour earlier than expected" ( ) 2.  "F­22 Raptors [...] experienced multiple computer crashes coincident with their crossing of [...] the International Date Line" ( ) 3.  "Damage to a German steel facility occurred during a DST transition" ( ) 4.  5.  6.  Unjustified fraud accusation (  ­ [37]) 7.  Catalog of few hundred bugs, up to year 2000 (!) ( ) catless.ncl.ac.uk/Risks/20.58.html#subj12 en.wikipedia.org/wiki/List_of_software_bugs en.wikipedia.org/wiki/Daylight_Savings_Time www.wired.com/wiredenterprise/2012/07/leap­second­bug­wreaks­havoc­with­java­ linux www.theregister.co.uk/2012/07/02/leap_second_crashes_airlines www.cs.tau.ac.il/~nachumd/horror.html www.csl.sri.com/users/neumann/cal.html
  43. 43. Interesting and useful 1.   ­ everything about time 2.   ­ UNIX time 3.   ­ foundation for this presentation 4.   ­ definitions of TAI, UT, UTC... 5.   ­ Samoa and Tokelau skip a day for dateline change www.timeanddate.com www.epochconverter.com www.odi.ch/prog/design/datetime.php tycho.usno.navy.mil/systime.html www.bbc.co.uk/news/world­asia­16351377
  44. 44. Thank you for your... time!   Twitter:  nurkiewicz@gmail.com @tnurkiewicz nurkiewicz.github.io/talks/confitura2013

×