views:

312

answers:

9

If I wanted to work using dates and time going millions of years into the past/future how would I do it in C/C++/C#?

For example say I was working on an algorithm to see if a comet was going to hit the earth? Are there commercial or open source libraries that do this?

Most DateTime values only work for a few years. Unixes will run out in only 2038!.

Tony

A: 

You just need more bits to store the value and/or use larger increments of time to represent by the timestamp. Instead of ms, do years, then possibly use a math library/API designed for very large numbers if that isn't enough (and depending on your precision requirements) or use floating point.

Sydius
+7  A: 

Astronomers use their own calendar, different from the civil, Gregorian calendar.

Astronomical Julian Dates are what they use.

Look at http://en.wikipedia.org/wiki/Julian_day

Here's a typical package: Solar Clock.

S.Lott
+5  A: 

Well, not to be blunt or anything, but if it will hit the earth in like 1700 years, I don't think we'll need to know the actual date.

Unless it's a tuesday.

Never could get the hang of tuesdays.

Lasse V. Karlsen
When Kang and Kodos take over, they'll impose a new calendar anyway.
S.Lott
It can't be a Tuesday, I have meetings... can we reschedule? :P
BenAlabaster
The Douglas Adams quote is actually Thursday.
Echostorm
Let him worry about thursdays, my problem is tuesdays!
Lasse V. Karlsen
+6  A: 

You can't work out future UTC dates down to the second, because of the existence of leap seconds (one of which is scheduled for December 31, a couple of weeks from now). No one knows when leap seconds will be added to the calendar, because no one knows the rate at which the Earth's rotation will continue to slow in the future.

phenry
Good point! +1 ;-)
Treb
I think if you're trying to predict when a meteor will crash into the earth, possibly obliterating all human life, I don't think needing to know exactly what second is imperative :P
BenAlabaster
but you only need 86,400 missed leap seconds to miss a whole day. if you go millions of years away, it's a 'built-in' uncertainty of around 1:3e7 (assuming 0-2 leap seconds a year)
Javier
@Javier: it isn't 0-2 leap seconds per year. It's much less than that. It's less than 0-2 per decade overall. But it is random.
S.Lott
+4  A: 

If the time span offered by typical datetime variables is too small, you have two options:

  1. Using a variable with a bigger scope (i.e. more bits)
  2. Allowing for less precision

Which one you should choose depends on what exactly you want to do. But in general, my advice is option number 2. When we are talking about centuries or millenia, milliseconds are usually not that important...

Treb
A: 

The DateTime object in C# goes from 1.1.0001 to 12.31.9999. It's even more limited in SQL Server where the earliest date is something like 1735.

You're pretty much going to have to write something yourself or try to scrounge something from someone else. Going forward is probably easier (except the leap second thing.) There is some pretty good info on calendars here.

Echostorm
Probably 1753 - to avoid the missing days from English calendars in September 1752. (Much of continental Europe switch from Julian to Gregorian in 1584 or just after, rather than delaying until 1752.)
Jonathan Leffler
A: 

I think the problem is not knowing whether the comet is going to hit on a Friday or Sunday but if the comet is or isn't going to hit. I guess this means that you could model time in 1000ths of a second as a 64bit long long type. You can resolve 213 Billion Years. You can then have a routine which maps this back to meaning full-ish dates...

Tony Lambert
+1  A: 

When it comes to astronomical events, it doesn't matter which part of the earth is facing the Sun, i.e. has daylight. Therefore, calendars and dates are irrelevant too. You should simply use time. A 64 bit time_t for instance is quite sufficient.

Even when you do use time, keep in mind that 3-body systems (like Earth-Sun-Jupiter) are chaotic. Predicting the position of the Earth in the far future has a rather large margin of error.

MSalters
lets predict it stays Sun, Earth, Jupiter for some time to come - shall we!
Tony Lambert
+1  A: 

A 64-bit time_t will work until the year 292,277,026,596.

dan04