Droidcon UK 2018
The correct handling of timezones and locales is one of the most under-appreciated parts of software development. Commonly known as internationalisation (i18n), a lot of people underestimate the impact that getting it wrong can have for your users as well as your systems.
Drawn from experiences with working on a global network of backend systems, websites and mobile apps in more than 30 locales for the last 10 years, this talk will start with an introduction to the concepts behind time zones and locales.
You’re going to learn about the history of time measurement and time synchronisation and how the world eventually ended up with the global system of time zones of today. Today’s model is full of interesting and sometimes outright bizarre quirks and you’ll look at some of best and worst of them. From there you’ll learn about the ideas behind locales and why cultural context is at least as important as a locale’s common collection of purely technical data such a number formats or text direction. After this, the talk will cover how common runtime environments like Android and Java represent these ideas.
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Why a whole country skipped a day - Fun with Timezones
1. WHY A WHOLE COUNTRY SKIPPED A DAY
FUN WITH TIMEZONES
KAI KOENIG (@AGENTK)
2. HELLO
HELLO
My name is Kai.
Software Architect in the Web and Android space.
Stuff I enjoy: Coding in Android, Kotlin, Java & CFML, compilers and parsers,
aviation and flying small aircraft, cats and chickens, Nintendo video games of all
ages.
Twitter: @AgentK
9. EXAMPLES
▸ Spoken language, written language & scripts, reading direction, translations
▸ Decimal formats, currency symbols
▸ Meaning of colours, symbols, artworks in a cultural context
▸ Time formats & notation, time zones, boundaries, DST
SOME CONTEXT
10. TERMINOLOGY
▸ i18n: internationalisation
▸ Designing software in a way so it can be adapted to various languages
▸ l10n: localisation
▸ Adapting software for a certain region, time or language
SOME CONTEXT
11. SOME CONTEXT
LOCALES
▸ A locale is a set of parameters that defines the user's language, country and
any special variant preferences that the user wants to see in their user interface.
▸ A locale identifier consists of at least a language identifier and (usually) a
region identifier:
▸ de_DE vs. de_CH
▸ en_AU vs. en_NZ
▸ mi_NZ
12. SOME CONTEXT
TWO WORLDS
▸ Up to and including SDK 23:
▸ Limited exposure of ICU functionality in Android SDK
▸ ICU version-equivalent depends on SDK and in some rare cases on OEM.
▸ Suggestion: import ICU4J manually into your project
▸ From SDK 24 onwards:
▸ Subset of ICU4J is included in SDK, less need for additional library for better
i18n support.
18. MORE HISTORY
▸ Greenwich Mean Time (GMT)
▸ Started in 1675 as a local solar time
▸ Used by railway companies in the UK from ~1840 with portable
chronometers
TIME
19. EVEN MORE HISTORY
▸ November 1868:
▸ New Zealand Mean Time was introduced (GMT+11:30)
▸ Between 1860s-1890s:
▸ USA introduced the railroad time with 4 major time zone
TIME
20. TIME ON A GLOBAL LEVEL
▸ The Italian mathematician Filopanti suggested a 24hr time zone system
centred in Rome (1858).
▸ By ~1900 most of the Earth had standard time zones, not necessarily in
brackets of one hour.
▸ Today: Full hour, 1/2 hour and 1/4 hour time zones
▸ Also: politics play a big part
TIME
22. GMT VS UTC
▸ GMT: Mean solar time at the meridian passing through the Royal Observatory
in Greenwich
▸ UTC: Atomic time that includes leap seconds, guaranteed to always be within
0.9 seconds of the original GMT
▸ UTC is also known as Zulu Time
▸ Fun fact: Earth’s rotation is slowing - we’ll need more leap seconds and more
often
TIME
24. INTERNATIONAL DATE LINE
▸ An imaginary “line” in the Pacific Ocean.
▸ Crossing the IDL:
▸ East to West (North America -> Asia/Oceania): add a day
▸ West to East (Asia/Oceania -> North America): subtract a day
▸ Established in 1884 based on the 180 degrees Meridian
TIME
25. THE NEED FOR A TEMPORAL DISCONTINUITY ON
THE GLOBE CAN BE DESCRIBED MATHEMATICALLY
AS FOLLOWING FROM THE BORSUK-ULAM
THEOREM IN DIMENSION 1
TIME
26. “IT IS A TOPOLOGICAL FACT THAT THERE DOES
NOT EXIST ANY CONTINUOUS, ONE-TO-ONE
FUNCTION MAPPING FROM A CIRCLE ONTO AN
INTERVAL.”
TIME
27.
28. TIME FORMATS
▸ 09:30 UTC would usually be expressed as 09:30Z or 0930Z
▸ 14:45:15 UTC -> 14:45:15Z or 144515Z
▸ Offsets:
▸ +/-03:00 or +/-0300 or +/-03
▸ Terminology such as MEST, NZDT, BST etc. are made up and not standardised.
TIME
30. TIME FORMATS
TIME
B3819/18 NOTAMR B3791/18
Q) NZZC/QLXXX/IV/NBO/A /000/999/4120S17448E005
A) NZWN B) 1806231919 C) 1806241800 EST
E) TWY A1 CL LGT WIP. INSTALLATION OF TEMPO BLUE TWY
EDGE LGT. TWY CL LGT AND BLUE TWY EDGE LGT MAY OPR
SIMULTANEOUSLY AS TWY LGT IS PROGRESSIVELY REPLACED
31. TIME FORMATS
TIME
B3819/18 NOTAMR B3791/18
Q) NZZC/QLXXX/IV/NBO/A /000/999/4120S17448E005
A) NZWN B) 1806231919 C) 1806241800 EST
E) TWY A1 CL LGT WIP. INSTALLATION OF TEMPO BLUE TWY
EDGE LGT. TWY CL LGT AND BLUE TWY EDGE LGT MAY OPR
SIMULTANEOUSLY AS TWY LGT IS PROGRESSIVELY REPLACED
32. DAYLIGHT SAVINGS TIME
▸ DST occurs when a country/region decides to wind their clocks forward one
hour to make most of the available sunlight.
▸ Might have a local name - Germany: “Sommerzeit” and “Winterzeit”
▸ Northern and Southern Hemisphere usually move in opposite directions.
▸ Lord Howe (an island between AU and NZ) has a DST of +0030
▸ Some other areas in Australia ignore DST - mainly West Australia and
Queensland — too many farmers that don’t want their cattle/sheep to be upset.
TIME
33. TIME ZONES IN JVM LAND
▸ TZ Database: IANA Time Zone Database
▸ Set of text files that can be read and used with the right tools. It’s safe to
assume you don’t have those tools and don’t care - most of the time.
▸ A JVM comes with a copy of the TZ Database built-in.
▸ It contains historic and current information on timezones for various locations
at any time of the year.
TIME
34.
35. TIME ZONES IN JVM LAND
▸ It’s important to update your JVM’s timezone database regularly through JVM
updates or through manual patching.
▸ Time legislation changes over the years:
▸ DST start/end dates might be adjusted
▸ Political changes at will
▸ Examples:
▸ JRE 6u10- tzdata2008c
▸ JRE 6u191 - tzdata2018c
TIME
36. TIME AND ANDROID
▸ As users, we can’t update tzdata ourselves in any easy way:
▸ Usually baked into the ORM’s system image
▸ If one is root, there are some whacky ways to “fix” tzdata
▸ Hardware manufacturer updates… 🤔🙄
▸ Big changes in 8.1+ - TimeZone updater solution:
▸ Android team creates timezone rules updates
▸ OEMs can deploy them without full system upgrades
TIME
37. CODE AND APIS TO AVOID
▸ Don’t use the old, built-in Date and Calendar classes:
▸ java.util.Date
▸ SimpleDateFormatter etc.
▸ NOT thread-safe
▸ Weeeeeeeeird API
TIME
38. JODA TIME
▸ Third-party library to fix the problems of the Date/Time APIs pre-Java 8.
▸ Everyone used Joda Time at some point.
▸ Fixes tzdata update issue:
▸ Library releases get updates
▸ Manual patching if needed now
▸ https://www.joda.org/joda-time
TIME
39. JODA PROBLEMS
▸ End of life is on the horizon.
▸ Migration to JSR-310 (java.time) is suggested.
▸ Joda Time’s author was heavily involved with that effort.
▸ Android:
▸ Class loading
▸ Large memory footprint
▸ Joda Time Android: https://github.com/dlew/joda-time-android
TIME
40. THREETEN BP
▸ JSR-310 is only available for Java 8+.
▸ ThreeTenBP is a back port library of JSR-310 APIs to Java 6 and 7.
▸ Gotchas:
▸ Not actively developed, library is in “bug fix” mode.
▸ Not a JSR-310 implementation, merely an API back port.
▸ https://github.com/ThreeTen/threetenbp
▸ https://github.com/JakeWharton/ThreeTenABP
TIME
41. TIME
SOME GOOD PRACTICES
▸ If you can:
▸ Everywhere in your backend, store and manage time in UTC
▸ When passing time around between server and client, use UTC
▸ Presentation layer in clients (Android, iOS, Web etc) takes UTC time and
applies user’s timezone information, offsets for DST etc to it
▸ SDK 24+: Consider using android.icu.util package from ICU4J library
▸ https://developer.android.com/reference/android/icu/util/package-summary
42. TIME
EXCEPTIONS
▸ There are exceptions:
▸ Is it a good idea to store alarm clock values in UTC - or schedulers in
general?
▸ What if the user travels and changes timezone?
▸ What if the user’s timezone information is wrong in the first place?
43. TIME
▸ Australian Central Western Time - UTC
+0845
▸ Self-proclaimed timezone by 5
settlements in the desert.
44. TIME
▸ Time zones cross states
▸ Time zones can even cross counties
▸ Worst offenders:
▸ Kentucky
▸ Indiana
▸ Tennessee
45. TIME
▸ Island of Markets Fyr (Baltic Sea)
▸ Possibly the world’s smallest multi-
timezone location
48. TIME
SAMOA
▸ Samoa skipped 30/12/2011 and moved across the International Date Line.
▸ Second big change for the country since changing from right-side driving to
left-side driving in 2009
▸ Tokelau followed their move
Samoa Mon 9-5 Tue 9-5 Wed 9-5 Thu 9-5 Fri 9-5 Sat 9-5 Sun 9-5
NZ Tue 8-4 Wed 8-4 Thu 8-4 Fri 8-4 Sat 8-4 Sun 8-4 Mon 8-4
AU (Syd) Tue 6-2 Wed 6-2 Thu 6-2 Fri 6-2 Sat 6-2 Sun 6-2 Mon 6-2
Samoa Mon 9-5 Tue 9-5 Wed 9-5 Thu 9-5 Fri 9-5 Sat 9-5 Sun 9-5
NZ Mon 8-4 Tue 8-4 Wed 8-4 Thu 8-4 Fri 8-4 Sat 8-4 Sun 8-4
AU (Syd) Mon 6-2 Tue 6-2 Wed 6-2 Thu 6-2 Fri 6-2 Sat 6-2 Sun 6-2
49.
50. TIME
▸ Kiribati used to split across 2
days
▸ GMT+12
▸ GMT+13
▸ GMT-10
▸ In 1995, the easternmost group
of islands moved across the
International Date Line and
became GMT+14
51. TIME
OTHER ODDITIES
▸ Russia changed all their timezones in 2010 and then (partly) again in 2011 - created
a huge issue for a lot of the users of one of my clients (Russian/Ukrainian-focussed
sites and app).
▸ The International Space Station uses UTC.
▸ Spain should geographically be in the same time zone as the UK. Why aren’t they?
▸ During WW2, Spain’s dictator Franco wanted to align the Spanish time with Nazi-
Germany, their ally.
▸ UK was permanently on GMT+2 during WW2 - even though Greenwich is IN THE UK
54. OTHER THINGS
(NORTHERN HEMISPHERE) AUTUMN DST CHANGES FALLOUT
▸ 30 September 2018: NZ goes into DST (moves 1 hr East)
▸ 7 October 2018: AU (New South Wales) goes into DST (moves 1 hr East)
▸ 28 October 2018: Germany and UK leave DST (move 1 hr West)
- 30/9 30/9 - 7/10 7/10 - 28/10 28/10 -
NZ-AU +2 +3 +2 +2
NZ-Europe +10 +11 +11 +12
AU-Europe +8 +8 +9 +10
55.
56. OTHER THINGS
RESOURCES (I)
▸ i18n support in Android:
https://developer.android.com/guide/topics/resources/internationalization
▸ ICU4J versioning:
https://developer.android.com/guide/topics/resources/internationalization#versioning-nougat
▸ IANA Time Zone Database:
http://en.wikipedia.org/wiki/Tz_database
▸ Oracle TZ technote:
http://www.oracle.com/technetwork/java/javase/timezones-137583.html
▸ TimeZone management in Android:
https://source.android.com/devices/tech/config/timezone-rules
57. OTHER THINGS
RESOURCES (II)
▸ Room + Time (zones):
https://medium.com/@chrisbanes/room-time-2b4cf9672b98
▸ US Navy Astronomical Information:
http://aa.usno.navy.mil/faq/index.php
▸ Joda Time
https://www.joda.org/joda-time
https://github.com/JodaOrg/joda-time
https://github.com/dlew/joda-time-android
▸ ThreeTen
https://github.com/ThreeTen/threetenbp
https://github.com/JakeWharton/ThreeTenABP
58. OTHER THINGS
GET IN TOUCH
Kai Koenig
Email: kai@ventego-creative.co.nz
Work: http://www.ventego-creative.co.nz
Twitter: @AgentK
Telegram: @kaikoenig
Slides: http://www.slideshare.com/agentk