Re: How to: Unix timestamps -
Anzipane! - 14.06.2011
Here's the function to decode a unix timestamp.
NOTE: this function was posted by
pen_TheGun on the Useful Functions topic.
pawn Code:
stock date( timestamp, _form=0 )
{
/*
~ convert a Timestamp to a Date.
~ 10.07.2009
date( 1247182451 ) will print >> 09.07.2009-23:34:11
date( 1247182451, 1) will print >> 09/07/2009, 23:34:11
date( 1247182451, 2) will print >> July 09, 2009, 23:34:11
date( 1247182451, 3) will print >> 9 Jul 2009, 23:34
*/
new year=1970, day=0, month=0, hour=0, mins=0, sec=0;
new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
new returnstring[32];
while(timestamp>31622400){
timestamp -= 31536000;
if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
year++;
}
if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
days_of_month[1] = 29;
else
days_of_month[1] = 28;
while(timestamp>86400){
timestamp -= 86400, day++;
if(day==days_of_month[month]) day=0, month++;
}
while(timestamp>60){
timestamp -= 60, mins++;
if( mins == 60) mins=0, hour++;
}
sec=timestamp;
switch( _form ){
case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hour,mins);
default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
}
return returnstring;
}
Re: How to: Unix timestamps -
Blacklite - 14.06.2011
Note that in MySQL, you can use the
FROM_UNIXTIME() function to retrieve a date:
Code:
mysql> SELECT FROM_UNIXTIME(1196440219);
-> '2007-11-30 10:30:19'
mysql> SELECT FROM_UNIXTIME(1196440219) + 0;
-> 20071130103019.000000
mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
-> '%Y %D %M %h:%i:%s %x');
-> '2007 30th November 10:30:59 2007'
The FROM_UNIXTIME() function uses the same format as
DATE_FORMAT().
Re: How to: Unix timestamps -
jot16 - 14.06.2011
nice tutorial!
Re: How to: Unix timestamps -
Jack_Rocker - 14.06.2011
Good tut 9/10
Re: How to: Unix timestamps -
Iphone1234g - 15.06.2011
nice work man!
Re: How to: Unix timestamps -
__ - 17.06.2011
Thanks for all of the kind words!
SQLite tutorial now available:
https://sampforum.blast.hk/showthread.php?tid=262417
Re: How to: Unix timestamps -
Steamator - 20.06.2011
Mine function for decoding you can find in my Signature and it's very short.
Re: How to: Unix timestamps -
||123|| - 14.07.2011
How many times would this be more efficient than using variables to set time units seperetely? Because, the decoding of UNIX will also take time, but the variables could be used with no need to decode.
Re: How to: Unix timestamps -
cessil - 14.07.2011
Quote:
Originally Posted by ||123||
How many times would this be more efficient than using variables to set time units seperetely? Because, the decoding of UNIX will also take time, but the variables could be used with no need to decode.
|
if you're talking about getdate then the benefit of using gettime is that it's easier to compare dates within pawn
Re: How to: Unix timestamps -
__ - 15.07.2011
Quote:
Originally Posted by ||123||
How many times would this be more efficient than using variables to set time units seperetely? Because, the decoding of UNIX will also take time, but the variables could be used with no need to decode.
|
You need to think of it from a logical point of view, which is something you probably haven't done.
A calculation that takes ~1 microsecond or 2 useless variables that could be one - you spend more time saving and loading the other two than you do calculating or 'decoding' a timestamp.
Re: How to: Unix timestamps -
Jantjuh - 16.07.2011
thanks, i am using this to calculate speed for my speedometer i am making
Re: How to: Unix timestamps -
BrUn3S - 18.10.2011
Good
Re: How to: Unix timestamps -
Scenario - 17.12.2011
How could I decode the actual day from a timestamp (i.e. Monday, Tuesday, Wednesday, etc...)?
Re: How to: Unix timestamps -
Rokzlive - 07.01.2012
Question, how do i convert a Unix timestamp to a format like January 1, 2011 5:49 PM?
Re: How to: Unix timestamps -
Scenario - 07.01.2012
Quote:
Originally Posted by Rokzlive
Question, how do i convert a Unix timestamp to a format like January 1, 2011 5:49 PM?
|
Did you read the thread?
http://pastebin.ca/2064736
Re: How to: Unix timestamps -
Rokzlive - 07.01.2012
Quote:
Originally Posted by RealCop228
|
Lol, maybe.... xD
Re: How to: Unix timestamps -
Calgon - 23.03.2012
Quote:
Originally Posted by RealCop228
How could I decode the actual day from a timestamp (i.e. Monday, Tuesday, Wednesday, etc...)?
|
Sorry for the late response. I don't think there's a calculation you can perform to do it, you have to have some sort of database of days or something, you can always check online or something. You can't do the same with the getdate function anyway though.
Re: How to: Unix timestamps -
__ - 15.06.2012
Quote:
Originally Posted by [HLF]Southclaw
Couldn't you use the modulus operator in some way?
Find out what day the first date of unix is (1st, jan, 1970?)
As far as I know days haven't changed their cycle (similar to how leap years do), or have they?
|
The timec() function should work fine, there are other functions released on the forum too if this isn't the case.
Quote:
Originally Posted by [HLF]Southclaw
By the way, the function on the first post seems to not work for me...
|
Which one, timec()?
Re: How to: Unix timestamps -
Tanush123 - 22.05.2013
This is really good!
Re: How to: Unix timestamps -
trukker1998 - 19.02.2014
So if I say..
new timestamp = gettime();
Then timestamp is the current timestamp of this moment?