Geek
Geek

Reputation: 27203

Is the value returned by System.currentTimeMillis() affected by Day Light Savings and Leap Second adjustments?

I know that System.currentTimeMillis() gives the time in milliseconds since the epoch and it is sensitive to the wall clock time of the system. I also know that it is not advisable to use System.currentTimeMillis() to calculate elapsed time in a program that measures time. Java library have provided System.nanoTime() for that purpose.

I have two specific questions for System.currentTimeMillis():

  1. Does it get affected by leap second adjustments? I think the answer is yes since the wall clock time of the system will be adjusted because of the leap second.
  2. Does it get affected when DST (DayLight Savings) is turned on/off ? What would happen when the time suddenly changes from 23:59 to 2:00? Since the system clock actually changes I would think that the answer is again yes but I would like to check with the community.

Upvotes: 8

Views: 3611

Answers (2)

Meno Hochschild
Meno Hochschild

Reputation: 44071

The clear answer to your two questions is: No, no.

In supplement to the answer of @ok about dst effects, System.currentTimeMillis() does not count leap seconds. In theory and practise it depends on the operational system. Since most OS and the Java VM included rather follow POSIX standard the consequence is (also by interoperability and behavioural compatibility issues) that leap seconds are not even allowed to have any effect on System.currentTimeMillis().

You can also easily verify this by comparing the value of System.currentTimeMillis() (divided by 1000) with your own calculation of SI seconds elapsed since 1972-01-01T00:00:00. The difference must be now 25 seconds.

UPDATE from the discussion below:

Leap seconds do not have effect on System.currentTimeMillis() in the long term (not counted), but maybe in the short term. I consider this as irrelevant however because this time source is not designed for achieving high accuracy and time stability.

The output of this time source is even subject to sudden changes up to minutes (for example if a pc was offline for a long time). And if people are going to use this time source and experience hard problems during a leap second then they will almost for sure also experience (harder) problems during any resync of this time source with an external time source like a NTP server.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691943

The value returned by System.currentTimeMillis() is the number of milliseconds that have elapsed since the epoch. This number of milliseconds is just that: a number of milliseconds.

Let's say that on a given day, your country has decided to go from 1:00 to 2:00 instantly, due to DST. Does that mean that 1 hour has elapsed between 1:00 and 2:00? No. 0 milliseconds have elapsed. Your country has simply decided that the millisecond value X was represented as 1:00, and that the millisecond X + 1 was represented at 2:00. The representation of the time doesn't change what milliseconds are.

Upvotes: 11

Related Questions