SA-MP Forums Archive
TimestampToDate function - two days too far - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: TimestampToDate function - two days too far (/showthread.php?tid=358817)



TimestampToDate function - two days too far - Jochemd - 11.07.2012

Hello,

If I use my TimestampToDate function to calculate the current date from the current timestamp, it will give the exact date and time, except two days too much. So today it would give 6-7-2012. I don't see anything wrong in my code. Read it down here.

pawn Код:
/*
-               Timestamp To Date converter             -
-                    Made by Jochemd                    -
-          http://forum.sa-mp.com/member.php?u=580      -
*/



#include <a_samp>

new MonthTimes[12][4] =
{
    { 31, 31, 2678400, 2678400 },
    { 28, 29, 2419200, 2505600 },
    { 31, 31, 2678400, 2678400 },
    { 30, 30, 2592000, 2592000 },
    { 31, 31, 2678400, 2678400 },
    { 30, 30, 2592000, 2592000 },
    { 31, 31, 2678400, 2678400 },
    { 31, 31, 2678400, 2678400 },
    { 30, 30, 2592000, 2592000 },
    { 31, 31, 2678400, 2678400 },
    { 30, 30, 2592000, 2592000 },
    { 31, 31, 2678400, 2678400 }
};

stock IsLeapYear(year)
{
    if(year % 4 == 0) return 1;
    else return 0;
}

stock TimestampToDate(Timestamp, &year, &month, &day, &hour, &minute, &second, HourGMT, MinuteGMT = 0)
{
    new tmp = 2;
    year = 1970,
    month = 1;
    for(;;)
    {
        if(Timestamp >= 31536000)
        {
            Timestamp -= 31536000;
            year ++;
            tmp ++;
            if(tmp == 4)
            {
                if(Timestamp >= 31622400)
                {
                    tmp = 0;
                    year ++;
                    Timestamp -= 31622400;
                }
                else break;
            }
        }
        else break;
    }
    if(IsLeapYear(year)) tmp = 3;
    else tmp = 2;
    for(new i = 0; i < 12; i ++)
    {
        if(Timestamp >= MonthTimes[i][tmp])
        {
            month ++;
            Timestamp -= MonthTimes[i][tmp];
        }
        else break;
       
    }
    day = 1 + (Timestamp / 86400);
    Timestamp %= 86400;
    hour = HourGMT + (Timestamp / 3600);
    Timestamp %= 3600;
    minute = MinuteGMT + (Timestamp / 60);
    second = (Timestamp % 60);
    if(minute > 59)
    {
        minute = 0;
        hour ++;
        if(hour > 23)
        {
            hour -= 24;
            day ++;
            if(day > MonthTimes[month][IsLeapYear(year)])
            {
                day = 1;
                month ++;
                if(month > 12)
                {
                    month = 1;
                    year ++;
                }
            }
        }
    }
    return 1;
}
I don't see anything wrong but maybe you do. Any idea?

Jochem