Raised This Month: $85 Target: $400
 21% 

GetTime offset relative to server's timezone


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 09-19-2020 , 06:10   GetTime offset relative to server's timezone
Reply With Quote #1

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); 
	}
}
__________________
Accepting private work. Contact me if you're interested.
If you like my work, feel free to donate in appreciation with PayPal or CSGO Skins.

Total Donated: 15$
eyal282 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-06-2020 , 13:30   Re: GetTime offset relative to server's timezone
Reply With Quote #2

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.

Last edited by Bacardi; 10-06-2020 at 13:55.
Bacardi is online now
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:58.


Powered by vBulletin®
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Theme made by Freecode