Date/Time Stamps

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Anonymous

Date/Time Stamps

Post by Anonymous »

Hi.

This question isn't specific to operating systems, but is more relevant here than anywhere else.
Some filesystems store their date/time stamps as a dword containing the number of seconds between Jan. 1, 1970 at 0:00 and the date/time being stored.
I was wondering how, exactly, to convert this dword to seperate values, compensating for daylight saving time and leap years (including leap seconds, if possible).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Date/Time Stamps

Post by Pype.Clicker »

i would try a two-step approach: first convert that amount of seconds in an amount of days plus an amount of seconds in the day.

then if you divide your amount of days by (3*365+366) you should move to the nearest 4 years block from which you just have to substract 365 (or 366) to the remainder until you find Day In Year ...

From there, finding the month, day of month etc. is just classic programming (hint consider using {31,28,31,30,31,30,31,31,...} array)
Anonymous

Re:Date/Time Stamps

Post by Anonymous »

Here's my code (C++). I threw this together in a few minutes, so I'm not sure how accurate it is.

Is there a better way to do this?

Code: Select all

void print_stamp(int stamp) {
   int second, minute, hour, dayyears4, year, dayofyear, month, dayofmonth;
   int minutes, hours, days, years4, years;
   int leapval = 0;
   bool leapyear = false;
   int monthdata[13] = {0, 0, 31, 31 + 28, 31 + 28 + 31, 31 + 28 + 31 + 30, 31 + 28 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30};
   
   minutes = stamp / 60;
   second = stamp % 60;
   hours = minutes / 60;
   minute = minutes % 60;
   days = hours / 24;
   hour = hours % 24;
   years4 = days / ((4 * 365) + 1);
   dayyears4 = days % ((4 * 365) + 1);
   years = dayyears4 / 365;
   year = (years4 * 4) + years + 1970;
   dayofyear = dayyears4 % 365;
   
   if (years == 3) {
      leapyear = true;
   }
   
   int i;
   
   for (i = 12; i > 0; --i) {
      if (leapyear) {
         if (i > 2) {
            leapval = 1;
         }
         else {
            leapval = 0;
         }
      }
      else {
         leapval = 0;
      }
      if (dayofyear >= (monthdata[i] + leapval)) {
         month = i;
         dayofmonth = (dayofyear - monthdata[i]) + (1 - leapval);
         break;
      }
   }
   
   cout << hour << ":" << minute << ":" << second << endl;
   cout << month << " " << dayofmonth << ", " << year << endl;
}
Tuxaroo

Re:Date/Time Stamps

Post by Tuxaroo »

What's with the year 1970?
Anonymous

Re:Date/Time Stamps

Post by Anonymous »

1970 is known as "the epoch" and is used as the base of the year. If you start at a different base year, you'll get a different date/time from the one actually stored (because they are stored with their base year at 1970).
Tux

Re:Date/Time Stamps

Post by Tux »

I was wondering what is so special about 1970?
Billy made 1 billion?
Schol-R-LEA

Re:Date/Time Stamps

Post by Schol-R-LEA »

That's easy: it was the year that the timestamp software for the original PDP-7 Unix was written.

Comments and correction welcome.
Post Reply