Raised This Month: $32 Target: $400
 8% 

Determining hour of the day and day of the week


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
andrept4
Senior Member
Join Date: Dec 2012
Location: Portugal
Old 06-24-2019 , 06:25   Determining hour of the day and day of the week
Reply With Quote #1

Hello, I'm trying to make a function that determines the day of the week and the hour of the day, is this a good way to do it?

Code:
 
void function() {
	
	int dayOfTheWeek = 0;
	int hourOfTheDay = 0;
	int time = GetTime();

	int dcTime = time/86400;
	
	float dcalcTime = dcTime;
	
	if(dcalcTime % 7 < 1/7) {
		dayOfTheWeek = 0;
	} else if(dcalcTime % 7 < 2/7) {
		dayOfTheWeek = 1;
	} else if(dcalcTime % 7 < 3/7) {
		dayOfTheWeek = 2;
	} else if(dcalcTime % 7 < 4/7) {
		dayOfTheWeek = 3;
	} else if(dcalcTime % 7 < 5/7) {
		dayOfTheWeek = 4;
	} else if(dcalcTime % 7 < 6/7) {
		dayOfTheWeek = 5;
	} else {
		dayOfTheWeek = 6;
	}
	
	int hcTime = time/3600;
	
	float hcalcTime = hcTime;
	
	if(hcalcTime % 24 < (1/24)) {
		hourOfTheDay = 0;
	} else if(hcalcTime % 24 < (2/24)) {
		hourOfTheDay = 1;
	} else if(hcalcTime % 24 < (3/24)) {
		hourOfTheDay = 2;
	} else if(hcalcTime % 24 < (4/24)) {
		hourOfTheDay = 3;
	} else if(hcalcTime % 24 < (5/24)) {
		hourOfTheDay = 4;
	} else if(hcalcTime % 24 < (6/24)) {
		hourOfTheDay = 5;
	} else if(hcalcTime % 24 < (7/24)) {
		hourOfTheDay = 6;
	} else if(hcalcTime % 24 < (8/24)) {
		hourOfTheDay = 7;
	} else if(hcalcTime % 24 < (9/24)) {
		hourOfTheDay = 8;
	} else if(hcalcTime % 24 < (10/24)) {
		hourOfTheDay = 9;
	} else if(hcalcTime % 24 < (11/24)) {
		hourOfTheDay = 10;
	} else if(hcalcTime % 24 < (12/24)) {
		hourOfTheDay = 11;
	} else if(hcalcTime % 24 < (13/24)) {
		hourOfTheDay = 12;
	} else if(hcalcTime % 24 < (14/24)) {
		hourOfTheDay = 13;
	} else if(hcalcTime % 24 < (15/24)) {
		hourOfTheDay = 14;
	} else if(hcalcTime % 24 < (16/24)) {
		hourOfTheDay = 15;
	} else if(hcalcTime % 24 < (17/24)) {
		hourOfTheDay = 16;
	} else if(hcalcTime % 24 < (18/24)) {
		hourOfTheDay = 17;
	} else if(hcalcTime % 24 < (19/24)) {
		hourOfTheDay = 18;
	} else if(hcalcTime % 24 < (20/24)) {
		hourOfTheDay = 19;
	} else if(hcalcTime % 24 < (21/24)) {
		hourOfTheDay = 20;
	} else if(hcalcTime % 24 < (22/24)) {
		hourOfTheDay = 21;
	} else if(hcalcTime % 24 < (23/24)) {
		hourOfTheDay = 22;
	} else {
		hourOfTheDay = 23;
	}
}
__________________

Last edited by andrept4; 06-24-2019 at 06:59.
andrept4 is offline
kratoss1812
Senior Member
Join Date: May 2018
Location: Romānia
Old 06-24-2019 , 07:56   Re: Determining hour of the day and day of the week
Reply With Quote #2

ew.

Here is my code for some time clan tag.
PHP Code:
public void OnClientPutInServer(int iClient)
{
    
CreateTimer(1.0FirstTimeriClient);
}

public 
Action FirstTimer(Handle pTimerany iClient)
{
    
char sTime[32];
    
FormatTime(sTimesizeof(sTime), "%I:%M:%S %p"GetTime());
    
    
CS_SetClientClanTag(iClientsTime);
    
CreateTimer(1.0SecondTimeriClient);
}

public 
Action SecondTimer(Handle pTimerany iClient)
{
    
char sTime[32];
    
FormatTime(sTimesizeof(sTime), "%I:%M:%S %p"GetTime());
    
    
CS_SetClientClanTag(iClientsTime);
    
CreateTimer(1.0FirstTimeriClient);

Learn from it.

Make the sTime as you want using this.

Then just use StrContains()
__________________
kratoss1812 is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 06-24-2019 , 08:00   Re: Determining hour of the day and day of the week
Reply With Quote #3

Quote:
Originally Posted by kratoss1812 View Post
ew.

Here is my code for some time clan tag.
eww

PHP Code:
public void OnClientPutInServer(int iClient)
{
    
CreateTimer(1.0FirstTimeriClientTIMER_REPEAT);
}

public 
Action FirstTimer(Handle pTimerany iClient)
{
    if( !
IsClientConnected(iClient) ) return Plugin_Stop;

    
char sTime[32];
    
FormatTime(sTimesizeof(sTime), "%I:%M:%S %p"GetTime());
    
    
CS_SetClientClanTag(iClientsTime);

__________________

Last edited by Silvers; 06-24-2019 at 08:02.
Silvers is offline
andrept4
Senior Member
Join Date: Dec 2012
Location: Portugal
Old 06-24-2019 , 08:06   Re: Determining hour of the day and day of the week
Reply With Quote #4

Could you guys explain to me what does that do?
__________________
andrept4 is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 06-24-2019 , 09:16   Re: Determining hour of the day and day of the week
Reply With Quote #5

Quote:
Originally Posted by andrept4 View Post
Could you guys explain to me what does that do?
These are the lines that will help you get the time and day you want.

PHP Code:
char sTime[32];
FormatTime(sTimesizeof(sTime), "%I:%M:%S %p"GetTime()); 
Here's a page where you can find other format specifiers: http://www.opengroup.org/onlinepubs/.../strftime.html
__________________
Psyk0tik is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-24-2019 , 09:18   Re: Determining hour of the day and day of the week
Reply With Quote #6

Quote:
Originally Posted by Silvers View Post
eww

PHP Code:
public void OnClientPutInServer(int iClient)
{
    
CreateTimer(1.0FirstTimeriClientTIMER_REPEAT);
}

public 
Action FirstTimer(Handle pTimerany iClient)
{
    if( !
IsClientConnected(iClient) ) return Plugin_Stop;

    
char sTime[32];
    
FormatTime(sTimesizeof(sTime), "%I:%M:%S %p"GetTime());
    
    
CS_SetClientClanTag(iClientsTime);



eww

Assuming GetTime() acknowledges your server's timezone, this should be the absolutely most efficient way:

new HourOfTheDay = (GetTime() % 86400) / 3600

Also, check it to make sure this formula isn't wrong. I think it is right.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 06-24-2019 at 09:21.
eyal282 is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 06-24-2019 , 09:40   Re: Determining hour of the day and day of the week
Reply With Quote #7

Quote:
Originally Posted by eyal282 View Post
eww.
I was showing how the double timer was stupid. He already told the OP where to look and to learn from it.

Quote:
Originally Posted by kratoss1812 View Post
ew.

Here is my code for some time clan tag.

Learn from it.

Make the sTime as you want using this.
__________________
Silvers is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 06-24-2019 , 09:45   Re: Determining hour of the day and day of the week
Reply With Quote #8

Quote:
Originally Posted by eyal282 View Post
eww

Assuming GetTime() acknowledges your server's timezone, this should be the absolutely most efficient way:

new HourOfTheDay = (GetTime() % 86400) / 3600

Also, check it to make sure this formula isn't wrong. I think it is right.
UNIX timestamps don't include timezone information and is always the number of seconds since 1970 January 1 0:00 UTC.

The current UNIX time immediately before I posted this is 1561383914
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 06-24-2019 at 09:47.
Powerlord is offline
andrept4
Senior Member
Join Date: Dec 2012
Location: Portugal
Old 06-24-2019 , 16:42   Re: Determining hour of the day and day of the week
Reply With Quote #9

Thank you guys for all the relpies! After some work I finally got how it works! One more question, does GetEngineTime() work instead of GetTime() on FormatTime()?
__________________
andrept4 is offline
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 04:32.


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