GnuCash  5.6-150-g038405b370+
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 #include <functional>
33 #include <optional>
34 
35 #include <boost/date_time/gregorian/gregorian.hpp>
36 
37 typedef struct
38 {
39  int year; //1400-9999
40  int month; //1-12
41  int day; //1-31
42 } gnc_ymd;
43 
44 enum class DayPart {
45  start, // 00:00 local
46  neutral, // 10:59 UTC
47  end, // 23:59 local
48 };
49 
50 class GncDateTimeImpl;
51 class GncDateImpl;
52 class GncDate;
53 using time64 = int64_t;
54 constexpr const time64 MINTIME = -17987443200;
55 constexpr const time64 MAXTIME = 253402214400;
56 
71 {
72 public:
76  GncDateTime();
82  GncDateTime(const time64 time);
90  GncDateTime(const struct tm tm);
105  GncDateTime(const GncDate& date, DayPart part = DayPart::neutral);
112  GncDateTime(const std::string& str);
113  GncDateTime(const char* str);
114  ~GncDateTime();
117  void now();
119  explicit operator time64() const;
122  explicit operator struct tm() const;
127  long offset()const;
131  struct tm utc_tm() const;
135  GncDate date() const;
137  bool isnull (void) { return m_impl == nullptr; }
146  std::string format(const char* format) const;
155  std::string format_zulu(const char* format) const;
159  std::string format_iso8601() const;
163  static std::string timestamp();
164 
165 private:
166  std::unique_ptr<GncDateTimeImpl> m_impl;
167 };
168 
179 using StringToDate = std::function<boost::gregorian::date(const std::string&)>;
180 
182 {
183 public:
189  GncDateFormat (const char* fmt, const char* re) :
190  m_fmt(fmt), m_re(re) {}
191  GncDateFormat (const char* fmt, StringToDate str_to_date, const char* re) :
192  m_fmt(fmt), m_re(re), m_str_to_date(str_to_date) {}
193  GncDateFormat (const char* fmt, StringToDate str_to_date) :
194  m_fmt(fmt), m_str_to_date(str_to_date) {}
196  const std::string m_fmt;
197 private:
201  const std::string m_re;
202  std::optional<StringToDate> m_str_to_date;
203 
204  friend class GncDateImpl;
205 };
206 
213 class GncDate
214 {
215  public:
232  static const std::vector<GncDateFormat> c_formats;
235  GncDate();
249  GncDate(int year, int month, int day);
266  GncDate(const std::string str, const std::string fmt);
269  GncDate(std::unique_ptr<GncDateImpl> impl);
272  GncDate(const GncDate&);
275  GncDate(GncDate&&);
278  ~GncDate();
281  GncDate& operator=(const GncDate&);
286  void today();
290  gnc_ymd year_month_day() const;
299  std::string format(const char* format);
301  bool isnull (void) { return m_impl == nullptr; }
302 
303 private:
304  std::unique_ptr<GncDateImpl> m_impl;
305 
306  friend GncDateTime::GncDateTime(const GncDate&, DayPart);
307  friend bool operator<(const GncDate&, const GncDate&);
308  friend bool operator>(const GncDate&, const GncDate&);
309  friend bool operator==(const GncDate&, const GncDate&);
310  friend bool operator<=(const GncDate&, const GncDate&);
311  friend bool operator>=(const GncDate&, const GncDate&);
312  friend bool operator!=(const GncDate&, const GncDate&);
313 };
314 
318 bool operator<(const GncDate& a, const GncDate& b);
319 bool operator>(const GncDate& a, const GncDate& b);
320 bool operator==(const GncDate& a, const GncDate& b);
321 bool operator<=(const GncDate& a, const GncDate& b);
322 bool operator>=(const GncDate& a, const GncDate& b);
323 bool operator!=(const GncDate& a, const GncDate& b);
326 #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.
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.
gnc_ymd year_month_day() const
Get the year, month, and day from the date as a gnc_ymd.
~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.
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
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
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.