8

I'm trying to extract a table from a database and reloading it in another type of database. The problem is that with my local settings the date 1 july 1937 poses a problem of gettng the timestamp when its 00:00:00. In the netherlands they changed meridians in 1937 causing the first 28 seconds of 1st of july 1937 not existing.

When i'm reading the date into a calendar to reformat the output, the time changes to either 28 seconds before the date; june 30th 23:59:32 or july 1st 00:00:28 (depending on driver) Anyone knows a workaround around this problem?

http://themagicofscience.blogspot.com/2010/08/java-puzzler-1-july-1937.html

6
  • It's not clear exactly what the inputs and outputs are here. Could you post some code?
    – Jon Skeet
    May 26, 2011 at 14:57
  • 1
    what would you consider valid output? Also, do you have to localize the time, or can you use UTC?
    – atk
    May 26, 2011 at 15:02
  • 1
    Generally speaking, if you require that level of accuracy, modern general purpose time system will not be adequate. They typically use what is called the 'proleptic Gregorian calendar' which applies the current rules backwards. It would require a very specialized system to handle such issues. There were eras when some countries (and I seem to remember The Netherlands being one of them) when their time offset from GMT (UTC only started in 1972) was included a fraction of a second. This would have been in the early 1900's. May 26, 2011 at 15:21
  • Does your software really make adjustments for this time zone change? There are so many of these odd variations in the history of timekeeping in the world, I would think that coding for them would, (a) be a nightmare to code, and (b) be a nightmare for users to deal with
    – Jay
    May 26, 2011 at 15:31
  • This is suspiciously related to the number of "leap seconds" that have been added to UTC over the past fourty years; there have been 28 added so far... en.wikipedia.org/wiki/Leap_second
    – joev
    May 26, 2011 at 20:51

4 Answers 4

3

Configure your Calendar to use a different Locale.

Calendars translate the encoded time into the local time. Those 20+ seconds just don't exist with different display formats in that Locale, so if you insist on keeping that Locale, and you insist on displaying dates with seconds set so, then you need to take it up with the Dutch government in 1937; however, if you change the display formatting to that of a different Locale, you will discover that the actual value of the underlying time data structure wasn't changed, it will resolve to different times in locales that have different seconds values to display.

The only caveats is that should you manipulate the time between reading it and storing it, then you might inadvertently create a new Time or Calendar object, which would set or reset its underlying data structures based on a translation of the Locale formatted time into the underlying data representation.

This is why it is best to handle bulk date and time handling in UTC, without daylight savings. Even though the times don't match up to the local times (and are harder to read for people in different time zones), every second of UTC exists, so simple +5 second changes can quickly be verified by a simple formatting of the impacted time.

The only caveat with this sort of handling is that later, you must always translate UTC time back to local time for display. Depending on the education of your audience, some of the Dutch might be shocked to find that their government didn't allow such seconds to exist and might demand that they are shown despite the rulings that such seconds are not part of the Dutch calendar.

Just wait until you discover the missing days back in 1582.

6
  • Doing manipulations in UTC seems the best solution to me. However, do other timezones pose the same problems, maybe in a slightly different manner?
    – ferdyh
    May 26, 2011 at 21:15
  • It's not really the time zone that's the issue, it is the locale combined with the time zone. Other locales present issues (rarely) but they tend to be, well... different in the details, although similar in the fact that a governmental action decided that some shift was needed to align calendars with the desired objective.
    – Edwin Buck
    May 26, 2011 at 21:57
  • Isn't there some sort of calendar object that completely ignores the locale?
    – ferdyh
    May 27, 2011 at 6:42
  • There are pure Date objects, but they have lots of issues (because they ignore locale). It really isn't possible to follow the Gregorian Calendar correctly unless you have locale awareness, so there's no one-size-fits-all solution on the horizon.
    – Edwin Buck
    May 27, 2011 at 14:20
  • Well.. When using JDBC there is no real option to change the default object anyways. All dates in MSSQL/ORACLE are returned in a java.sql.Date format and this will remain the problem from the beginning on. I'm now getting the date as a string from the database, seems to be working for now. Guess this is the best bet for extracting data.
    – ferdyh
    May 30, 2011 at 7:46
1

In Java, a date is stored internally in a time-zone independent way. (It's stored as the number of milliseconds since -- I forget the starting date, was it Jan 1, 1970 GMT?) When you output a date, THEN it has to take the time zone into account. But any internal manipulations shouldn't matter. You didn't say what database engine you're using so I don't know how it stores dates. I'm mostly working with Postgres these days, which stores all dates in GMT and converts to and from the appropriate time zone at input and output time.

So if you just set your time zone to GMT, then any changes for moving time zone boundaries, daylight savings time, etc should be irrelevant.

4
  • FYI, using 32-bit signed time (in seconds) covers a range from 1901-12-13 to 2038-01-19. If you have other options, e.g. 64-bit ms, use it instead.
    – karmakaze
    May 26, 2011 at 17:13
  • I just checked he source: The Java Date class stores the time as a long integer (64 bits) number of milliseconds since Jan 1, 1970. That should handle ... quick calculation ... a span of about 292 million years either way. No offense to Sun and Oracle, but I'm guessing that Java will probably not still be in use in 290 million years. I'll start worrying about a transition strategy to a bigger date field in about 280 million years. That should give adequate time to plan and implement it.
    – Jay
    May 27, 2011 at 17:30
  • But seriously folks, anyone making a time type in this day and age should at the very least be able to cover 14 billion years back to the big bang and forwards at least that much, or more...
    – karmakaze
    May 27, 2011 at 19:10
  • @karmakaze: I'm a creationist, so I only have to go back 6,000 years. I do have to allow for an infinite amount of time going forward into eternity.
    – Jay
    May 31, 2011 at 16:29
0

The simplest would probably be to do simple check for those specific times and change the date accordingly.

0

Try and extract the dates in GMT or UTC, then load them back in that way. Then you'll use the details of the new system for the locale, rather than the original system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.