[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


Messages In This Thread
How to: Unix timestamps - by __ - 13.05.2011, 15:22
Re: How to: Unix timestamps - by CrunkBankS - 13.05.2011, 16:58
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:11
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:39
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:43
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:58
Re: How to: Unix timestamps - by Zimon95 - 13.05.2011, 18:34
Re: How to: Unix timestamps - by __ - 14.05.2011, 10:10
Re: How to: Unix timestamps - by Haydz - 16.05.2011, 10:52
Re: How to: Unix timestamps - by linuxthefish - 16.05.2011, 11:40
Re: How to: Unix timestamps - by Swiftz - 16.05.2011, 15:47
Re: How to: Unix timestamps - by Zimon95 - 16.05.2011, 17:57
Re: How to: Unix timestamps - by __ - 17.05.2011, 08:39
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 00:23
Re: How to: Unix timestamps - by __ - 19.05.2011, 10:04
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 14:30
Re: How to: Unix timestamps - by __ - 19.05.2011, 14:42
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 16:38
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 22:00
Re: How to: Unix timestamps - by Scenario - 14.06.2011, 19:30
Re: How to: Unix timestamps - by Anzipane! - 14.06.2011, 20:39
Re: How to: Unix timestamps - by Blacklite - 14.06.2011, 21:14
Re: How to: Unix timestamps - by jot16 - 14.06.2011, 21:17
Re: How to: Unix timestamps - by Jack_Rocker - 14.06.2011, 21:20
Re: How to: Unix timestamps - by Iphone1234g - 15.06.2011, 09:41
Re: How to: Unix timestamps - by __ - 17.06.2011, 23:08
Re: How to: Unix timestamps - by Steamator - 20.06.2011, 14:45
Re: How to: Unix timestamps - by ||123|| - 14.07.2011, 07:26
Re: How to: Unix timestamps - by cessil - 14.07.2011, 12:50
Re: How to: Unix timestamps - by __ - 15.07.2011, 05:39
Re: How to: Unix timestamps - by Jantjuh - 16.07.2011, 19:49
Re: How to: Unix timestamps - by BrUn3S - 18.10.2011, 18:59
Re: How to: Unix timestamps - by Scenario - 17.12.2011, 00:28
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:02
Re: How to: Unix timestamps - by Scenario - 07.01.2012, 20:07
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:14
Re: How to: Unix timestamps - by Calgon - 23.03.2012, 10:42
Re: How to: Unix timestamps - by __ - 15.06.2012, 07:19
Re: How to: Unix timestamps - by Tanush123 - 22.05.2013, 21:03
Re: How to: Unix timestamps - by trukker1998 - 19.02.2014, 11:35
Re: How to: Unix timestamps - by Scenario - 19.02.2014, 11:53
Re: How to: Unix timestamps - by AroseKhanNiazi - 01.03.2014, 11:38
Re: How to: Unix timestamps - by _Application_ - 20.01.2015, 11:20
Re: How to: Unix timestamps - by PowerPC603 - 20.01.2015, 13:37
Re: How to: Unix timestamps - by AdamCooper - 07.10.2018, 00:45

Forum Jump:


Users browsing this thread: 1 Guest(s)