Kristi

02-18-2007, 03:22 AM

Edit: I made an error in the formal month function. This has been updated. I also needed to swap two lines in the date function. Updated as well.

New Edit: I stated that the getDate function could be made better. It was using a while loop to go though each year. I redid the math so it handled all the leap year rules linerally (leap years dont occur on years evenly divisible by 100 but not divisible by 400, so although 1900 is divisble by 4, its not a leap year.) Now instead of running through the loops x amount of years since 1970, it only runs through the loop 4 times, which is far far far more efficient, for even 5000 years. You can see the old getDate function at the bottom of this post.

NEWER Edit: Added getUTC, a function that will get the number of seconds since Jan. 1st, 1970, when given the year, month, day, hour, minutes, and seconds. At the bottom of this post, in green, I will show a demo of how to use it to find out what day of the week any given date will be.

------------------------------------------------------------------

Someone posted a thread about this, so I made a class for the world to enjoy for various functions for serverside date and time. I just wrote this up quickly to help out.

Note from Skyld: Hell Raven requested I update the getUTC() function; this post and the code snippet below has been updated to reflect those changes. [1 (http://forums.graalonline.com/forums/showpost.php?p=1279559&postcount=5)]

// Class made by Hell Raven (Kristi)

// Various Date and Time Functions

// Including functions for formatting.

function getUTC(year,month,day,hour,minutes,seconds) {

// Given the year, month, day, hour, minutes, and seconds

// It will return the UTC var (Unix standard time)

// IE: the number of seconds since Jan 1st, 1970

// IE timevar2 in graal.

year -= 1601;

sendtorc(isLeapYear(year) SPC isLeapYear(temp.year2));

temp.days = int(year/400) - int(year/100) + int(year/4) + year * 365;

temp.daysinmonth = {31,28 + isLeapYear(year+1),31,30,31,30,31,31,30,31,30,31};

for(i=0;i<month-1;i++)

temp.days += temp.daysinmonth[i];

temp.days += day - 134775;

return temp.days * 86400 + hour * 3600 + minutes * 60 + seconds;

}

function adjustTimeZone(ctime,hours) {

// Takes a UTC variable (eg: timevar2)

// And adjusts it by how many hours you need to.

// UTC is GMT, so eg: EST would be hours = -5 (GMT minus 5 hours)

return ctime + 3600 * hours;

}

function doubleDigit(num) {

// Makes a double digit string out of a number

// eg: 2 becomes 02 and 14 stays 14

return num.length()==1?"0" @ num:num;

}

function getFormalNumber(num) {

// Formalizes a number

// eg: 1 becomes 1st, 22 becomes 22nd, etc

temp.num2 = doubleDigit(num);

temp.test = {temp.num2.substring(temp.num2.length()-2,1),temp.num2.substring(temp.num2.length()-1,1)};

if(temp.test[0] == 1) return num @ "th";

switch(temp.test[1]) {

case 1:

return num @ "st";

break;

case 2:

return num @ "nd";

break;

case 3:

return num @ "rd";

break;

default:

return num @ "th";

break;

}

}

function getFormalDayEnglish(weekday) {

// Returns the English day of the week for a number given weekday

// eg: 2 returns Tuesday

temp.days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

return temp.days[weekday];

}

function getFormalMonthEnglish(month) {

// Returns the English month for a number given month

// eg: 2 returns February

temp.months = {"January","February","March","April","May","June","July","August","September","October","November","December"};

return temp.months[month-1];

}

function getDate(ctime) {

// Returns an array {year,month,day}

// For UTC time variable (eg: timevar2)

ctime += 11644473600;

temp.year = 1601;

temp.days = ((ctime - ctime % 86400)/86400);

temp.yearsets = {146097,36524,1461,365};

temp.yearmult = {400,100,4,1};

for(i=0;i<4;i++) {

temp.var = int(temp.days / temp.yearsets[i]);

temp.year += temp.var * temp.yearmult[i];

temp.days -= temp.var * temp.yearsets[i];

}

temp.daysinmonth = {31,28 + isLeapYear(temp.year),31,30,31,30,31,31,30,31,30,3 1};

for(temp.month = 1;temp.days >= temp.daysinmonth[temp.month-1];temp.month++)

temp.days -= temp.daysinmonth[temp.month-1];

return {temp.year,temp.month,temp.days+1};

}

function getWeekday(ctime) {

// Returns the number of the weekday

// For UTC time variable (eg: timevar2)

return (((ctime - ctime % 86400)/86400) + 4) % 7;

}

function getAMPM(hour) {

// Returns AM or PM for a given hour

// eg 11 returns AM and 20 returns PM

return hour%12==hour?"AM":"PM";

}

function to12Hour(hour) {

// Returns the 12 hour number for a 24 hour hour

// Eg: 22 returns 10, 9 returns 9, 0 returns 12

temp.ahour = hour%12;

return temp.ahour==0?12:temp.ahour;

}

function isLeapYear(year) {

// Returns true if year is a leap year.

if(!(year % 400)) return true;

else if(!(year % 100)) return false;

else if(!(year % 4)) return true;

return false;

}

function getSeconds(ctime) {

// Returns the seconds of formal time

// For UTC time variable (eg: timevar2)

return int(ctime % 60);

}

function getMinutes(ctime) {

// Returns the minutes of formal time

// For UTC time variable (eg: timevar2)

return ((ctime - ctime % 60)/60) % 60;

}

function getHour(ctime) {

// Returns the hours of formal time

// For UTC time variable (eg: timevar2)

return ((ctime - ctime % 3600)/3600) % 24;

}

Here is an example

join("time"); //The Class

function onCreated() {

sendToRc(hellRavenFormalTime());

}

function hellRavenFormalTime() {

temp.ctime = adjustTimeZone(timevar2,-5); // to EST

temp.date = getDate(temp.ctime);

temp.clock = {getHour(temp.ctime),getMinutes(temp.ctime),getSec onds(temp.ctime)};

return getFormalDayEnglish(getWeekday(temp.ctime)) @ ", " @

getFormalMonthEnglish(temp.date[1]) SPC getFormalNumber(temp.date[2]) @ ", " @ temp.date[0] @ " @ " @

to12Hour(temp.clock[0]) @ ":" @ doubleDigit(temp.clock[1]) @ ":" @ doubleDigit(temp.clock[2]) SPC getAMPM(temp.clock[0]) SPC "EST";

}

This outputs the following:

Classic (Server): Saturday, February 17th, 2007 @ 8:34:53 PM EST

The following is a demo using getUTC to find out what day of the week any given day will be. In my case, I made a poorly coded demo to tell what day 4/17/2009 will be. This is just to show a way that getUTC can be useful.

function onCreated() {

temp.newtimevar = getUTC(2009,4,17,0,0,0);

sendtorc("4/17/2009 will be a" SPC getFormalDayEnglish(getWeekday(temp.newtimevar)));

}

This produced the following output:

Classic (Server): 4/17/2009 will be a Friday

Edit Subnote: This is the old getDate function that was replaced. I leave this here so you can see an example of an infinite structure vs a finite one. You should always try to make a structure finite if possible. This repeats for as many years as it needs to, the newer function above only needs exactly 4 loops. Learn from this ^-^

function getDate(ctime) {

// Returns an array {year,month,day}

// For UTC time variable (eg: timevar2)

temp.days = ((ctime - ctime % 86400)/86400);

temp.year = 1970;

while(temp.days >= 365 + isLeapYear(temp.year)) {

temp.days -= 365 + isLeapYear(temp.year);

temp.year++;

}

temp.daysinmonth = {31,28 + isLeapYear(temp.year),31,30,31,30,31,31,30,31,30,3 1};

for(temp.month = 1;temp.days >= temp.daysinmonth[temp.month-1];temp.month++)

temp.days -= temp.daysinmonth[temp.month-1];

return {temp.year,temp.month,temp.days+1};

}

New Edit: I stated that the getDate function could be made better. It was using a while loop to go though each year. I redid the math so it handled all the leap year rules linerally (leap years dont occur on years evenly divisible by 100 but not divisible by 400, so although 1900 is divisble by 4, its not a leap year.) Now instead of running through the loops x amount of years since 1970, it only runs through the loop 4 times, which is far far far more efficient, for even 5000 years. You can see the old getDate function at the bottom of this post.

NEWER Edit: Added getUTC, a function that will get the number of seconds since Jan. 1st, 1970, when given the year, month, day, hour, minutes, and seconds. At the bottom of this post, in green, I will show a demo of how to use it to find out what day of the week any given date will be.

------------------------------------------------------------------

Someone posted a thread about this, so I made a class for the world to enjoy for various functions for serverside date and time. I just wrote this up quickly to help out.

Note from Skyld: Hell Raven requested I update the getUTC() function; this post and the code snippet below has been updated to reflect those changes. [1 (http://forums.graalonline.com/forums/showpost.php?p=1279559&postcount=5)]

// Class made by Hell Raven (Kristi)

// Various Date and Time Functions

// Including functions for formatting.

function getUTC(year,month,day,hour,minutes,seconds) {

// Given the year, month, day, hour, minutes, and seconds

// It will return the UTC var (Unix standard time)

// IE: the number of seconds since Jan 1st, 1970

// IE timevar2 in graal.

year -= 1601;

sendtorc(isLeapYear(year) SPC isLeapYear(temp.year2));

temp.days = int(year/400) - int(year/100) + int(year/4) + year * 365;

temp.daysinmonth = {31,28 + isLeapYear(year+1),31,30,31,30,31,31,30,31,30,31};

for(i=0;i<month-1;i++)

temp.days += temp.daysinmonth[i];

temp.days += day - 134775;

return temp.days * 86400 + hour * 3600 + minutes * 60 + seconds;

}

function adjustTimeZone(ctime,hours) {

// Takes a UTC variable (eg: timevar2)

// And adjusts it by how many hours you need to.

// UTC is GMT, so eg: EST would be hours = -5 (GMT minus 5 hours)

return ctime + 3600 * hours;

}

function doubleDigit(num) {

// Makes a double digit string out of a number

// eg: 2 becomes 02 and 14 stays 14

return num.length()==1?"0" @ num:num;

}

function getFormalNumber(num) {

// Formalizes a number

// eg: 1 becomes 1st, 22 becomes 22nd, etc

temp.num2 = doubleDigit(num);

temp.test = {temp.num2.substring(temp.num2.length()-2,1),temp.num2.substring(temp.num2.length()-1,1)};

if(temp.test[0] == 1) return num @ "th";

switch(temp.test[1]) {

case 1:

return num @ "st";

break;

case 2:

return num @ "nd";

break;

case 3:

return num @ "rd";

break;

default:

return num @ "th";

break;

}

}

function getFormalDayEnglish(weekday) {

// Returns the English day of the week for a number given weekday

// eg: 2 returns Tuesday

temp.days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

return temp.days[weekday];

}

function getFormalMonthEnglish(month) {

// Returns the English month for a number given month

// eg: 2 returns February

temp.months = {"January","February","March","April","May","June","July","August","September","October","November","December"};

return temp.months[month-1];

}

function getDate(ctime) {

// Returns an array {year,month,day}

// For UTC time variable (eg: timevar2)

ctime += 11644473600;

temp.year = 1601;

temp.days = ((ctime - ctime % 86400)/86400);

temp.yearsets = {146097,36524,1461,365};

temp.yearmult = {400,100,4,1};

for(i=0;i<4;i++) {

temp.var = int(temp.days / temp.yearsets[i]);

temp.year += temp.var * temp.yearmult[i];

temp.days -= temp.var * temp.yearsets[i];

}

temp.daysinmonth = {31,28 + isLeapYear(temp.year),31,30,31,30,31,31,30,31,30,3 1};

for(temp.month = 1;temp.days >= temp.daysinmonth[temp.month-1];temp.month++)

temp.days -= temp.daysinmonth[temp.month-1];

return {temp.year,temp.month,temp.days+1};

}

function getWeekday(ctime) {

// Returns the number of the weekday

// For UTC time variable (eg: timevar2)

return (((ctime - ctime % 86400)/86400) + 4) % 7;

}

function getAMPM(hour) {

// Returns AM or PM for a given hour

// eg 11 returns AM and 20 returns PM

return hour%12==hour?"AM":"PM";

}

function to12Hour(hour) {

// Returns the 12 hour number for a 24 hour hour

// Eg: 22 returns 10, 9 returns 9, 0 returns 12

temp.ahour = hour%12;

return temp.ahour==0?12:temp.ahour;

}

function isLeapYear(year) {

// Returns true if year is a leap year.

if(!(year % 400)) return true;

else if(!(year % 100)) return false;

else if(!(year % 4)) return true;

return false;

}

function getSeconds(ctime) {

// Returns the seconds of formal time

// For UTC time variable (eg: timevar2)

return int(ctime % 60);

}

function getMinutes(ctime) {

// Returns the minutes of formal time

// For UTC time variable (eg: timevar2)

return ((ctime - ctime % 60)/60) % 60;

}

function getHour(ctime) {

// Returns the hours of formal time

// For UTC time variable (eg: timevar2)

return ((ctime - ctime % 3600)/3600) % 24;

}

Here is an example

join("time"); //The Class

function onCreated() {

sendToRc(hellRavenFormalTime());

}

function hellRavenFormalTime() {

temp.ctime = adjustTimeZone(timevar2,-5); // to EST

temp.date = getDate(temp.ctime);

temp.clock = {getHour(temp.ctime),getMinutes(temp.ctime),getSec onds(temp.ctime)};

return getFormalDayEnglish(getWeekday(temp.ctime)) @ ", " @

getFormalMonthEnglish(temp.date[1]) SPC getFormalNumber(temp.date[2]) @ ", " @ temp.date[0] @ " @ " @

to12Hour(temp.clock[0]) @ ":" @ doubleDigit(temp.clock[1]) @ ":" @ doubleDigit(temp.clock[2]) SPC getAMPM(temp.clock[0]) SPC "EST";

}

This outputs the following:

Classic (Server): Saturday, February 17th, 2007 @ 8:34:53 PM EST

The following is a demo using getUTC to find out what day of the week any given day will be. In my case, I made a poorly coded demo to tell what day 4/17/2009 will be. This is just to show a way that getUTC can be useful.

function onCreated() {

temp.newtimevar = getUTC(2009,4,17,0,0,0);

sendtorc("4/17/2009 will be a" SPC getFormalDayEnglish(getWeekday(temp.newtimevar)));

}

This produced the following output:

Classic (Server): 4/17/2009 will be a Friday

Edit Subnote: This is the old getDate function that was replaced. I leave this here so you can see an example of an infinite structure vs a finite one. You should always try to make a structure finite if possible. This repeats for as many years as it needs to, the newer function above only needs exactly 4 loops. Learn from this ^-^

function getDate(ctime) {

// Returns an array {year,month,day}

// For UTC time variable (eg: timevar2)

temp.days = ((ctime - ctime % 86400)/86400);

temp.year = 1970;

while(temp.days >= 365 + isLeapYear(temp.year)) {

temp.days -= 365 + isLeapYear(temp.year);

temp.year++;

}

temp.daysinmonth = {31,28 + isLeapYear(temp.year),31,30,31,30,31,31,30,31,30,3 1};

for(temp.month = 1;temp.days >= temp.daysinmonth[temp.month-1];temp.month++)

temp.days -= temp.daysinmonth[temp.month-1];

return {temp.year,temp.month,temp.days+1};

}