[Tutorial] How to: Unix timestamps
#21

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;
}
Reply
#22

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().
Reply
#23

nice tutorial!
Reply
#24

Good tut 9/10
Reply
#25

nice work man!
Reply
#26

Thanks for all of the kind words!

SQLite tutorial now available: https://sampforum.blast.hk/showthread.php?tid=262417
Reply
#27

Mine function for decoding you can find in my Signature and it's very short.
Reply
#28

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.
Reply
#29

Quote:
Originally Posted by ||123||
View Post
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
Reply
#30

Quote:
Originally Posted by ||123||
View Post
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.
Reply
#31

thanks, i am using this to calculate speed for my speedometer i am making
Reply
#32

Good
Reply
#33

How could I decode the actual day from a timestamp (i.e. Monday, Tuesday, Wednesday, etc...)?
Reply
#34

Question, how do i convert a Unix timestamp to a format like January 1, 2011 5:49 PM?
Reply
#35

Quote:
Originally Posted by Rokzlive
View Post
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
Reply
#36

Quote:
Originally Posted by RealCop228
View Post
Did you read the thread?

http://pastebin.ca/2064736
Lol, maybe.... xD
Reply
#37

Quote:
Originally Posted by RealCop228
View Post
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.
Reply
#38

Quote:
Originally Posted by [HLF]Southclaw
View Post
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
View Post
By the way, the function on the first post seems to not work for me...
Which one, timec()?
Reply
#39

This is really good!
Reply
#40

So if I say..
new timestamp = gettime();
Then timestamp is the current timestamp of this moment?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)