AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   GetTime offset relative to server's timezone (https://forums.alliedmods.net/showthread.php?t=327447)

eyal282 09-19-2020 06:10

GetTime offset relative to server's timezone
 
This stock allows you to get the amount of seconds the server is before or after unix. I used it to make offsets to use GetTime in a way that works around the end of the day, 00:00. Hopefully it will be useful to you.

Code:

stock int GetTimeOffset()
{
        bool Negate = false;
        char Time[11];

        FormatTime(Time, sizeof(Time), "%A", 86400);
       
        if(StrEqual(Time, "Thursday")) // 86400 in unix is Friday, meaning the timezone brought us back a bit.
                Negate = true;
               
        FormatTime(Time, sizeof(Time), "%H %M", 86400); // Can't use 0 because of negatives I think. I assume.
       
        char sHour[4], sMinute[4];
       
        int pos = BreakString(Time, sHour, sizeof(sHour));
       
        FormatEx(sMinute, sizeof(sMinute), Time[pos]);
       
        int secondstotal = StringToInt(sHour) * 3600 + StringToInt(sMinute) * 60;
       
        if(secondstotal == 0 || !Negate) // Server's time is positive or zero ( unix time )
                return secondstotal;
               
        else
        {
                /*
                Suppose an hour negative, turns 00:00 into 23:00. The result should be -3600.
                -1 * (86400 - (23 * 3600 + 0 * 60)) = -3600
                */
                return -1 * (86400 - secondstotal);
        }
}


Bacardi 10-06-2020 13:30

Re: GetTime offset relative to server's timezone
 
How about, this way ?

PHP Code:

#include <sdktools>

public void OnPluginStart()
{
    
RegConsoleCmd("sm_test"test);
}

public 
Action test(int clientint args)
{
    
char buffer[60];
    
FormatTime(buffersizeof(buffer), "%z");

    
// Example "%Z", output: "FLE Daylight Time"
    // **** Standard Time = disabled, Daylight Saving Time (DST)
    // **** Daylight Time = enabled, Daylight Saving Time (DST)

    
int offset StringToInt(buffer);

    
int minutes offset 100// 0 - 99
    
int hours offset 100;
    
    
offset = (hours*3600) + (minutes*60);
    
    
PrintToServer("FormatTime UTC%s, offset %i seconds"bufferoffset);


    return 
Plugin_Handled;


*edit
And... GetTime() returns UNIX time, which is UTC-0.
But FormatTime returns with timezone+dts.


All times are GMT -4. The time now is 22:16.

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