In general, I think that processing dates in programming is a rather tedious process, but I will tell you that it will be more troublesome when dealing with old dates. The sample code is written in Java, but I think similar problems will occur in other languages.
LocalDateTime.of(1887, 1, 1, 0, 0).atZone(ZoneId.of("Asia/Tokyo"));
// 1887-01-01T00:00+09:18:59[Asia/Tokyo]
The time zone offset in Japan Standard Time is basically "+09: 00", but if you specify 1887, the time zone offset is "+09: 18: 59". Since seconds are also included and it is different from the general format, for example, if this value is generated on the server and returned to the client, problems such as failure when parsing with JavaScript code on the front end side may occur. (happened)
That's because it's actually the case with tz databases. As the name implies, tz database is a database of time zone information. In Java, it is bundled with JRE and is referenced from the library that handles date and time in Java.
The current Japan Standard Time started on January 1, 1888, and it was different before that, so that is reflected in the tz database.
This is an example where the result changes between the old API and the new API after Java 8.
First from the old API.
var cal = Calendar.getInstance();
cal.clear();
cal.set(1582, Calendar.OCTOBER, 4);
var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(cal.getTime()); // "1582-10-04 00:00:00"
I specified 1582/10/4 and it was output as it is. So what about the new API?
var cal = Calendar.getInstance();
cal.clear();
cal.set(1582, Calendar.OCTOBER, 4);
LocalDateTime.ofInstant(cal.toInstant(), ZoneId.of("Asia/Tokyo"));
// 1582-10-14T00:18:59
It was off by 10 days.
This is because different libraries are treated differently before October 15, 1582. October 15, 1582 is a special date, the day when the current Gregorian calendar was introduced. Prior to that, the Julian calendar was used. Therefore, it is divided into a library that treats the day before October 15, 1582 as the Julian calendar as it is, and a library that treats it as a pseudo Gregorian calendar (proleptic Gregorian calendar). In Java, the old API is the former and the new one. The result has changed because the API is different from the latter.
In addition, the transition from the Julian calendar to the Gregorian calendar was carried out with the day following October 4, 1582 in the Julian calendar as October 15, 1582 in the Gregorian calendar. In other words, it actually flew for 10 days, so the difference came out as it was as a result of processing the date library.
LocalDateTime.of(1950, 8, 1, 0, 0).atZone(ZoneId.of("Asia/Tokyo"));
// 1950-08-01T00:00+10:00[Asia/Tokyo]
The time zone offset in Japan Standard Time is basically "+09: 00". (Second time) However, this result is "+10:00".
Same as Case 1, but because the tz database actually does. As for current affairs, there was a lot of noise about introducing daylight saving time for the Tokyo Olympics a while ago. Many of you may know by looking at the news at that time, but summer time was actually introduced in Japan from 1949 to 1951. That is faithfully reflected in the tz database.
--In Japan Standard Time, if the date is old, there is a possibility that the date may shift or the time zone offset may change. --Timezone offset is "+09: 18: 59" for dates prior to 1888 --Processing results for dates before October 15, 1582 vary by library --Since daylight saving time was introduced from 1949 to 1951, the time zone offset becomes "+10:00". ――It is troublesome to consider these, so you should avoid dealing with them if possible. --For Web systems, you can't enter dates older than a certain amount. --If you really need to handle it, check the specifications of the date processing library carefully. --Julian calendar or proleptic Gregorian calendar? --Especially Java is different between the old API and the new API, so be careful
http://nowokay.hatenablog.com/entries/2015/01/09 https://ja.wikipedia.org/wiki/Tz_database http://www.mwsoft.jp/programming/other/time_mendoi.html https://qiita.com/sota2502/items/e8df68d9cfebd01af809
Recommended Posts