Slide 1

Slide 1 text

I have a Date T renaud_mathieu

Slide 2

Slide 2 text

What day is it Today?

Slide 3

Slide 3 text

Problem #1 Different calendars…

Slide 4

Slide 4 text

Current year according to various historical and world calendars, as of January 1. 2023 0 1500 3000 4500 6000 Hebrew Chinese Gregorian Islamic French Revolutionary 230 1   2   4   5  

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Problem #2 Different timezones…

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

https://data.iana.org/time-zones/tzdb/NEWS

Slide 11

Slide 11 text

https://data.iana.org/time-zones/tzdb/NEWS

Slide 12

Slide 12 text

Problem #3 The rotation of the Earth is not regular…

Slide 13

Slide 13 text

UTC Coordinated Universal Time UTC Coordinated Universal Time UT Universal Time TAI International Atomic Time

Slide 14

Slide 14 text

UTC Français TUC Temps universel coordonné Anglais CUT Coordinated Universal Time 🤝 UTC Universal Time Coordinated Universel Temps Coordonné Coordinated Universal Time

Slide 15

Slide 15 text

UTC UTC Coordinated Universal Time GMT Greenwich Mean Time ≠ Coordinated Universal Time

Slide 16

Slide 16 text

🛠 Tools

Slide 17

Slide 17 text

MACHINE TIME MANAGEMENT HUMAN TIME MANAGEMENT

Slide 18

Slide 18 text

🛠 Unix Epoch • Unix time is a date and time representation widely used in computing. • Leap seconds* are known only 6 months in advance. • Year 2038 problem *leap seconds = secondes intercalaires 🇫🇷 MACHINE TIME MANAGEMENT

Slide 19

Slide 19 text

2023-04-27T15:50:00+02:00 General Principles 🛠 ISO 8601 MACHINE TIME MANAGEMENT HUMAN TIME MANAGEMENT 🤝

Slide 20

Slide 20 text

2023-04-27T15:50:00+02:00 YYYY-MM-DD or YYYYMMDD YYYY-MM (but not YYYYMM) Thh:mm:ss.sss or Thhmmss.sss Thh:mm:ss or Thhmmss Thh:mm or Thhmm Thh

Slide 21

Slide 21 text

History of Time, in a Java world…

Slide 22

Slide 22 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 23

Slide 23 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 24

Slide 24 text

java.util

Slide 25

Slide 25 text

java.util.Date import java.util.Date fun main() { val date = Date().apply { year = 2023 month = 4 day = 27 } print(date) } / / Fri May 04 21:18:05 UTC 3923

Slide 26

Slide 26 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 27

Slide 27 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 28

Slide 28 text

Joda Time & ThreeTen

Slide 29

Slide 29 text

Joda Time A Powerful and Versatile Date and Time Library • Provide an alternative to the JDK date management classes which have several drawbacks. • Aimed to provide a simpler and more intuitive API for handling dates and times.

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

ThreeTenBP ThreeTenABP

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 36

Slide 36 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 37

Slide 37 text

java.time

Slide 38

Slide 38 text

MACHINE TIME MANAGEMENT HUMAN TIME MANAGEMENT

Slide 39

Slide 39 text

MACHINE TIME MANAGEMENT

Slide 40

Slide 40 text

Machine Time Instant • Encapsulates a point in time with nanosecond precision. • Represent a point in the timeline. • The Instant class is the closest class to the java.util.Date class I MACHINE TIME MANAGEMENT

Slide 41

Slide 41 text

Machine Time Instant import java.time.Instant fun main() { println(Instant.EPOCH) println(Instant.now()) } —— 1970-01-01T00:00:00Z 2023-04-26T09:21:39.149061Z MACHINE TIME MANAGEMENT

Slide 42

Slide 42 text

Machine Time Duration • Encapsulates a time between two instants stored with nanosecond precision. • Duration can be negative! • The ISO-8601 standard de fi nes the textual format of a duration: PTnHnMn.nS MACHINE TIME MANAGEMENT

Slide 43

Slide 43 text

Machine Time Duration D I I MACHINE TIME MANAGEMENT

Slide 44

Slide 44 text

import java.time.Instant import java.time.Duration import java.time.temporal.ChronoUnit fun main() { var oneHour = Duration.ofHours(1L) println("oneHour = $oneHour") oneHour = Duration.ofMinutes(60L) println("oneHour = $oneHour") oneHour = Duration.ofSeconds(3600L) println("oneHour = $oneHour") val instant1 = Instant.parse("2023-04-26T23:59:59Z") val instant2 = instant1.plus(1L, ChronoUnit.DAYS) val duration = Duration.between(instant1, instant2) println("duration = $duration") } oneHour = PT1H oneHour = PT1H oneHour = PT1H duration = PT24H

Slide 45

Slide 45 text

MACHINE TIME MANAGEMENT HUMAN TIME MANAGEMENT

Slide 46

Slide 46 text

HUMAN TIME MANAGEMENT

Slide 47

Slide 47 text

LocalDate LocalTime LocalDateTime Period Duration LocalDate private final int year; private final short month; private final short day; val date1 = LocalDate.now() val date2 = LocalDate.of(1987, Month.JULY, 6) val date3 = LocalDate.ofEpochDay(113) println(date1) println(date2) println(date3) —— 2023-07-05 1987-07-06 1970-04-24

Slide 48

Slide 48 text

LocalDate LocalTime LocalDateTime Period Duration val time1 = LocalTime.now() val time2 = LocalTime.of(23,59,59,104534) println(time1) println(time2) println(time1.minusHours(1)) —— 10:06:27.256581 23:59:59.000104534 09:06:27.256581 LocalTime private final byte hour; private final byte minute; private final byte second; private final int nano;

Slide 49

Slide 49 text

LocalDate LocalTime LocalDateTime Period Duration val date = LocalDate.now() val time = LocalTime.now() val dateTime1: LocalDateTime = date.atTime(time) val dateTime2 = time.atDate(date) println(dateTime1) println(dateTime2) println(dateTime1.dayOfWeek) —— 2023-07-04T15:42:32.878386241 2023-07-04T15:42:32.878386241 TUESDAY LocalDateTime private final LocalDate date; private final LocalTime time; LocalTime LocalDate

Slide 50

Slide 50 text

LocalDate LocalTime LocalDateTime Period Duration val date = LocalDate.now().minusWeeks(2) val dateTime = LocalDateTime.now() val period = Period.between(date,dateTime.toLocalDate()) println(date) println(dateTime) println(period) —— 2023-06-20 2023-07-04T15:43:57.093814609 P14D Period private final int years; private final int months; private final int days;

Slide 51

Slide 51 text

LocalDate LocalTime LocalDateTime Period Duration val time1 = LocalTime.now() val time2 = LocalTime.now().minusMinutes(40) val duration = Duration.between(time1, time2) println(duration) —— PT-39M-59.999147S Duration private final long seconds; private final int nanos;

Slide 52

Slide 52 text

Time Zones ZoneId • Encapsulates a time zone identi fi ed by its unique id: « Europe/Paris » • Contains rules to transform Instant to LocalDateTime

Slide 53

Slide 53 text

Time Zones ZoneId + ZoneRules • There are three types of IDs: • simple: corresponds to the one encapsulated in a ZoneOffset. Its representation begins with the letter Z then the sign + or - and the offset value • offset-style: starts with a pre fi x (UTC, GMT or UT) then the + or - sign and the offset value • region-based: starts with at least two characters different from the offset-style pre fi xes and the characters + and -,

Slide 54

Slide 54 text

val zoneId: ZoneId = ZoneId.of("Europe/Paris") val rules = zoneId.getRules() println(rules.getOffset(LocalDateTime.of(2023, Month.MARCH,26, 0, 0))) println(rules.getOffset(LocalDateTime.of(2023, Month.MARCH, 27, 0, 0))) +01:00 +02:00

Slide 55

Slide 55 text

ZonedDateTime vs OffsetDateTime • ZonedDateTime : • is based on the time zone identi fi er, such as « America/New_York » • is used when you need to represent date and time information in different time zones • OffsetDateTime: • is based on the offset from UTC, such as « +03:00 ». • is used when you need to represent date and time information with a known UTC offset.

Slide 56

Slide 56 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 57

Slide 57 text

History of Time, in a Java world… 1996 Java 1.0 java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime

Slide 58

Slide 58 text

kotlinx-datetime

Slide 59

Slide 59 text

kotlinx-datetime Bene fi ts public fun DateTimePeriod( years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, nanoseconds: Long = 0 ): DateTimePeriod = buildDateTimePeriod(totalMonths(years, months), days, totalNanoseconds(hours, minutes, seconds, nanoseconds)) • It's in the alpha stage, and the development continues. • Multiplatform support • @Serializable • Simpler API

Slide 60

Slide 60 text

kotlinx-datetime Bene fi ts • It's in the alpha stage, and the development continues. • Multiplatform support • @Serializable • Simpler API // Support ISO-8601 format "2010-06-01T22:19:44.475Z".toInstant() "2010-06-01T22:19:44".toLocalDateTime() "2010-06-01".toLocalDate() "12:01:03".toLocalTime() "12:0:03.999".toLocalTime()

Slide 61

Slide 61 text

How about Today?

Slide 62

Slide 62 text

Project Mainline Modular System Components IANA releases an update to the 
 Time Zone Database Google or the Android partner prepares a Time Zone Data module update (APEX fi le) End-user device downloads the update, reboots, then applies the changes

Slide 63

Slide 63 text

How about today? JSR-310 (API) TimeZone Management Multiplatform java.util joda-time-android ~ ThreeTenABP X ~ java.time X X* kotlinx.datetime X* X* X

Slide 64

Slide 64 text

Thank you! www.renaudmathieu.com