Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
TimeUtils.h
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
6#ifndef SKYFIRE_PLATFORM_TIME_H
7#define SKYFIRE_PLATFORM_TIME_H
8
9#include "Define.h"
10
11#include <chrono>
12#include <ctime>
13#include <thread>
14
15namespace Skyfire
16{
18 {
19 using Clock = std::chrono::steady_clock;
20 static const Clock::time_point ApplicationStartTime = Clock::now();
21 return uint32(std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - ApplicationStartTime).count());
22 }
23
24 inline uint32 GetMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
25 {
26 if (oldMSTime > newMSTime)
27 return (0xFFFFFFFF - oldMSTime) + newMSTime;
28
29 return newMSTime - oldMSTime;
30 }
31
33 {
34 return GetMSTimeDiff(oldMSTime, GetMSTime());
35 }
36
37 inline void SleepForMicroseconds(uint32 microseconds)
38 {
39 std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
40 }
41
42 inline void SleepForMilliseconds(uint32 milliseconds)
43 {
44 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
45 }
46
47 inline void SleepForSeconds(uint32 seconds)
48 {
49 std::this_thread::sleep_for(std::chrono::seconds(seconds));
50 }
51
52 inline void SleepFor(uint32 milliseconds)
53 {
54 SleepForMilliseconds(milliseconds);
55 }
56
57 inline bool LocalTime(time_t const& time, tm& result)
58 {
59#if PLATFORM == PLATFORM_WINDOWS
60 return localtime_s(&result, &time) == 0;
61#else
62 return localtime_r(&time, &result) != nullptr;
63#endif
64 }
65}
66
67#endif
std::uint32_t uint32
Definition Define.h:77
uint32 GetMSTime()
Definition TimeUtils.h:17
uint32 GetMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition TimeUtils.h:24
bool LocalTime(time_t const &time, tm &result)
Definition TimeUtils.h:57
void SleepForMicroseconds(uint32 microseconds)
Definition TimeUtils.h:37
void SleepFor(uint32 milliseconds)
Definition TimeUtils.h:52
void SleepForSeconds(uint32 seconds)
Definition TimeUtils.h:47
void SleepForMilliseconds(uint32 milliseconds)
Definition TimeUtils.h:42
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition TimeUtils.h:32