datetime.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib 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 ClanLib 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 
32 
33 #pragma once
34 
35 #include "../api_core.h"
36 #include "../System/cl_platform.h"
37 
38 namespace clan
39 {
42 
44 class CL_API_CORE DateTime
45 {
48 public:
49  enum TimeZone
50  {
52  utc_timezone
53  };
54 
56  DateTime();
57  DateTime(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone=utc_timezone);
58  ~DateTime();
59 
61  static DateTime get_current_local_time();
62 
64  static DateTime get_current_utc_time();
65 
67  static DateTime get_local_time_from_ticks(byte64 ticks);
68 
70  static DateTime get_utc_time_from_ticks(byte64 ticks);
71 
72  static DateTime from_short_date_string(const std::string &value);
73 
75 
78 public:
79  bool is_null() const;
80  unsigned short get_year() const;
81 
85  unsigned char get_month() const;
86  unsigned char get_day() const;
87  unsigned char get_hour() const;
88  unsigned char get_minutes() const;
89  unsigned char get_seconds() const;
90  unsigned int get_nanoseconds() const;
91  TimeZone get_timezone() const;
92 
96  unsigned char get_week() const;
97 
101  int get_difference_in_days(const DateTime &other) const;
102 
106  unsigned int get_day_of_week() const;
107 
111  static int get_days_in_month(int month, int year);
113 
116 public:
117  void set_null();
118  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);
119  void set_year(int year);
120  void set_month(int month);
121  void set_day(int day);
122  void set_hour(int hour);
123  void set_minutes(int minutes);
124  void set_seconds(int seconds);
125  void set_nanoseconds(int nanoseconds);
126  void set_timezone(TimeZone timezone);
127 
128  DateTime &add_years(int years);
129  DateTime &add_days(int days);
130  DateTime &add_months(int months);
131 /*
132  void add_hours(int hours);
133  void add_minutes(int minutes);
134  void add_seconds(int seconds);
135  void add_nanoseconds(int nanoseconds);
136 */
137  DateTime to_utc() const;
138  DateTime to_local() const;
139 
141  byte64 to_ticks() const;
142 
144  std::string to_long_date_string() const;
145 
147  std::string to_short_date_string() const;
148 
150  std::string to_short_datetime_string() const;
151 
153  std::string to_long_time_string() const;
154 
156  std::string to_short_time_string() const;
157 
159  std::string to_string() const;
160 
161  bool operator <(const DateTime &other) const;
162  bool operator <=(const DateTime &other) const;
163  bool operator >(const DateTime &other) const;
164  bool operator >=(const DateTime &other) const;
165  bool operator ==(const DateTime &other) const;
166  bool operator !=(const DateTime &other) const;
168 
171 private:
172  void throw_if_invalid_date(int year, int month, int day, int hour, int minute, int seconds, int nanoseconds) const;
173  void throw_if_null() const;
174 
175  int get_day_number() const;
176  void set_date_from_daynumber(int g);
177 
178  unsigned short year;
179  unsigned char month;
180  unsigned char day;
181  unsigned char hour;
182  unsigned char minute;
183  unsigned char seconds;
184  unsigned int nanoseconds;
185 
186  TimeZone timezone;
187  static const byte64 ticks_from_1601_to_1900;
189 };
190 
191 }
192 
Date/Time class.
Definition: datetime.h:44
TimeZone
Definition: datetime.h:49
long long byte64
Definition: cl_platform.h:65
Definition: datetime.h:51