AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [STOCK] DateToTimeStamp converter (https://forums.alliedmods.net/showthread.php?t=209597)

Despirator 02-27-2013 12:24

[STOCK] DateToTimeStamp converter
 
2 Attachment(s)
provides stock to convert a date to seconds since Unix Epoch (01.01.1970)

attached test plugin
command to test "date_test" - only from server console or rcon

PHP Code:

stock DateToTimeStamp(daymonthyearhourminuteseconds)
{
    return 
YearToSeconds(year) + MonthToSeconds(yearmonth) + DaysToSeconds(day) + TimeToSeconds(hourminuteseconds) - 21600// Why it returns more by 21600?
}

stock YearToSeconds(year)
{
    new 
var1 year 4;
    new 
var2 year var1;
    
    new 
var3 var1 492;
    new 
var4 var2 1478;
    
    
var3 *= 31622400;
    
var4 *= 31536000;
    
    return 
var3 var4;
}

// 31 days = 2678400 seconds
// 30 days = 2592000
// 29 days = 2505600
// 28 days = 2419200

stock MonthToSeconds(yearmonth)
{
    
month -= 1;
    
    new 
result;
    
    switch (
month)
    {
        case 
1357810 :
        {
            
result 2678400;
        }
        case 
:
        {
            if (
year == 0)
            {
                
result 2505600;
            }
            else
            {
                
result 2419200;
            }
        }
        case 
46911 :
        {
            
result 2592000;
        }
    }
    
    if (
month 1)
    {
        
result += MonthToSeconds(yearmonth);
    }
    
    return 
result;
}

stock DaysToSeconds(days)
{
    
days-=1;
    return 
days 86400;
}

stock TimeToSeconds(hourminuteseconds)
{
    return 
hour 3600 minute 60 seconds;



Powerlord 02-27-2013 16:36

Re: [STOCK] DateToTimeStamp converter
 
It'd be better if SourceMod just had a native to call the C library's strptime function much like FormatTime calls the C library's strftime function.

Despirator 02-27-2013 23:25

Re: [STOCK] DateToTimeStamp converter
 
i have already requested it yesterday https://bugs.alliedmods.net/show_bug.cgi?id=5635


All times are GMT -4. The time now is 18:30.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.