Page 1 of 1

Date/Time Stamps

Posted: Thu Aug 07, 2003 7:05 pm
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).

Re:Date/Time Stamps

Posted: Fri Aug 08, 2003 12:25 am
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)

Re:Date/Time Stamps

Posted: Fri Aug 08, 2003 9:35 pm
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;
}

Re:Date/Time Stamps

Posted: Fri Aug 08, 2003 10:06 pm
by Tuxaroo
What's with the year 1970?

Re:Date/Time Stamps

Posted: Sat Aug 09, 2003 9:33 am
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).

Re:Date/Time Stamps

Posted: Sat Aug 09, 2003 10:28 am
by Tux
I was wondering what is so special about 1970?
Billy made 1 billion?

Re:Date/Time Stamps

Posted: Sat Aug 09, 2003 2:29 pm
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.