Joda-time DateTime.withTimeAtStartOfDay() wrong result in date (2036,3,21) with Asia/Tehran time zone

Sohbati Source

In Joda-Time version 2.9.9 I want to remove time part of DateTime variable.

Only for time zone Asia/Tehran and some dates like (2036-03-21, 2037-03-21, ...) it returns 1:00:00 in time part of result.

I also checked the Joda-Time source code but I couldn't find any problem.

The code is:

DateTime dt = new DateTime(2036, 03, 21, 10, 0, DateTimeZone.forID(Asia/Tehran));
dt = dt.withTimeAtStartOfday(); 

Actual result:

2036-03-21T01:00:00.000+04:30

Expected result:

2036-03-21T00:00:00.000+04:30

Time is not zero. This only happens for zone Asia/Tehran.

My system config:

  • Java version: 1.7.0_72 - I have to use Java 7
  • Joda-Time: 2.9.9

I solved this problem by converting DateTime to LocalDate, but I want to know why this problem happens?

javadatetimejodatime

Answers

answered 3 months ago Jim Garrison #1

This is because Iran switches from Standard time to Daylight Savings time at 00:00 on the 21st or 22nd of March (whichever day contains the astronomical equinox). In 2036 this happens on the 21st. In 2018 it happens to fall on the 22nd.

In short, the time jumps from 2036/03/20 24:00 to 2036/03/21 01:00. The hour from midnight to 1 AM does not exist on that specific day.

When writing code that deals with time, ALWAYS keep in mind that unexpected small offsets from expected results are almost surely due to administrative time changes. This is even more true for historical dates, where offsets could be any number of minutes and seconds, not just whole or half-hours.

comments powered by Disqus