It's a pain to deal with old dates

Overview

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.

Operating environment

Text

Case 1: Time zone offset becomes "+09: 18: 59"

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)

Cause

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.

Case 2: Date skips for 10 days

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.

Cause

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.

Case 3: Timezone offset is off by 1 hour

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".

Cause

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.

Summary

--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

Reference link

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

It's a pain to deal with old dates
A story of connecting to a CentOS 8 server with an old Ansible
[Android] How to deal with dark themes
It's good to go to a programming school.
If you want to make a zip file with Ruby, it's rubyzip.
How to deal with Precompiling assets failed.
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
[Rails] rails new to create a database with PostgreSQL
Convert a string to a character-by-character array with swift
Transition to a view controller with Swift WebKit
Rip a CD to MP3 with Ubuntu 18.04 LTS
I tried to break a block with java (1)
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
How to deal with errors in Rails s could not find a JavaScript runtime.
Minecraft BE Tap with a wooden sword to jump
How to make a factory with a model with polymorphic association
Submit a job to AWS Batch with Java (Eclipse)
Diet program with preprocessor (how to deal with i-appli size)
How to deal with fatal: remote origin already exists.
Considering a property editor to use with SpringToolSuite (STS)
How to delete a new_record object built with Rails
How to deal with No template for interactive request
How to manually generate a JWT with Rails Knock
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
I want to monitor a specific file with WatchService
A story about trying to get along with Mockito
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
Refusing to install package with name "webpack" under a package
A story about reducing memory consumption to 1/100 with find_in_batches
Common problems with WSL and how to deal with them
How to get started with creating a Rails app
[Docker + Rails] How to deal with Rails server startup failure
[Java] How to start a new line with StringBuilder
It's not a big deal if you understand that I was addicted to receiving emails with Java Mail from Exchange Online