PDA

View Full Version : Current Time via Timevar2


godofwarares
02-16-2007, 01:14 AM
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
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.

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

godofwarares
02-17-2007, 02:17 AM
Cool :o

napo_p2p
02-17-2007, 02:28 AM
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
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?

Admins
02-17-2007, 06:28 PM
On clientside on Linux/Mac timevar2 is similar like on the server (seconds since 1970), but it can be a different timezone than the server. On Windows it is the seconds (plus nanoseconds) since the start of the program I think.

godofwarares
02-17-2007, 07:04 PM
x.x

I need timevar3 -_-

I just made a simple non-accurate clock ._.

Twinny
02-17-2007, 07:24 PM
I just made a simple non-accurate clock ._.

Join the club ^^. We serve punch on fridays.

godofwarares
02-17-2007, 09:45 PM
Join the club ^^. We serve punch on fridays.

:P

Admins
02-17-2007, 11:35 PM
Hmmm depending on the precision you need you could use "981048814 + timevar*5" to have a synchronized unix time, timevar is only synced each 5 seconds though.

godofwarares
02-17-2007, 11:38 PM
Any way to get precision with timevar2? Or could you enable timevar3 on my server? :P

Kristi
02-18-2007, 03:37 AM
I have written a class for you to use. I posted it in the completed code section for other people as well. ^_^

http://forums.graalonline.com/forums/showthread.php?p=1278750#post1278750

Admins
04-08-2009, 08:32 PM
Ok have added time formating functions, they will work once the new npcserver has been uploaded to all machines:

converttimetostring(float) - returns string - prints the timevar2 like 'Wed Apr 8 20:53:13 2009', see formatTimeString() for more options

formattimestring(str, float) - returns string - formats a string like the strftime function, parameters are the format and time

See http://www.manpagez.com/man/3/strftime/ for parameters.

Tigairius
04-08-2009, 08:34 PM
converttimetostring(float) - returns string - prints the timevar2 like 'Wed Apr 8 20:53:13 2009', see formatTimeString() for more options


Thank you for this!!!

Now how about a timevar3? :)

cbk1994
04-08-2009, 09:54 PM
Now how about a timevar3? :)

Seconded.