Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
ServerRestartSchedule.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
7
9
10#include <cctype>
11
12namespace
13{
14 constexpr uint32 SecondsPerMinute = 60;
15 constexpr uint32 SecondsPerHour = 60 * SecondsPerMinute;
16 constexpr uint32 SecondsPerDay = 24 * SecondsPerHour;
17
18 bool IsDigit(char value)
19 {
20 return std::isdigit(static_cast<unsigned char>(value)) != 0;
21 }
22
23 bool ParseTwoDigits(char const*& cursor, uint32& value)
24 {
25 if (!IsDigit(cursor[0]) || !IsDigit(cursor[1]))
26 return false;
27
28 value = uint32(cursor[0] - '0') * 10 + uint32(cursor[1] - '0');
29 cursor += 2;
30 return true;
31 }
32}
33
34bool Skyfire::ServerRestartSchedule::ParseTimeOfDay(char const* text, uint32& secondsOfDay)
35{
36 secondsOfDay = 0;
37
38 if (!text || !*text || !IsDigit(*text))
39 return false;
40
41 char const* cursor = text;
42 uint32 hour = uint32(*cursor - '0');
43 ++cursor;
44
45 if (IsDigit(*cursor))
46 {
47 hour = hour * 10 + uint32(*cursor - '0');
48 ++cursor;
49 }
50
51 if (*cursor != ':')
52 return false;
53
54 ++cursor;
55
56 uint32 minute = 0;
57 if (!ParseTwoDigits(cursor, minute))
58 return false;
59
60 uint32 second = 0;
61 if (*cursor == ':')
62 {
63 ++cursor;
64
65 if (!ParseTwoDigits(cursor, second))
66 return false;
67 }
68
69 if (*cursor != '\0' || hour > 23 || minute > 59 || second > 59)
70 return false;
71
72 secondsOfDay = hour * SecondsPerHour + minute * SecondsPerMinute + second;
73 return true;
74}
75
76bool Skyfire::ServerRestartSchedule::CalculateRestartDelay(char const* text, uint32 currentSecondsOfDay, uint32& delaySeconds)
77{
78 delaySeconds = 0;
79
80 if (currentSecondsOfDay >= SecondsPerDay)
81 return false;
82
83 uint32 targetSecondsOfDay = 0;
84 if (!ParseTimeOfDay(text, targetSecondsOfDay))
85 return false;
86
87 delaySeconds = targetSecondsOfDay >= currentSecondsOfDay
88 ? targetSecondsOfDay - currentSecondsOfDay
89 : SecondsPerDay - currentSecondsOfDay + targetSecondsOfDay;
90
91 return true;
92}
93
94bool Skyfire::ServerRestartSchedule::CalculateRestartDelay(char const* text, time_t now, uint32& delaySeconds)
95{
96 tm localTime;
97 if (!Skyfire::LocalTime(now, localTime))
98 return false;
99
100 uint32 currentSecondsOfDay = uint32(localTime.tm_hour) * SecondsPerHour
101 + uint32(localTime.tm_min) * SecondsPerMinute
102 + uint32(localTime.tm_sec);
103
104 return CalculateRestartDelay(text, currentSecondsOfDay, delaySeconds);
105}
106
108{
109 return CalculateRestartDelay(text, time(NULL), delaySeconds);
110}
std::uint32_t uint32
Definition Define.h:77
bool ParseTimeOfDay(char const *text, uint32 &secondsOfDay)
bool CalculateRestartDelay(char const *text, uint32 currentSecondsOfDay, uint32 &delaySeconds)
bool LocalTime(time_t const &time, tm &result)
Definition TimeUtils.h:57