0

I want to make time datatype in C++.To calculate date we can use julian number. Is there any specific number to calculate time

manu_dilip_shah
  • 880
  • 14
  • 32
  • Dates and times are startlingly hard to get right, given the vast number of special cases. You'd do well to hunt down some existing implementation's test suite to check that yours is actually correct. Personally, I'd avoid rolling my own entirely! – Rook Nov 12 '12 at 12:12
  • @Rook: or to look at it another way, times are reasonably easy but *calendars* are way too complex. – Steve Jessop Nov 12 '12 at 12:13
  • @SteveJessop I'll see your "reasonably easy" and raise you "time zones" and "leap seconds" ;-) – Rook Nov 12 '12 at 12:15
  • @Rook: arguably those are part of "calendars", but I certainly agree that they're complex :-) The basic problem with leap seconds is is that UTC doesn't advance uniformly at one second per elapsed second, and with time zones that local time isn't even non-strictly increasing. And in neither case can the discontinuities be entirely predicted in advance. TAI is simple, but not useful if you want to catch a train. – Steve Jessop Nov 12 '12 at 12:18

2 Answers2

2

Most programs use "non-leap seconds since epoc" when they need a number to represent an absolute date/time. The epoc could be anything, but midnight UTC on January 1, 1970 is common since so many OSes use it. If you use a 32 bit signed type then that's good for +/- 68 years. A 64 bit type easily covers the age of the universe.

For a few purposes (such as GPS and astronomy) "non-leap seconds" aren't good enough, you need actual time elapsed.

You also get variations on that theme, for example the Java standard libraries nudge you towards non-leap milliseconds since epoc, since that's what System.currentTimeMillis() returns. That only fits in 32 bit type for about a month, which is why older environments than Java didn't always embrace it.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
0

Not sure to understand, but maybe you can give a look at struct tm:

tm structure