xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
dates.h
1 #pragma once
2 #include <chrono>
3 #include <ctime>
4 
5 #if defined (WIN32)
6 static time_t __make_local_date_time(struct tm *tm) noexcept {return mktime(tm);}
7 static time_t __make_utc_date_time(struct tm *tm) noexcept {return _mkgmtime(tm);}
8 #else
9 static time_t __make_local_date_time(struct tm *tm) noexcept {return mktime(tm);}
10 static time_t __make_utc_date_time(struct tm *tm) noexcept {return timegm(tm);}
11 #endif
12 
13 namespace xtd {
14  enum class date_time_kind {
15  utc,
16  local,
17  };
18 
19  enum class day_of_week {
20  sunday,
21  monday,
22  tuesday,
23  wednesday,
24  thursday,
25  friday,
26  saturday
27  };
28 
29  class date_times {
30  public:
31  static std::tm create(date_time_kind kind = date_time_kind::local) noexcept {return create(min(), kind);}
32 
33  static std::tm create(time_t time, date_time_kind kind = date_time_kind::utc) noexcept {return kind == date_time_kind::utc ? to_universal_time(time) : to_local_time(time);}
34 
35  static std::tm create(const tm& time, date_time_kind kind = date_time_kind::utc) noexcept {return kind == date_time_kind::utc ? to_universal_time(time) : to_local_time(time);}
36 
37  static std::tm create(int year, int month, int day, date_time_kind kind = date_time_kind::local) noexcept {
38  std::tm time;
39  time.tm_year = year - 1900;
40  time.tm_mon = month - 1;
41  time.tm_mday = day;
42  time.tm_hour = time.tm_min = time.tm_sec = time.tm_wday = time.tm_yday = 0;
43  time.tm_isdst = -1;
44  time.tm_gmtoff = 0;
45  time.tm_zone = nullptr;
46  return create(time, kind);
47  }
48 
49  static std::tm create(int year, int month, int day, int hour, int minute, int second, date_time_kind kind = date_time_kind::local) noexcept {
50  std::tm time;
51  time.tm_year = year - 1900;
52  time.tm_mon = month - 1;
53  time.tm_mday = day;
54  time.tm_hour = hour;
55  time.tm_min = minute;
56  time.tm_sec = second;
57  time.tm_wday = time.tm_yday = 0;
58  time.tm_isdst = -1;
59  time.tm_gmtoff = 0;
60  time.tm_zone = nullptr;
61  return create(time, kind);
62  }
63 
64  //static std::tm min() noexcept {return to_local_time(static_cast<time_t>(-62135596800));}
65  static std::tm min() noexcept {return to_local_time(static_cast<time_t>(0));}
66  static std::tm max() noexcept {return to_local_time(static_cast<time_t>(253402300799));}
67 
68  static std::tm to_local_time(time_t time) noexcept {return *std::localtime(&time);}
69  static std::tm to_local_time(const std::chrono::system_clock::time_point& time) noexcept {return to_local_time(std::chrono::system_clock::to_time_t(time));}
70  static std::tm to_local_time(std::tm time) noexcept {
71  if (time.tm_zone != nullptr && std::string(time.tm_zone) == "UTC") return to_local_time(__make_utc_date_time(&time));
72  return to_local_time(__make_local_date_time(&time));
73  }
74 
75  static std::tm to_universal_time(time_t time) noexcept {return *std::gmtime(&time);}
76  static std::tm to_universal_time(const std::chrono::system_clock::time_point& time) noexcept {return to_universal_time(std::chrono::system_clock::to_time_t(time));}
77  static std::tm to_universal_time(std::tm time) noexcept {
78  if (time.tm_zone != nullptr && std::string(time.tm_zone) != "UTC") return to_universal_time(__make_local_date_time(&time));
79  return to_universal_time(__make_utc_date_time(&time));
80  }
81 
82  static std::tm now() {return to_local_time(std::chrono::system_clock::now());}
83 
84  static std::tm utc_now() {return to_universal_time(std::chrono::system_clock::now());}
85  };
86 }
87 
88 
89 /*
90 using namespace std;
91 using namespace std::string_literals;
92 using namespace xtd;
93 using namespace xtd::string_literals;
94 
95 // The main entry point for the application.
96 int main() {
97  tm now = xtd::date_times::now();
98  tm utc_now = xtd::date_times::utc_now();
99  tm loc = xtd::date_times::create(2019, 4, 6, 3, 4, 5);
100  tm utc = xtd::date_times::create(2019, 4, 6, 1, 4, 5, xtd::date_time_kind::utc);
101 
102  cout << strings::format("min {0} {0:Z}", xtd::date_times::min()) << endl;
103  cout << strings::format("min {0} {0:Z}", xtd::date_times::to_local_time(xtd::date_times::min())) << endl;
104  cout << strings::format("min {0} {0:Z}", xtd::date_times::to_universal_time(xtd::date_times::min())) << endl;
105  cout << endl;
106  cout << strings::format("max {0} {0:Z}", xtd::date_times::max()) << endl;
107  cout << strings::format("max {0} {0:Z}", xtd::date_times::to_local_time(xtd::date_times::max())) << endl;
108  cout << strings::format("max {0} {0:Z}", xtd::date_times::to_universal_time(xtd::date_times::max())) << endl;
109  cout << endl;
110  cout << strings::format("empty {0} {0:Z}", xtd::date_times::create()) << endl;
111  cout << strings::format("empty {0} {0:Z}", xtd::date_times::create(date_time_kind::local)) << endl;
112  cout << strings::format("empty {0} {0:Z}", xtd::date_times::create(date_time_kind::utc)) << endl;
113  cout << endl;
114  cout << strings::format("now {0} {0:Z}", now) << endl;
115  cout << strings::format("now {0} {0:Z}", xtd::date_times::to_local_time(now)) << endl;
116  cout << strings::format("now {0} {0:Z}", xtd::date_times::to_universal_time(now)) << endl;
117  cout << endl;
118  cout << strings::format("loc {0} {0:Z}", loc) << endl;
119  cout << strings::format("loc {0} {0:Z}", xtd::date_times::to_local_time(loc)) << endl;
120  cout << strings::format("loc {0} {0:Z}", xtd::date_times::to_universal_time(loc)) << endl;
121  cout << endl;
122  cout << strings::format("utc now {0} {0:Z}", utc_now) << endl;
123  cout << strings::format("utc now {0} {0:Z}", xtd::date_times::to_local_time(utc_now)) << endl;
124  cout << strings::format("utc now {0} {0:Z}", xtd::date_times::to_universal_time(utc_now)) << endl;
125  cout << endl;
126  cout << strings::format("utc {0} {0:Z}", utc) << endl;
127  cout << strings::format("utc {0} {0:Z}", xtd::date_times::to_local_time(utc)) << endl;
128  cout << strings::format("utc {0} {0:Z}", xtd::date_times::to_universal_time(utc)) << endl;
129 }
130 
131 */
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Definition: dates.h:29