1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #include <cmajor/rt/Time.hpp>
 7 #include <chrono>
 8 #include <thread>
 9 #include <ctime>
10 
11 int64_t RtNow()
12 {
13     return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
14 }
15 
16 void RtSleep(int64_t nanoseconds)
17 {
18     std::chrono::nanoseconds duration{ nanoseconds };
19     std::this_thread::sleep_for(duration);
20 }
21 
22 void RtGetCurrentDate(int16_t* yearint8_t* monthint8_t* day)
23 {
24     std::time_t currentTime;
25     std::time(&currentTime);
26     struct tm* localTime = nullptr;
27     localTime = std::localtime(&currentTime);
28     *year = 1900 + localTime->tm_year;
29     *month = 1 + localTime->tm_mon;
30     *day = localTime->tm_mday;
31 }
32 
33 void RtGetCurrentDateTime(int16_t* yearint8_t* monthint8_t* dayint32_t* secs)
34 {
35     std::time_t currentTime;
36     std::time(&currentTime);
37     struct tm* localTime = nullptr;
38     localTime = std::localtime(&currentTime);
39     *year = 1900 + localTime->tm_year;
40     *month = 1 + localTime->tm_mon;
41     *day = localTime->tm_mday;
42     *secs = localTime->tm_hour * 3600 + localTime->tm_min * 60 + localTime->tm_sec;
43 }
44 
45 int64_t RtGetCurrentTime()
46 {
47     std::time_t currentTime;
48     std::time(&currentTime);
49     return currentTime;
50 }