datetime.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Kenneth Gangstoe
28 ** Harry Storbacka
29 */
30 
31 #pragma once
32 
33 #include <cstdint>
34 
35 namespace uicore
36 {
38  class DateTime
39  {
40  public:
41  enum TimeZone
42  {
45  };
46 
48  DateTime();
49  DateTime(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone = utc_timezone);
50  ~DateTime();
51 
54 
56  static DateTime current_utc_time();
57 
59  static DateTime local_time_from_ticks(int64_t ticks);
60 
62  static DateTime utc_time_from_ticks(int64_t ticks);
63 
64  static DateTime from_short_date_string(const std::string &value);
65 
66  bool is_null() const;
67  unsigned short year() const;
68 
72  unsigned char month() const;
73  unsigned char day() const;
74  unsigned char hour() const;
75  unsigned char minutes() const;
76  unsigned char seconds() const;
77  unsigned int nanoseconds() const;
78  TimeZone timezone() const;
79 
83  unsigned char week() const;
84 
88  int difference_in_days(const DateTime &other) const;
89 
93  unsigned int day_of_week() const;
94 
98  static int days_in_month(int month, int year);
99 
100  void set_null();
101  void set_date(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone = utc_timezone);
102  void set_year(int year);
103  void set_month(int month);
104  void set_day(int day);
105  void set_hour(int hour);
106  void set_minutes(int minutes);
107  void set_seconds(int seconds);
108  void set_nanoseconds(int nanoseconds);
110 
111  DateTime &add_years(int years);
112  DateTime &add_days(int days);
113  DateTime &add_months(int months);
114  /*
115  void add_hours(int hours);
116  void add_minutes(int minutes);
117  void add_seconds(int seconds);
118  void add_nanoseconds(int nanoseconds);
119  */
120  DateTime to_utc() const;
121  DateTime to_local() const;
122 
124  int64_t to_ticks() const;
125 
128 
131 
134 
137 
140 
142  std::string to_string() const;
143 
144  bool operator <(const DateTime &other) const;
145  bool operator <=(const DateTime &other) const;
146  bool operator >(const DateTime &other) const;
147  bool operator >=(const DateTime &other) const;
148  bool operator ==(const DateTime &other) const;
149  bool operator !=(const DateTime &other) const;
150 
151  private:
152  void throw_if_invalid_date(int year, int month, int day, int hour, int minute, int seconds, int nanoseconds) const;
153  void throw_if_null() const;
154 
155  int day_number() const;
156  void set_date_from_daynumber(int g);
157 
158  unsigned short _year;
159  unsigned char _month;
160  unsigned char _day;
161  unsigned char _hour;
162  unsigned char _minute;
163  unsigned char _seconds;
164  unsigned int _nanoseconds;
165 
166  TimeZone _timezone;
167  static const int64_t ticks_from_1601_to_1900;
168  };
169 }
static DateTime current_utc_time()
Get current system time in UTC.
static DateTime utc_time_from_ticks(int64_t ticks)
Converts a time tick value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a date t...
void set_month(int month)
TimeZone timezone() const
std::string to_long_time_string() const
hh:mm:ss
DateTime & add_days(int days)
int difference_in_days(const DateTime &other) const
Returns the difference in days between two dates. This function is only accurate for the next few mil...
std::string to_short_date_string() const
yyyy-mm-dd
void set_nanoseconds(int nanoseconds)
Definition: datetime.h:43
std::string to_short_time_string() const
hh:mm
static DateTime current_local_time()
Get current system time in local time zone.
void set_timezone(TimeZone timezone)
unsigned char seconds() const
TimeZone
Definition: datetime.h:41
void set_day(int day)
unsigned char minutes() const
unsigned int nanoseconds() const
DateTime & add_months(int months)
DateTime to_utc() const
unsigned int day_of_week() const
Get the day of the week.
static int days_in_month(int month, int year)
Returns the number of days in the given month.
bool operator!=(const DateTime &other) const
std::string to_string() const
Mon Feb 3 12:32:54 2008.
void set_hour(int hour)
bool is_null() const
DateTime & add_years(int years)
unsigned short year() const
static DateTime from_short_date_string(const std::string &value)
bool operator==(const DateTime &other) const
DateTime to_local() const
void set_minutes(int minutes)
static DateTime local_time_from_ticks(int64_t ticks)
Converts a time tick value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a date t...
unsigned char day() const
std::string to_short_datetime_string() const
yyyy-mm-dd hh:mm:ss
bool operator>(const DateTime &other) const
unsigned char month() const
Returns the month number in range 1-12.
bool operator<=(const DateTime &other) const
void set_year(int year)
std::string to_long_date_string() const
Mon Mar 3 2007.
Definition: datetime.h:44
void set_seconds(int seconds)
DateTime()
Constructs a date/time object.
int64_t to_ticks() const
Converts the date to the number of 100-nanosecond intervals since January 1, 1601 UTC...
unsigned char hour() const
Definition: Application/application.h:35
bool operator<(const DateTime &other) const
bool operator>=(const DateTime &other) const
Date/Time class.
Definition: datetime.h:38
void set_date(int year, int month, int day, int hour=0, int minute=0, int seconds=0, int nanoseconds=0, TimeZone timezone=utc_timezone)
unsigned char week() const
Returns the ISO 8601 week number of the date.