Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Current Time via Timevar2 (https://forums.graalonline.com/forums/showthread.php?t=72217)

godofwarares 02-16-2007 01:14 AM

Current Time via Timevar2
 
How do I take timevar2 and manipulate it to make it display the current time?

I know this can be done, I've seen it done :o

Gambet 02-16-2007 01:18 AM

timevar2 is the number of seconds since 1/1/1970

godofwarares 02-16-2007 01:28 AM

I know that, I didn't need a definition >_>

But I guess i'll experiment.

Gambet 02-16-2007 01:29 AM

Quote:

Originally Posted by godofwarares (Post 1278083)
I know that, I didn't need a definition >_>

But I guess i'll experiment.



Do the math x-x

Riot 02-16-2007 01:40 AM

There are two scripts on Graal.net you might want to look at:

http://graal.net/snippet/detail.php?type=snippet&id=68
http://graal.net/snippet/detail.php?type=snippet&id=84

napo_p2p 02-16-2007 03:09 AM

I have something that does this well, but is really 'barebone' right now. Once I document it and add some higher level functions, then I'll post it in the code gallery.

If you can't wait, you can also look at various time.c source files and check out the function 'gmtime'.

Twinny 02-16-2007 08:20 AM

I requested a date() and now() function. Don't think anything will happen though ^^.

Tolnaftate2004 02-17-2007 12:28 AM

It probably won't be too tough to write this soon, once a version with timevar3 is released (so it can be on both server- and clientside).

napo_p2p 02-17-2007 01:17 AM

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.dayclocktemp.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 = {
    {
312831303130313130313031},
    {
312931303130313130313031}
  };
  
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.

godofwarares 02-17-2007 02:17 AM

Cool :o

napo_p2p 02-17-2007 02:28 AM

Quote:

Originally Posted by godofwarares (Post 1278416)
Cool :o

I should probably mention that the gmtime() function takes a single parameter, which is the timestamp that you want converted from 'epoch time' to 'real time'.
It returns an array, and the indexes correspond with the enum defined at the beginning of the script.

Admins 02-17-2007 03:09 AM

Hmmm
We could add a date() function which is similar to format,
e.g. date("%Y-%m-%d",timevar2[,timezone]) which would print the time variable in a readable format. That would work on serverside-only though, the clientside timevar2 is not synchronized to the correct unix time yet (there is timevar3 on Graal3D though).

napo_p2p 02-17-2007 03:13 AM

Quote:

Originally Posted by Stefan (Post 1278433)
Hmmm
We could add a date() function which is similar to format,
e.g. date("%Y-%m-%d",timevar2[,timezone]) which would print the time variable in a readable format. That would work on serverside-only though, the clientside timevar2 is not synchronized to the correct unix time yet (there is timevar3 on Graal3D though).

Yeah, that what I've been planning to do, but it would be better if it were built-in.

How far is timevar2 off? Only by a couple of seconds at most, it seems.

zokemon 02-17-2007 02:25 PM

timevar2 on the client is your computer's time if I recall correctly.

godofwarares 02-17-2007 03:09 PM

I'm wondering why timevar3 hasn't been released yet?


All times are GMT +2. The time now is 05:44 AM.

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