Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-18-2007, 03:22 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Class for getting serverside date and time

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]

PHP Code:
// 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(yearSPC 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,31};
  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

PHP Code:
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),getSeconds(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.
PHP Code:
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 ^-^
PHP Code:

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,31};
  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};

__________________

Last edited by Skyld; 02-20-2007 at 01:12 AM..
Reply With Quote
  #2  
Old 02-18-2007, 09:10 AM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
Impressive, reputation increase!
Reply With Quote
  #3  
Old 02-18-2007, 03:34 PM
contiga contiga is offline
Graal2001 Administration
contiga's Avatar
Join Date: Jul 2004
Location: Netherlands
Posts: 419
contiga is an unknown quantity at this point
Send a message via ICQ to contiga Send a message via AIM to contiga Send a message via MSN to contiga Send a message via Yahoo to contiga
Does what it should do, but not too impressive.
__________________
AIM: Contiga122
MSN: [email protected]
Status:
Quote:
Originally Posted by unixmad View Post
I am also awake 3AM to help correct problems.
Quote:
Originally Posted by Bomy Island RC people
Daniel: HoudiniMan is a bad guy =p
*Bell: rofl. I first read that as houdini is a bad man. like the little kid that wants his mommy to keep her away from that boogie man
Daniel: xD
*Rufus: I wouldn't want my kids around him.
Reply With Quote
  #4  
Old 02-18-2007, 05:52 PM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by contiga View Post
Does what it should do, but not too impressive.
I only wrote it up because someone on this forum needed it. If you think it can be done better, then by all means go ahead.
__________________
Reply With Quote
  #5  
Old 02-20-2007, 01:06 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
I am bumbing this because I made an error in the getUTC, that caused an addition/subtraction of a day in the getUTC function.
The code needs to be replaced with:
PHP Code:
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(yearSPC 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;

Apparently I cannot edit my original post anymore.
__________________
Reply With Quote
  #6  
Old 02-21-2007, 03:33 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
This is very nice, thanks for making it! Modded it slightly for my server (clientside time). Hope you dont mind
If you do, just tell me, I'll remove it.
__________________
Reply With Quote
  #7  
Old 02-21-2007, 04:54 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by cbkbud View Post
This is very nice, thanks for making it! Modded it slightly for my server (clientside time). Hope you dont mind
If you do, just tell me, I'll remove it.
I made it public for people to use. ^-^ credits are always nice, thats about it.
__________________
Reply With Quote
  #8  
Old 12-16-2007, 08:08 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
*necromancy*
I think this should be stickied, could someone do that?

Also Graal's timevar2 seems to be out of synch after I just synched my clock to the time server. So I added this function to make match the clock on my computer by the second.

PHP Code:
function adjustTime(ctime) {
  
// ctime + (60 * 8) - 3600 - 2
  
return ctime 3122;

Maybe Stefan needs to resynch whatever controls the timevar2?
__________________
Reply With Quote
  #9  
Old 12-16-2007, 09:06 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
wait for timevar3?
__________________
Deep into the Darkness peering...
Reply With Quote
  #10  
Old 12-16-2007, 07:40 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Inverness View Post
*necromancy*
I think this should be stickied, could someone do that?

Also Graal's timevar2 seems to be out of synch after I just synched my clock to the time server. So I added this function to make match the clock on my computer by the second.

PHP Code:
function adjustTime(ctime) {
  
// ctime + (60 * 8) - 3600 - 2
  
return ctime 3122;

Maybe Stefan needs to resynch whatever controls the timevar2?
If this is serverside, then it will just be a simple case of the system time being wrong on the machine that is hosting your gserver.

If this is clientside and on Windows, then timevar2 isn't doing what you think. (Windows version's clientside timevar2 is using the amount of seconds after the client has been opened, Linux/Mac and serverside use timevar2 as true UNIX time.)
Quote:
Originally Posted by Angel_Light View Post
wait for timevar3?
timevar3 does already exist, however it only works on Graal3D servers at the present time. In any case, timevar3's accuracy will only be good if the system time on the server is correct, so the gserver time needs updating (which I guess is what Inverness is trying to say.)
Reply With Quote
  #11  
Old 12-16-2007, 08:17 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Everything I did was on serverside (Val Dev), so I am indeed saying that the gserver time needs updating.
__________________
Reply With Quote
  #12  
Old 12-17-2007, 07:17 PM
Knightmare1 Knightmare1 is offline
Billy Allan
Knightmare1's Avatar
Join Date: Apr 2007
Posts: 804
Knightmare1 can only hope to improve
would their be a way to make this show up in the corner of your client? because my friend shakaraja is trying to make one..
__________________
I am the devil, I am here to do to devils work.
Reply With Quote
  #13  
Old 12-17-2007, 10:45 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
The client would have to be synched to the server
__________________
Reply With Quote
  #14  
Old 12-18-2007, 04:01 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Why not just add screenshot functionality for scripts, have it detect what type of computer, take a screenshot snippet of the correct place, use an online PHP OCR system that returns the correct time, then use that! Much simpler, fools.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 04:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.