Ok, this is
very barebone, but maybe you can make use of it. All of the hard math stuff has been done already.
PHP Code:
enum {
SEC, //Seconds
MIN, //Minutes
HOUR, //Hours
WDAY, //Day of the week (0:Sunday to 6:Saturday)
YEAR, //Year
YDAY, //Day of the year (starts at 0)
MON, //Month (0:January to 11:December)
MDAY //Day of the Month
}
const EPOCH_YR = 1970; //Epoch Year
const SECS_DAY = 24 * 60 * 60; //Seconds in a day
/**
gmtime():
Returns array (see enum)
*/
function gmtime(tvar) {
temp.result;
temp.dayclock, temp.dayno;
temp.year;
result = new[8];
year = EPOCH_YR;
dayclock = tvar % SECS_DAY;
dayno = tvar / SECS_DAY;
result[SEC] = dayclock % 60;
result[MIN] = (dayclock % 3600) / 60;
result[HOUR] = dayclock / 3600;
result[WDAY] = (dayno + 4) % 7; //Epoch starts on a Thurs.
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
result[YEAR] = year;
result[YDAY] = dayno;
result[MON] = 0;
while (dayno >= this._ytab[LEAPYEAR(year)][result[MON]]) {
dayno -= this._ytab[LEAPYEAR(year)][result[MON]];
result[MON]++;
}
result[MDAY] = dayno + 1;
return result;
}
function LEAPYEAR(year) {
return (!((year) % 4) && (((year) % 100) || !((year) % 400)));
}
function YEARSIZE(year) {
return (LEAPYEAR(year) ? 366 : 365)
}
Here is code that initializes some pseudo-const arrays and a little bit of sample code that will make use of the array that gmtime returns:
PHP Code:
//Pseudo-constarrays
function constArrays() {
//I WANT CONST ARRAYS!
this._ytab = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
this._days = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
this._months = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
//STILL WANT CONST ARRAYS!
}
function onCreated() {
constArrays();
//Sample Code
temp.result;
result = gmtime(timevar2 - 60*60*8); //GMT - 8
printf("Time (Pacific): %d:%d:%d", result[HOUR], result[MIN], result[SEC]);
printf("Date (Pacific): %s, %s %d, %d", this._days[result[WDAY]], this._months[result[MON]], result[MDAY], result[YEAR]);
}
Again, this is just all the math stuff done for you. If I get around to making functions that will return a date/time based on a format string (like PHP's time() function), I will release it in the code gallery, but maybe this can get you started with whatever you need to do.