0
$\begingroup$

What mathematical algorithm would I use to convert seconds since 1/1/1970 into a usable date/time format (time can be left off)? This includes leap years with extra days and any other additions.

I'm a novice at maths, so please try to keep it in layman's terms (or explain any terminology used).

  • 0
    Are you looking for an algorithm for [Unix Time/ Epoch Time](http://en.wikipedia.org/wiki/Unix_time) ? You usually ignore leap seconds in calculation. If you are doing something else, I don't think there is any math involved per se, just `if` loops to check for leap year.2012-06-06
  • 1
    As @Nunoxic said. However, there are tricks involving modulo computations that let you do this without any loops. Another useful trick is to pretend that the year begins March 1 when you calculate back and forth between day number and the date. This helps because it puts any leap days at the end of the year. This is after all how the old Roman calendar worked, and the reason why leap day is at the end of February (I am oversimplifying somewhat here). The final conversion to and from dates as we use them is quite easy.2012-06-06
  • 0
    @Nunoxic Pretty much. This is ignoring leap seconds (I think that might be a little too pedantic). But it still has to be a mathematical algorithm to determine the correct date (I'm just mentioning leap years as a reminder in-case it's assumed as a simple seconds->days->year esque exercise).2012-06-06
  • 1
    Why don't you look at the algorithm used in the implementation of a programming language like C? This should get you started: http://www.raspberryginger.com/jbailey/minix/html/gmtime_8c-source.html2012-06-06
  • 0
    @Adeel - Almost unreadable to me. This is mathematics, after all. Not C.2012-06-07

0 Answers 0