{"id":141,"date":"2014-12-11T19:14:46","date_gmt":"2014-12-11T19:14:46","guid":{"rendered":"http:\/\/www.baruchyoussin.com\/?p=141"},"modified":"2015-02-08T18:21:22","modified_gmt":"2015-02-08T18:21:22","slug":"time-zone-design-in-java-olson-time-zone-database","status":"publish","type":"post","link":"https:\/\/baruchyoussin.com\/en\/time-zone-design-in-java-olson-time-zone-database.html","title":{"rendered":"Time zone design in Java | Olson time zone database in Java"},"content":{"rendered":"<br \/>\n<h2><span id=\"background_olson_time_zone_database\">Background: Olson time zone database<\/span><\/h2>\n<p>This database maintains the time zone information including the \t\tperiods when Daylight Savings Time (DST) is in effect, for all \t\tlocations around the world.<\/p>\n<p> \t\tFor its description and history, see the <a \t\t\thref=\"http:\/\/en.wikipedia.org\/wiki\/Tz_database\"> Wikipedia \t\t\tarticle<\/a>. \t<\/p>\n<p>What is important for us, is that this database is quite \t\tdynamic, with changes being introduced every few months if not weeks, \t\tas local authorities all over the world change the dates of their \t\tdaylight savings time from year to year.<\/p>\n<p> \t\tFor example, in the United States, some <em>counties<\/em> have their \t\town rules for Daylight Savings Time, and this contribute to the number \t\tof entries in this database. In fact, it currently has 408 entries \t\taccording to the <a \t\t\thref=\"http:\/\/en.wikipedia.org\/wiki\/List_of_tz_database_time_zones\">special \t\t\tWikipedia article dedicated to this list<\/a>; bear in mind that the \t\tcontents of each entry \u2013 the dates its DST is in effect \u2013 may change \t\tfrom time to time. \t<\/p>\n<p> \t\tIn my country, Israel, the issue of Daylight Savings Time is highly \t\tpolitical and is decided by parliament debate; for some parties \t\tchanges in Daylight Savings Time are part of their political program \t\tand they write these changes in coalition agreements; see more details \t\t<a \t\t\thref=\"daylight-savings-time-in-israel-politics-reasons-interests.html\"> \t\t\there<\/a>. \t<\/p>\n<h2><span id=\"a_brief_survey_of_java_time_zone_design\">A brief survey of Java time zone design<\/span><\/h2>\n<p>The design of Java time zone functionality is as follows.<\/p>\n<p> \t\tJava maintains its own time zone database, see <a \t\t\thref=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/tsgdesktop\/time-zone.html\"> \t\t\tOracle documentation<\/a>. This database is maintained to match the Olson \t\ttime zone database through updates to JRE that are regularly issued. \t<\/p>\n<p>In addition, Java allows custom time zones of the form \t\tGMT+&lt;offset&gt; (e.g., GMT+3) which do not bear any daylight \t\tsavings time information.<\/p>\n<p>Whenever Java needs to find out the current time and time zone, \t\tit turns to the operating system and tries to identify the time zone \t\twhich is currently set on the computer, with a time zone in its \t\tdatabase.<\/p>\n<h2><span id=\"on_the_first_glance_why_this_design_seems_to_be_wrong\">On the first glance: why this design seems to be wrong<\/span><\/h2>\n<p> \t\tWhen I first bumped into the <a \t\t\thref=\"java-incorrect-time-zone-bug-windows.html\">Java incorrect \t\t\ttime zone bug on Windows<\/a>, I thought that there was a problem with the \t\tJava design, as follows. \t<\/p>\n<p>Java Runtime (JRE) maintains its own time zone database and the \t\toperating system (OS) maintains its own database.<\/p>\n<p>Both databases are periodically updated against the standard \t\tOlson database but these updates are not synchronized.<\/p>\n<p>Thus, whenever Olson database is changed, there is some period \t\twhen one of the two databases, JRE and OS, is already updated, while \t\tthe other one is not. During this period, the two databases mismatch.<\/p>\n<p>In many cases user needs his program to correctly understand the \t\ttime changes according to his operating system, not according to some \t\tabstract database maintained by Java. In such cases user needs access \t\tto the OS database rather than the one maintained by Java.<\/p>\n<p>However, Java does not give such an option.<\/p>\n<p>Rather, it despises it completely: all it does, it tries to \t\tascertain the name of the current time zone from the OS data, and use \t\tits own database. When it fails, user gets a completely different time \t\tzone.<\/p>\n<p>According to this line of reasoning, in addition to the time \t\tzones in the database maintained by Java, user should be able to use \t\tthe time zones in the OS time zone database, or at least, the current \t\ttime zone of the operating system, together with its DST information.<\/p>\n<p>To summarize, the problems with the Java time zone design are as \t\tfollows:<\/p>\n<ul>\n<li>Java does not give access to time zones maintained by the \t\t\toperating system.<\/li>\n<li>There is a possibility of mismatch between the time zone \t\t\tdatabases maintained by Java and by the operating system, and it is \t\t\tnot clear how applications can deal with it.<\/li>\n<\/ul>\n<h2><span id=\"on_the_second_glance_why_the_current_java_design_is_correct\">On the second glance: why the current Java design is correct<\/span><\/h2>\n<p>We address the above problems one by one.<\/p>\n<h3><span id=\"time_zones_maintained_by_windows_better_not_give_access_to_them\">Time zones maintained by Windows: better not give access to \t\tthem<\/span><\/h3>\n<p> \t\tThe information on the current time zone on Windows may be unreliable, \t\tboth the current time zone offset and the dates when DST is in the \t\teffect; see our discussion <a \t\t\thref=\"java-incorrect-time-zone-bug-windows.html#CurrentTimeZoneOnWindows\"> \t\t\there<\/a>. If we let user use this information, it may turn out to be \t\tcorrupt. \t<\/p>\n<p>This means that we cannot rely on the current time zone \t\tinformation provided by the OS, and need to allow for the possibility \t\tthat this information is incorrect or corrupt.<\/p>\n<p> \t\t<a href=\"java-incorrect-time-zone-bug-windows.html#what-to-do\"> \t\t\tHere<\/a> we suggest that in such case the best solution is to allow user \t\tto indicate manually the correct time zone in the Java database. \t<\/p>\n<p>With that, there is no point in taking trouble to extract the \t\tDST information from the operating system.<\/p>\n<p>In other words, there is no point in taking trouble to give \t\taccess to the time zones maintained by the OS since (1) we cannot \t\talways determine the current one correctly and (2) the DST information \t\tmay be corrupt.<\/p>\n<p>Thus, it is better to use our own database and use the operating \t\tsystem only to identify the current time zone in our database. This is \t\tthe decision taken by Java.<\/p>\n<h3><span id=\"the_mismatch_is_not_a_problem\">The mismatch is not a problem<\/span><\/h3>\n<p>Our concern is with the mismatch between the two time zone \t\tdatabases, the one maintained by Java and the one maintained by the \t\toperating system; a Java application needs to be able to deal with it.<\/p>\n<p>Any developer that needs to deal with future events in different \t\ttime zones, needs to take into account the following two \t\tpossibilities:<\/p>\n<ul>\n<li>The Daylight Savings Time information may change, and this \t\t\tchange may happen before the event in question takes place.\n<ul style=\"list-style-type: none\">\n<li>Thus, if we determine now that a future event takes place \t\t\t\t\tduring DST, we need to prepare for the possibility that the DST \t\t\t\t\trules may change in the meantime and the event will take place \t\t\t\t\tduring the Standard time rather than DST.<\/li>\n<\/ul>\n<\/li>\n<li>In addition, the local time zone database (whether JRE, or \t\t\tOS, or both) may fail to be updated correctly by the event time.\n<ul style=\"list-style-type: none\">\n<li>In such case, the computer or a Java application\/applet \t\t\t\t\twill show the local time incorrectly, and the user will have to \t\t\t\t\tchange the time zone manually to fix the local time set in the \t\t\t\t\tapplication or on the computer.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The possibility of the mismatch between the DST information in \t\tthe JRE time zone and in the OS time zone seems to be trivial in \t\tcomparison with the these two possibilities; if they are taken care \t\tof, the mismatch between the databases seems to be easy to deal with.<\/p>\n<p> \t\t<small>&copy;Baruch Youssin 2014<\/small> \t<\/p>\n<p> \t\t<strong>Read also: <\/strong> \t<\/p>\n<ul style=\"list-style-type: none\">\n<li><a \t\t\thref=\"daylight-savings-time-in-israel-politics-reasons-interests.html\">Daylight \t\t\t\tSavings Time in Israel: politics, reasons, interests<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Background: Olson time zone database This database maintains the time zone information including the periods when Daylight Savings Time (DST) is in effect, for all locations around the world. For its description and history, see the Wikipedia article. What is important for us, is that this database is quite dynamic, with changes being introduced every [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":3,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/posts\/141\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baruchyoussin.com\/en\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}