2

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!

Verdagon
  • 2,456
  • 3
  • 22
  • 36
  • 1
    Every now and then there's a leap second, then you have an hour with 3601 seconds. – Daniel Fischer May 11 '13 at 20:44
  • UNIX time does not care about Earth's rotation. Under UNIX time 1 hour is exactly 3600 seconds. – pmg May 11 '13 at 20:44
  • @DanielFischer that's the kind of thing I'm worried about, lol. – Verdagon May 11 '13 at 20:45
  • http://stackoverflow.com/questions/6158053/get-the-number-of-days-weeks-and-months-since-epoch-in-java – Rachel Gallen May 11 '13 at 20:45
  • Those are called leap seconds.As or today there have been 25 leap seconds since the epoch. – jim mcnamara May 11 '13 at 20:45
  • @pmg yes, but I want my HoursSinceEpoch() function to give me a number at 1:59pm today, and a different number at 2:00pm today. Using just a unix_timestamp and dividing by 3600 would not make that happen. – Verdagon May 11 '13 at 20:46
  • [Duplicate](http://stackoverflow.com/questions/3802893/number-of-days-between-two-dates-in-joda-time)? – hd1 May 11 '13 at 20:46
  • Awesome, could that joda thing do what I need here? – Verdagon May 11 '13 at 20:47
  • If what you're doing requires such a precision then there's a formula for that which you could use as a coefficient to calculate the real time. If you really wanted to get serious you could do it per the users GPS cords. – ChiefTwoPencils May 11 '13 at 20:48
  • @Verdagon: of course it would give a different result, as long as you do integer division. – pmg May 11 '13 at 20:49

3 Answers3

2

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);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

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.

Jemolah
  • 1,962
  • 3
  • 24
  • 41