Time and date for programmers

Time and date for programmers

Michał Pipa

WeBB, Bielsko-Biała, 2017-05-11

About me:

Date format

ISO 8601 was published on 06/05/88 and most recently amended on 12/01/04.

ISO 8601

2017-02-24T23:04:17Z

2017-02-24T23:04:17+00:00

RFC 3339

1985-04-12T23:20:50.52Z

1996-12-19T16:39:57-08:00

RFC 2616

Sun, 06 Nov 1994 08:49:37 GMT

Unix timestamp

number of seconds since midnight, 1 January 1970 (UTC)

Unix timestamp

1488024000

Avoid date formats not specifying time zone

2016-01-10 13:14:15

Programmers are not always aware of ISO 8601. Use it everywhere, UI can be an exception of this rule.

ISO week number

The ISO 8601 definition for week 01 is the week with the Gregorian year's first Thursday in it.

Leap years

Criteria to identify leap year:

  • year can be evenly divided by 4
  • if year can be evenly divided by 100, it is not a leap year
  • unless year can be evenly divided by 400

Leap years

  • 1896
  • 1904
  • ...
  • 1996
  • 2000
  • 2004

next year = year + 365 days

next year = year + 1 year

Easter

Computus

The calculation used to determine the calendar date of Easter

Check if your programming language has function for Easter date

Leap seconds

Universal Time (UT1)

Based on Earth's rotation

Coordinated Universal Time (UTC)

Based on Atomic time

Leap second

  • Earth slows down it's rotation
  • extra second can be added or removed from UTC
  • occurs on midnight UTC
  • usually last day of June or December

Leap second insertion

  • 23:59:58
  • 23:59:59
  • 23:59:60
  • 00:00:00

Leap second removal

  • 23:59:57
  • 23:59:58
  • 00:00:00

Unix timestamp ignores leap seconds

Leap second insertion

  • 1483228798
  • 1483228799
  • 1483228799
  • 1483228800

How to handle leap second? (Linux)

  • kernel backward step
  • daemon backward step
  • ignore leap second and correct by slewing
  • client slew
  • server slew

Time zones

Time zone naming

  • CET
  • Europe/Warsaw

Time zone offset is not always full hour

Asia/Kolkata +05:30

Asia/Kathmandu +05:45

Daylight saving time (DST)

Time difference between Warsaw and Sydney

Date: 2017-02-25

Warsaw: UTC + 1

Sydney: UTC + 11

Difference: 10 hours

Date: 2017-03-27

Warsaw: UTC + 2

Sydney: UTC + 11

Difference: 9 hours

Date: 2017-04-03

Warsaw: UTC + 2

Sydney: UTC + 10

Difference: 8 hours

Date: 2017-10-02

Warsaw: UTC + 2

Sydney: UTC + 11

Difference: 9 hours

Date: 2017-10-30

Warsaw: UTC + 1

Sydney: UTC + 11

Difference: 10 hours

DST offset is not always full hour

Australia/Lord_Howe +10:30

Australia/Lord_Howe +11:00 (DST)

Africa/Casablanca suspends DST for about a month (Ramadan)

DST in 2016 for Africa/Casablanca

from 2016-03-27 to 2016-06-05

from 2016-07-10 to 2016-10-30

Time and Date

How to measure elapsed time?


start = now()
f()
end = start - now()
                    

If you want to measure elapsed time use monotonic clock

If your code depends on time make it an input parameter

Instead of this:


function f()
{
    $now = time();
    // ...
}
                    

Do this:


function f($time)
{
    $now = $time;
    // ...
}
                    

In your application do not call system clock


interface Clock
{
    public function now(): DateTimeImmutable;
}
                    

class SystemClock
{
    public function now(): DateTimeImmutable
    {
        return new DateTimeImmutable();
    }
}
                    

Thank you!