Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 801
Geek Avatar asked Sep 15 '13 12:09

Geek


People also ask

How accurate is system currentTimeMillis?

Regarding accuracy, you are almost correct. On SOME Windows machines, currentTimeMillis() has a resolution of about 10ms (not 50ms).

What does system currentTimeMillis () do?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

Is currentTimeMillis monotonic?

currentTimeMillis() is not monotonic. It is based on system time, and hence can be subject to variation either way (forward or backward) in the case of clock adjustments (e.g. via NTP).

What does system nanoTime return?

nanoTime() method returns the current value of the most precise available system timer, in nanoseconds. The value returned represents nanoseconds since some fixed but arbitrary time (in the future, so values may be negative) and provides nanosecond precision, but not necessarily nanosecond accuracy.


1 Answers

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.

like image 64
JB Nizet Avatar answered Sep 18 '22 06:09

JB Nizet