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).
Date/Time Stamps
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Date/Time Stamps
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)
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
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?
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
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
That's easy: it was the year that the timestamp software for the original PDP-7 Unix was written.
Comments and correction welcome.
Comments and correction welcome.