SA-MP Forums Archive
any way? - 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: any way? (/showthread.php?tid=282121)



any way? - Gh0sT_ - 09.09.2011

Hi everyone, how can I optimizate this? This function converts timestamp to date:

pawn Код:
stock date(timestamp)
{
    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 returnstring[21];
    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;
    format(returnstring, 21, "%02d/%02d/%02d %02d:%02d:%02d", year, month + 1, day + 1, hour, mins, sec);
    return returnstring;
}
To get timestamp, Im using ****** function:

pawn Код:
stock mktime(hour, minute, second, day, month, year)
{
    static const days_of_month[12] =
    {
        0 * 86400, 31 * 86400, 59 * 86400,
        90 * 86400, 120 * 86400, 151 * 86400,
        181 * 86400, 212 * 86400, 243 * 86400,
        273 * 86400, 304 * 86400, 334 * 86400
    };
    static
        lMonth, lYear, lMonthS, lYearS;
    if (year != lYear)
    {
        year -= 1970;
        lYearS = year * 31536000;
        lYearS += 86400 * (year / 4);
        year += 70;
        lYearS -= (year / 100) * 86400;
        year += 300;
        lYearS += (year / 400) * 86400;
        year += 1600;
        lYear = year;
    }
    if (month != lMonth)
    {
        --month;
        lMonthS = days_of_month[month];
        if (IsLeapYear(year) && month >= 2)
        {
            lMonthS += 86400;
        }
        lMonth = month + 1;
    }
    day = (day - 1) * 86400;
    hour = (hour - (0 TIME_ZONE)) * 3600;
    minute = minute * 60;
    return lYearS + lMonthS + day + hour + minute + second;
}



Re: any way? - JaTochNietDan - 09.09.2011

Here's a function I wrote a while back that you may find useful:

pawn Код:
/*Usage: The seconds parameter should be the amount of seconds you want to convert and
the rest of the parameters should be variables to store the integer value for each of the
time values*/

stock formatSeconds(seconds, &hours_left, &minutes_left, &seconds_left)
{
    hours_left = seconds/60/60;
    minutes_left = (seconds - hours_left*60*60)/60;
    seconds_left = (seconds - hours_left*60*60 - minutes_left*60);
    return 1;
}
Usage example:

pawn Код:
new
    hours,
    minutes,
    seconds;
   
formatSeconds(5000, hours, minutes, seconds);
   
printf("Hours: %d, Minutes: %d, Seconds: %d in 5000 seconds", hours, minutes, seconds);
You should be able to edit that to your needs!


Re: any way? - Gh0sT_ - 09.09.2011

It's similiar to that I want, but not so much. Since I dont saw any other same scripts, I don't know, how to create a function which will convert timestamp to date(YY-DD-MM HH:MM). I just want to know, is there are a way to optimizate this function(date) or even possible.