1 | // LAF Base Library |
2 | // Copyright (c) 2021 Igara Studio S.A. |
3 | // Copyright (c) 2001-2018 David Capello |
4 | // |
5 | // This file is released under the terms of the MIT license. |
6 | // Read LICENSE.txt for more information. |
7 | |
8 | #ifndef BASE_TIME_H_INCLUDED |
9 | #define BASE_TIME_H_INCLUDED |
10 | #pragma once |
11 | |
12 | #include "base/ints.h" |
13 | |
14 | #include <ctime> |
15 | #include <string> |
16 | |
17 | namespace base { |
18 | |
19 | // ~1000 per second |
20 | typedef uint64_t tick_t; |
21 | |
22 | class Time { |
23 | public: |
24 | int year, month, day; |
25 | int hour, minute, second; |
26 | |
27 | Time(int year = 0, int month = 0, int day = 0, |
28 | int hour = 0, int minute = 0, int second = 0) |
29 | : year(year), month(month), day(day) |
30 | , hour(hour), minute(minute), second(second) { |
31 | } |
32 | |
33 | bool valid() const { |
34 | return (year != 0 && month != 0 && day != 0); |
35 | } |
36 | |
37 | void dateOnly() { |
38 | hour = minute = second = 0; |
39 | } |
40 | |
41 | Time& addSeconds(const int seconds); |
42 | Time& addMinutes(const int minutes) { |
43 | return addSeconds(minutes*60); |
44 | } |
45 | Time& addHours(const int hours) { |
46 | return addSeconds(hours*60*60); |
47 | } |
48 | Time& addDays(const int days) { |
49 | return addSeconds(days*24*60*60); |
50 | } |
51 | |
52 | bool operator==(const Time& other) const { |
53 | return |
54 | year == other.year && |
55 | month == other.month && |
56 | day == other.day && |
57 | hour == other.hour && |
58 | minute == other.minute && |
59 | second == other.second; |
60 | } |
61 | |
62 | bool operator!=(const Time& other) const { |
63 | return !operator==(other); |
64 | } |
65 | |
66 | bool operator<(const Time& other) const; |
67 | }; |
68 | |
69 | bool safe_localtime(const std::time_t time, std::tm* result); |
70 | |
71 | Time current_time(); |
72 | tick_t current_tick(); |
73 | |
74 | } |
75 | |
76 | #endif |
77 | |