SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 14/06/2011, 08:39 PM   #21
Anzipane!
Big Clucker
 
Anzipane!'s Avatar
 
Join Date: Jul 2010
Location: Ravenna, Italy
Posts: 82
Reputation: 5
Default Re: How to: Unix timestamps

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;
}
Anzipane! is offline   Reply With Quote
Old 14/06/2011, 09:14 PM   #22
Blacklite
Huge Clucker
 
Join Date: Mar 2009
Location: am kiwi
Posts: 411
Reputation: 40
Default Re: How to: Unix timestamps

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().
__________________


Blacklite is offline   Reply With Quote
Old 14/06/2011, 09:17 PM   #23
jot16
Big Clucker
 
Join Date: Mar 2011
Posts: 177
Reputation: 0
Default Re: How to: Unix timestamps

nice tutorial!
jot16 is offline   Reply With Quote
Old 14/06/2011, 09:20 PM   #24
Jack_Rocker
Huge Clucker
 
Jack_Rocker's Avatar
 
Join Date: May 2010
Posts: 320
Reputation: 25
Default Re: How to: Unix timestamps

Good tut 9/10
__________________
Need Help? PM me!


OR add me on Skype
Jack_Rocker is offline   Reply With Quote
Old 15/06/2011, 09:41 AM   #25
Iphone1234g
Banned
 
Join Date: Jun 2011
Posts: 113
Reputation: 0
Default Re: How to: Unix timestamps

nice work man!
Iphone1234g is offline   Reply With Quote
Old 17/06/2011, 11:08 PM   #26
__
Huge Clucker
 
__'s Avatar
 
Join Date: Nov 2010
Location: London, UK
Posts: 240
Reputation: 17
Default Re: How to: Unix timestamps

Thanks for all of the kind words!

SQLite tutorial now available: http://forum.sa-mp.com/showthread.php?t=262417
__ is offline   Reply With Quote
Old 20/06/2011, 02:45 PM   #27
Steamator
Little Clucker
 
Steamator's Avatar
 
Join Date: Dec 2010
Posts: 39
Reputation: 0
Default Re: How to: Unix timestamps

Mine function for decoding you can find in my Signature and it's very short.
__________________
Steamator is offline   Reply With Quote
Old 14/07/2011, 07:26 AM   #28
||123||
Huge Clucker
 
Join Date: Aug 2009
Posts: 205
Reputation: 6
Default Re: How to: Unix timestamps

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.
__________________
Don't forget to thank me, if I ever helped ya'
||123|| is offline   Reply With Quote
Old 14/07/2011, 12:50 PM   #29
cessil
Beta Tester
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 2,447
Reputation: 254
Default Re: How to: Unix timestamps

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
cessil is offline   Reply With Quote
Old 15/07/2011, 05:39 AM   #30
__
Huge Clucker
 
__'s Avatar
 
Join Date: Nov 2010
Location: London, UK
Posts: 240
Reputation: 17
Default Re: How to: Unix timestamps

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.
__ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix Time Czeladnik Help Archive 16 20/04/2011 08:09 AM


All times are GMT. The time now is 10:17 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.