Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Class for getting serverside date and time (https://forums.graalonline.com/forums/showthread.php?t=72260)

Kristi 02-18-2007 03:22 AM

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



Chandler 02-18-2007 09:10 AM

Impressive, reputation increase!

contiga 02-18-2007 03:34 PM

Does what it should do, but not too impressive.

Kristi 02-18-2007 05:52 PM

Quote:

Originally Posted by contiga (Post 1278845)
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.

Kristi 02-20-2007 01:06 AM

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.

cbk1994 02-21-2007 03:33 AM

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.

Kristi 02-21-2007 04:54 AM

Quote:

Originally Posted by cbkbud (Post 1280074)
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.

Inverness 12-16-2007 08:08 AM

*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?

Angel_Light 12-16-2007 09:06 AM

wait for timevar3?

Skyld 12-16-2007 07:40 PM

Quote:

Originally Posted by Inverness (Post 1364063)
*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 (Post 1364069)
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.)

Inverness 12-16-2007 08:17 PM

Everything I did was on serverside (Val Dev), so I am indeed saying that the gserver time needs updating.

Knightmare1 12-17-2007 07:17 PM

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..

Inverness 12-17-2007 10:45 PM

The client would have to be synched to the server

cbk1994 12-18-2007 04:01 AM

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.


All times are GMT +2. The time now is 12:43 PM.

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