Dates and Times
2015-07-01T12:40:00-07:00
Solar time
Solar time
● We’ve always equated the passage of time
with the sun’s position in the sky.
● This is called solar time.
● Time for the sun to return to its previous
position in the sky is called a solar day.
Mean solar day
Mean solar day
● Length of a solar day varies throughout the
year.
● A clock that ticks a constant number of times
per day cannot keep solar time.
● So we compute the mean solar day.
GMT
GMT
● Long ago, Britain had a kick-ass navy.
● Many nautical maps referred to Greenwich,
London as their Prime Meridian.
● All clocks synchronized against its mean
solar time.
Tidal effects
Tidal effects
● Tides redistribute a big chunk of the Earth’s
mass.
● Decreases its rotational speed.
● Length of the mean solar day is increasing.
● But the second was defined as 1/86,400 of a
mean solar day...
UT1
UT1
● Universal Time 1, succeeds GMT.
● Measured from stable celestial points of
reference.
SI seconds
SI seconds
● SI second is “the duration of 9,192,631,770
periods of the radiation corresponding to the
transition between the two hyperfine levels
of the ground state of the caesium 133
atom.”
● Atomic clocks won’t gain or lose such a
second over millions of years.
TAI
● International Atomic Time.
● Averages over 400 atomic clocks worldwide.
● Defines the rate at which all clocks should
tick.
● Was 10 seconds ahead of UT1 in 1958.
● 86,400 SI seconds is consistently less than
mean solar day, so now 36 seconds ahead.
TAI
TAI
UT1
UTC and leap seconds
● We want clocks to reflect solar time, or UT1.
● We want clocks to tick SI seconds, like TAI.
● UTC is an integer offset from TAI that
approximates UT1.
○ English: coordinated universal time (CUT)
○ French: temps universel coordonné (TUC)
● Leap seconds pump the brakes on TAI.
UTC and leap seconds
UTC
UT1
inserted leap second
UTC and leap seconds
Local time
● UTC is great, but it approximates the mean
solar time in Greenwich, London.
● Region defines all of its clocks as running
ahead or behind UTC by an offset.
● Clocks adopt a local time within a region,
and the region itself defines a time zone.
Local time
Daylight saving time
● Some regions have more daylight in the
summer than in the winter.
● Shifts the majority of the extra sunlight to
later in the day.
● Region switches away from standard time.
○ e.g. PST (UTC-8) to PDT (UTC-7)
○ In U.S., happens at 2 a.m. in each time zone.
Daylight saving time
Daylight saving time
"I have seen reports that show changing the
time twice a year has an adverse effect on
people's health. When the time changes,
people experience more mishaps, accidents
and even more heart attacks. Sometimes, no
change is good."
- Senator Cliff Pirtle R-Roswell
tz database
● Defines regions where all clocks have
shared the same time since Jan 1, 1970.
● Has history of time zones with UTC offsets
and daylight saving rules for each region.
tz database
tz database
$ ls -l /etc/localtime
... /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles
tz database
los_angeles_tz = timezone('America/Los_Angeles')
tz_fmt = '%H:%M %Z%z on %B %d, %Y'
utc_dt = datetime(2015, 1, 10, 16, 30, tzinfo=pytz.utc)
los_angeles_dt = utc_dt.astimezone(los_angeles_tz)
print los_angeles_dt.strftime(tz_fmt)
# 08:30 PST-0800 on January 10, 2015
utc_dt = datetime(2015, 6, 10, 16, 30, tzinfo=pytz.utc)
los_angeles_dt = utc_dt.astimezone(los_angeles_tz)
print los_angeles_dt.strftime(tz_fmt)
# 09:30 PDT-0700 on June 10, 2015
tz database
ISO 8601
los_angeles_tz = timezone('America/Los_Angeles')
kathmandu_tz = timezone('Asia/Kathmandu')
tokyo_tz = timezone('Asia/Tokyo')
now = datetime.now(los_angeles_tz)
print 'America/Los_Angeles: %s' % now.isoformat()
# America/Los_Angeles: 2015-06-30T23:33:11.498959-07:00
print 'UTC: %s' % now.astimezone(pytz.utc).isoformat()
# UTC: 2015-07-01T06:33:11.498959+00:00
print 'Asia/Kathmandu: %s' % now.astimezone(kathmandu_tz).isoformat()
# Asia/Kathmandu: 2015-07-01T12:18:11.498959+05:45
print 'Asia/Tokyo: %s' % now.astimezone(tokyo_tz).isoformat()
# Asia/Tokyo: 2015-07-01T15:33:11.498959+09:00
Unix time
● Number of seconds since January 1, 1970 at
00:00:00 UTC.
● This is called the Unix epoch.
Unix time
$ date +%s
1435733200
Unix time
Language Method name Units Data type
C time seconds integer
C gettimeofday microseconds struct
Java currentTimeMillis milliseconds 64-bit long
Python time seconds floating point
Objective-C timeIntervalSince1970 seconds floating point
Unix time
● Unlike UTC, Unix time does not include leap
seconds.
● Midnight between days is always 86,400
seconds.
● Unix time is discontinuous around leap
seconds.
● It is a lossy encoding of UTC.
Obsered unix time
UTC Unix time (internal) Unix time (observed)
2012-06-30T23:59:59.75 1341100799.75 1341100799.75
2012-06-30T23:59:60.00 1341100800.00 1341100800.00
2012-06-30T23:59:60.25 1341100799.25 1341100800.01
2012-06-30T23:59:60.50 1341100799.50 1341100800.02
2012-06-30T23:59:60.75 1341100799.75 1341100800.03
2012-07-01T00:00:00.00 1341100800.00 1341100800.04
2012-07-01T00:00:00.25 1341100800.25 1341100800.25
Observed unix time
● Not POSIX compliant.
● Same POSIX standard that wrongfully
considered the year 2100 a leap year…
● Google modified NTP to perform a “leap
smear” which is over the preceding day.
NTP
t0
t3
t1
t2
● Time to propagate the request and
response is (t3
– t0
) – (t2
– t1
)
● Assuming latency in both directions
is equal, correct time at t3
is
t2
+ [(t3
– t0
) – (t2
– t1
)] / 2
● So t3
+ δ = t2
+ [(t3
– t0
) – (t2
– t1
)] /
2
● δ = [(t1
– t0
) + (t2
– t3
)] / 2
client server

Dates and Times

  • 1.
  • 2.
  • 3.
    Solar time ● We’vealways equated the passage of time with the sun’s position in the sky. ● This is called solar time. ● Time for the sun to return to its previous position in the sky is called a solar day.
  • 4.
  • 5.
    Mean solar day ●Length of a solar day varies throughout the year. ● A clock that ticks a constant number of times per day cannot keep solar time. ● So we compute the mean solar day.
  • 6.
  • 7.
    GMT ● Long ago,Britain had a kick-ass navy. ● Many nautical maps referred to Greenwich, London as their Prime Meridian. ● All clocks synchronized against its mean solar time.
  • 8.
  • 9.
    Tidal effects ● Tidesredistribute a big chunk of the Earth’s mass. ● Decreases its rotational speed. ● Length of the mean solar day is increasing. ● But the second was defined as 1/86,400 of a mean solar day...
  • 10.
  • 11.
    UT1 ● Universal Time1, succeeds GMT. ● Measured from stable celestial points of reference.
  • 12.
  • 13.
    SI seconds ● SIsecond is “the duration of 9,192,631,770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium 133 atom.” ● Atomic clocks won’t gain or lose such a second over millions of years.
  • 14.
    TAI ● International AtomicTime. ● Averages over 400 atomic clocks worldwide. ● Defines the rate at which all clocks should tick. ● Was 10 seconds ahead of UT1 in 1958. ● 86,400 SI seconds is consistently less than mean solar day, so now 36 seconds ahead.
  • 15.
  • 16.
    UTC and leapseconds ● We want clocks to reflect solar time, or UT1. ● We want clocks to tick SI seconds, like TAI. ● UTC is an integer offset from TAI that approximates UT1. ○ English: coordinated universal time (CUT) ○ French: temps universel coordonné (TUC) ● Leap seconds pump the brakes on TAI.
  • 17.
    UTC and leapseconds UTC UT1 inserted leap second
  • 18.
    UTC and leapseconds
  • 19.
    Local time ● UTCis great, but it approximates the mean solar time in Greenwich, London. ● Region defines all of its clocks as running ahead or behind UTC by an offset. ● Clocks adopt a local time within a region, and the region itself defines a time zone.
  • 20.
  • 21.
    Daylight saving time ●Some regions have more daylight in the summer than in the winter. ● Shifts the majority of the extra sunlight to later in the day. ● Region switches away from standard time. ○ e.g. PST (UTC-8) to PDT (UTC-7) ○ In U.S., happens at 2 a.m. in each time zone.
  • 22.
  • 23.
    Daylight saving time "Ihave seen reports that show changing the time twice a year has an adverse effect on people's health. When the time changes, people experience more mishaps, accidents and even more heart attacks. Sometimes, no change is good." - Senator Cliff Pirtle R-Roswell
  • 24.
    tz database ● Definesregions where all clocks have shared the same time since Jan 1, 1970. ● Has history of time zones with UTC offsets and daylight saving rules for each region.
  • 25.
  • 26.
    tz database $ ls-l /etc/localtime ... /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles
  • 27.
    tz database los_angeles_tz =timezone('America/Los_Angeles') tz_fmt = '%H:%M %Z%z on %B %d, %Y' utc_dt = datetime(2015, 1, 10, 16, 30, tzinfo=pytz.utc) los_angeles_dt = utc_dt.astimezone(los_angeles_tz) print los_angeles_dt.strftime(tz_fmt) # 08:30 PST-0800 on January 10, 2015 utc_dt = datetime(2015, 6, 10, 16, 30, tzinfo=pytz.utc) los_angeles_dt = utc_dt.astimezone(los_angeles_tz) print los_angeles_dt.strftime(tz_fmt) # 09:30 PDT-0700 on June 10, 2015
  • 28.
  • 29.
    ISO 8601 los_angeles_tz =timezone('America/Los_Angeles') kathmandu_tz = timezone('Asia/Kathmandu') tokyo_tz = timezone('Asia/Tokyo') now = datetime.now(los_angeles_tz) print 'America/Los_Angeles: %s' % now.isoformat() # America/Los_Angeles: 2015-06-30T23:33:11.498959-07:00 print 'UTC: %s' % now.astimezone(pytz.utc).isoformat() # UTC: 2015-07-01T06:33:11.498959+00:00 print 'Asia/Kathmandu: %s' % now.astimezone(kathmandu_tz).isoformat() # Asia/Kathmandu: 2015-07-01T12:18:11.498959+05:45 print 'Asia/Tokyo: %s' % now.astimezone(tokyo_tz).isoformat() # Asia/Tokyo: 2015-07-01T15:33:11.498959+09:00
  • 30.
    Unix time ● Numberof seconds since January 1, 1970 at 00:00:00 UTC. ● This is called the Unix epoch.
  • 31.
    Unix time $ date+%s 1435733200
  • 32.
    Unix time Language Methodname Units Data type C time seconds integer C gettimeofday microseconds struct Java currentTimeMillis milliseconds 64-bit long Python time seconds floating point Objective-C timeIntervalSince1970 seconds floating point
  • 33.
    Unix time ● UnlikeUTC, Unix time does not include leap seconds. ● Midnight between days is always 86,400 seconds. ● Unix time is discontinuous around leap seconds. ● It is a lossy encoding of UTC.
  • 34.
    Obsered unix time UTCUnix time (internal) Unix time (observed) 2012-06-30T23:59:59.75 1341100799.75 1341100799.75 2012-06-30T23:59:60.00 1341100800.00 1341100800.00 2012-06-30T23:59:60.25 1341100799.25 1341100800.01 2012-06-30T23:59:60.50 1341100799.50 1341100800.02 2012-06-30T23:59:60.75 1341100799.75 1341100800.03 2012-07-01T00:00:00.00 1341100800.00 1341100800.04 2012-07-01T00:00:00.25 1341100800.25 1341100800.25
  • 35.
    Observed unix time ●Not POSIX compliant. ● Same POSIX standard that wrongfully considered the year 2100 a leap year… ● Google modified NTP to perform a “leap smear” which is over the preceding day.
  • 36.
    NTP t0 t3 t1 t2 ● Time topropagate the request and response is (t3 – t0 ) – (t2 – t1 ) ● Assuming latency in both directions is equal, correct time at t3 is t2 + [(t3 – t0 ) – (t2 – t1 )] / 2 ● So t3 + δ = t2 + [(t3 – t0 ) – (t2 – t1 )] / 2 ● δ = [(t1 – t0 ) + (t2 – t3 )] / 2 client server