Euphoria
datetime.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ctime>
4 
5 #include <cstdint>
6 
7 #include "base/ints.h"
8 
9 
10 namespace eu::core
11 {
12  struct TimetWrapper;
13  struct StructTmWrapper;
14 
15  enum class Month
16  {
18  april, may, june,
21  };
22 
23  enum class DstInfo
24  {
26  };
27 
28  enum class TimeZone
29  {
30  gmt, local
31  };
32 
33  struct TimetWrapper
34  {
35  time_t time;
36 
37  explicit TimetWrapper(time_t time);
38 
40  static TimetWrapper from_gmt(const StructTmWrapper& dt);
42 
43  [[nodiscard]] StructTmWrapper to_local_time() const;
44  [[nodiscard]] StructTmWrapper to_gmt() const;
45 
46  static double get_difference(const TimetWrapper& start, const TimetWrapper& end);
47  };
48 
49 
51  {
52  struct tm time;
53 
54  explicit StructTmWrapper(struct tm time);
55  StructTmWrapper(int year, core::Month month, int day);
56  StructTmWrapper(int year, core::Month month, int day, int hour, int minute, int second, bool dst=false);
57 
58  void set_seconds(int seconds);
59  void set_minutes(int minutes);
60  void set_hour(int hour);
61  void set_day_of_moth(int day_of_moth);
62  void set_month(core::Month month);
63  void set_year(int year);
64  void set_dst(DstInfo dst);
65 
66  [[nodiscard]] int get_seconds() const;
67  [[nodiscard]] int get_minutes() const;
68  [[nodiscard]] int get_hour() const;
69  [[nodiscard]] int get_day_of_moth() const;
70  [[nodiscard]] Month get_month() const;
71  [[nodiscard]] int get_year() const;
72  [[nodiscard]] DstInfo get_dst() const;
73 
74  [[nodiscard]] std::string to_debug_string() const;
75  };
76 
77  // unix date time format, 64 bit
78  // todo(Gustav): test 2038 problem
79  uint64_t c_date_time_to_int64(const TimetWrapper& dt);
81 
82  // public interface
83  // util class to make stuff nice to use
84  struct DateTime
85  {
86  public:
87  DateTime() = delete;
88 
89  static DateTime create_from_date(int year, core::Month month, int day, TimeZone timezone = TimeZone::local);
90  static DateTime create_from_date_and_time(int year, core::Month month, int day, int hour, int minute, int second, TimeZone timezone = TimeZone::local);
92 
93  void set_seconds(int seconds);
94  void set_minutes(int minutes);
95  void set_hour(int hour);
96  void set_day_of_moth(int day_of_moth);
97  void set_month(core::Month month);
98  void set_year(int year);
99  void set_dst(DstInfo dst);
100 
101  [[nodiscard]] std::string to_debug_string() const;
102  [[nodiscard]] int get_seconds() const;
103  [[nodiscard]] int get_minutes() const;
104  [[nodiscard]] int get_hour() const;
105  [[nodiscard]] int get_day_of_month() const;
106  [[nodiscard]] Month get_month() const;
107  [[nodiscard]] int get_year() const;
108  [[nodiscard]] DstInfo get_dst() const;
109 
110  [[nodiscard]] TimeZone get_timezone() const;
111  [[nodiscard]] TimetWrapper get_time() const;
112 
113  private:
114  TimeZone timezone;
115  TimetWrapper time;
116 
117  DateTime(TimeZone timezone, const StructTmWrapper& time);
118  DateTime(TimeZone timezone, const TimetWrapper& time);
119 
120  void update_time(const StructTmWrapper& s);
121 
122  [[nodiscard]] StructTmWrapper as_struct() const;
123  };
124 
125 }
TimetWrapper c_int64_to_date_time(U64 total_seconds)
Definition: datetime.cc:293
U64 c_date_time_to_int64(const TimetWrapper &dt)
Definition: datetime.cc:282
TimetWrapper get_time() const
Definition: datetime.cc:466
void set_month(core::Month month)
Definition: datetime.cc:383
void set_hour(int hour)
Definition: datetime.cc:365
static DateTime create_from_date_and_time(int year, core::Month month, int day, int hour, int minute, int second, TimeZone timezone=TimeZone::local)
Definition: datetime.cc:326
int get_hour() const
Definition: datetime.cc:424
std::string to_debug_string() const
Definition: datetime.cc:340
void set_seconds(int seconds)
Definition: datetime.cc:347
int get_year() const
Definition: datetime.cc:445
void set_minutes(int minutes)
Definition: datetime.cc:356
static DateTime create_from_current_time(TimeZone timezone=TimeZone::local)
Definition: datetime.cc:333
void set_year(int year)
Definition: datetime.cc:392
int get_minutes() const
Definition: datetime.cc:417
static DateTime create_from_date(int year, core::Month month, int day, TimeZone timezone=TimeZone::local)
Definition: datetime.cc:319
void set_day_of_moth(int day_of_moth)
Definition: datetime.cc:374
DstInfo get_dst() const
Definition: datetime.cc:452
Month get_month() const
Definition: datetime.cc:438
void set_dst(DstInfo dst)
Definition: datetime.cc:401
int get_day_of_month() const
Definition: datetime.cc:431
int get_seconds() const
Definition: datetime.cc:410
TimeZone get_timezone() const
Definition: datetime.cc:459
DstInfo get_dst() const
Definition: datetime.cc:225
void set_seconds(int seconds)
Definition: datetime.cc:123
Month get_month() const
Definition: datetime.cc:212
void set_year(int year)
Definition: datetime.cc:158
void set_hour(int hour)
Definition: datetime.cc:137
void set_month(core::Month month)
Definition: datetime.cc:151
void set_day_of_moth(int day_of_moth)
Definition: datetime.cc:144
void set_dst(DstInfo dst)
Definition: datetime.cc:165
std::string to_debug_string() const
Definition: datetime.cc:243
void set_minutes(int minutes)
Definition: datetime.cc:130
StructTmWrapper(struct tm time)
Definition: datetime.cc:89
int get_day_of_moth() const
Definition: datetime.cc:205
StructTmWrapper to_local_time() const
Definition: datetime.cc:73
static TimetWrapper from_current_time()
Definition: datetime.cc:59
static TimetWrapper from_local_time(const StructTmWrapper &dt)
Definition: datetime.cc:43
static TimetWrapper from_gmt(const StructTmWrapper &dt)
Definition: datetime.cc:51
static double get_difference(const TimetWrapper &start, const TimetWrapper &end)
Definition: datetime.cc:66
TimetWrapper(time_t time)
Definition: datetime.cc:36
StructTmWrapper to_gmt() const
Definition: datetime.cc:80