Calculate how many hours since the epoch?

it's a simple question, but I'm nervous about how complicated the answer might be.

I want to know in my application, how many hours have passed since the epoch. More specifically, I want to know "how many times since the epoch has a GMT clock ended in :00"

Normally I would just do:

number_of_seconds_since_epoch / 3600

but I'm not really sure if an hour is exactly 3600 seconds. I keep hearing about how the earth keeps slowing down and speeding up, and how leapyears do weird things, etc.

Can anyone show me the correct way to do this, in any language? Thanks!


Java systems, like many computer systems assume that every day as 24 hours (and some have daylight saving changes) Leap seconds are treated like a normal time drift correction and is handled via the OS and protocols like NTP or PTP.

GMT doesn't have daylight saving changes so you can calculate the number of hours since epoch with

int hours = (int) (System.currentTimeMillis() / 3600000);

There is a discussion about leap seconds in Date API, one of the points:

Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.


Be careful here. Depending on how you obtain the numbre of seconds since the epoch, you probably do not have to care about leap seconds. If you call an operating systems API you will just get the number of seconds since the epoch 1970-01-01T00:00:00.

Leap seconds have been inserted to adapt dates to earth rotation. Just counting seconds since the epoch is what OS APIs do return.

链接地址: http://www.djcxy.com/p/36670.html

上一篇: 乔达时间发现小时之间的差异

下一篇: 计算时代以来多少小时?