GnuCash  4.8a-134-g214de30c7+
gnc-datetime.hpp
1 /********************************************************************\
2  * gnc-datetime.cpp -- Date and Time classes for GnuCash *
3  * *
4  * Copyright 2015 John Ralls <jralls@ceridwen.us> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 
25 #ifndef __GNC_DATETIME_HPP__
26 #define __GNC_DATETIME_HPP__
27 
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 typedef struct
34 {
35  int year; //1400-9999
36  int month; //1-12
37  int day; //1-31
38 } ymd;
39 
40 enum class DayPart {
41  start, // 00:00 local
42  neutral, // 10:59 UTC
43  end, // 23:59 local
44 };
45 
46 class GncDateTimeImpl;
47 class GncDateImpl;
48 class GncDate;
49 using time64 = int64_t;
50 constexpr const time64 MINTIME = -17987443200;
51 constexpr const time64 MAXTIME = 253402214400;
52 
67 {
68 public:
72  GncDateTime();
78  GncDateTime(const time64 time);
86  GncDateTime(const struct tm tm);
101  GncDateTime(const GncDate& date, DayPart part = DayPart::neutral);
108  GncDateTime(std::string str);
109  ~GncDateTime();
112  void now();
114  explicit operator time64() const;
117  explicit operator struct tm() const;
122  long offset()const;
126  struct tm utc_tm() const;
130  GncDate date() const;
132  bool isnull (void) { return m_impl == nullptr; }
141  std::string format(const char* format) const;
150  std::string format_zulu(const char* format) const;
154  std::string format_iso8601() const;
158  static std::string timestamp();
159 
160 private:
161  std::unique_ptr<GncDateTimeImpl> m_impl;
162 };
163 
175 {
176 public:
182  GncDateFormat (const char* fmt, const char* re) :
183  m_fmt(fmt), m_re(re) {}
185  const std::string m_fmt;
186 private:
190  const std::string m_re;
191 
192  friend class GncDateImpl;
193 };
194 
201 class GncDate
202 {
203  public:
220  static const std::vector<GncDateFormat> c_formats;
223  GncDate();
237  GncDate(int year, int month, int day);
254  GncDate(const std::string str, const std::string fmt);
257  GncDate(std::unique_ptr<GncDateImpl> impl);
260  GncDate(const GncDate&);
263  GncDate(GncDate&&);
266  ~GncDate();
269  GncDate& operator=(const GncDate&);
274  void today();
278  ymd year_month_day() const;
287  std::string format(const char* format);
289  bool isnull (void) { return m_impl == nullptr; }
290 
291 private:
292  std::unique_ptr<GncDateImpl> m_impl;
293 
294  friend GncDateTime::GncDateTime(const GncDate&, DayPart);
295  friend bool operator<(const GncDate&, const GncDate&);
296  friend bool operator>(const GncDate&, const GncDate&);
297  friend bool operator==(const GncDate&, const GncDate&);
298  friend bool operator<=(const GncDate&, const GncDate&);
299  friend bool operator>=(const GncDate&, const GncDate&);
300  friend bool operator!=(const GncDate&, const GncDate&);
301 };
302 
306 bool operator<(const GncDate& a, const GncDate& b);
307 bool operator>(const GncDate& a, const GncDate& b);
308 bool operator==(const GncDate& a, const GncDate& b);
309 bool operator<=(const GncDate& a, const GncDate& b);
310 bool operator>=(const GncDate& a, const GncDate& b);
311 bool operator!=(const GncDate& a, const GncDate& b);
314 #endif // __GNC_DATETIME_HPP__
GncDate date() const
Obtain the date from the time, as a GncDate, in the current timezone.
std::string format_iso8601() const
Format the GncDateTime into a gnucash-style iso8601 string in UTC.
GnuCash DateTime class.
bool isnull(void)
Test that the Date has an implementation.
GnuCash DateFormat class.
void today()
Set the date object to the computer clock&#39;s current day.
long offset() const
Obtain the UTC offset in seconds.
std::string format(const char *format)
Format the GncDate into a std::string.
~GncDate()
Default destructor.
bool isnull(void)
Test if the GncDateTime has a member pointer.
static std::string timestamp()
Get an undelimited string representing the current date and time.
std::string format_zulu(const char *format) const
Format the GncDateTime into a std::string in GMT.
friend bool operator<(const GncDate &, const GncDate &)
GncDate()
Construct a GncDate representing the current day.
GncDate & operator=(const GncDate &)
Copy assignment operator.
Private implementation of GncDate.
GncDateTime()
Construct a GncDateTime representing the current time in the current timezone.
std::string format(const char *format) const
Format the GncDateTime into a std::string.
ymd year_month_day() const
Get the year, month, and day from the date as a ymd.
const std::string m_fmt
A string representing the format.
GncDateFormat(const char *fmt, const char *re)
Construct a GncDateFormat with a given format and corresponding regular expression.
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93
struct tm utc_tm() const
Obtain a struct tm representing the time in UTC.
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
GnuCash Date class.
void now()
Set the GncDateTime to the date and time indicated in the computer&#39;s clock.