GnuCash c935c2f+
Loading...
Searching...
No Matches
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#include <boost/regex.hpp>
37
38typedef struct
39{
40 int year; //1400-9999
41 int month; //1-12
42 int day; //1-31
43} gnc_ymd;
44
45enum class DayPart {
46 start, // 00:00 local
47 neutral, // 10:59 UTC
48 end, // 23:59 local
49};
50
51class GncDateTimeImpl;
52class GncDateImpl;
53class GncDate;
54using time64 = int64_t;
55constexpr const time64 MINTIME = -17987443200;
56constexpr const time64 MAXTIME = 253402214400;
57
72{
73public:
83 GncDateTime(const time64 time);
91 GncDateTime(const struct tm tm);
106 GncDateTime(const GncDate& date, DayPart part = DayPart::neutral);
113 GncDateTime(const std::string& str);
114 GncDateTime(const char* str);
115 ~GncDateTime();
118 void now();
120 explicit operator time64() const;
123 explicit operator struct tm() const;
128 long offset()const;
132 struct tm utc_tm() const;
136 GncDate date() const;
138 bool isnull (void) { return m_impl == nullptr; }
147 std::string format(const char* format) const;
156 std::string format_zulu(const char* format) const;
160 std::string format_iso8601() const;
164 static std::string timestamp();
165
166private:
167 std::unique_ptr<GncDateTimeImpl> m_impl;
168};
169
180using StringToDate = std::function<boost::gregorian::date(const std::string&)>;
181
183{
184public:
190 GncDateFormat (const char* fmt, const char* re) :
191 m_fmt(fmt), m_re(re) {}
192 GncDateFormat (const char* fmt, StringToDate str_to_date, const char* re) :
193 m_fmt(fmt), m_re(re), m_str_to_date(str_to_date) {}
194 GncDateFormat (const char* fmt, StringToDate str_to_date) :
195 m_fmt(fmt), m_str_to_date(str_to_date) {}
197 const std::string m_fmt;
198private:
202 boost::regex m_re;
203 std::optional<StringToDate> m_str_to_date;
204
205 friend class GncDateImpl;
206};
207
215{
216 public:
233 static const std::vector<GncDateFormat> c_formats;
236 GncDate();
250 GncDate(int year, int month, int day);
267 GncDate(const std::string str, const std::string fmt);
270 GncDate(std::unique_ptr<GncDateImpl> impl);
273 GncDate(const GncDate&);
282 GncDate& operator=(const GncDate&);
287 void today();
291 gnc_ymd year_month_day() const;
300 std::string format(const char* format);
302 bool isnull (void) { return m_impl == nullptr; }
303
304private:
305 std::unique_ptr<GncDateImpl> m_impl;
306
307 friend GncDateTime::GncDateTime(const GncDate&, DayPart);
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 friend bool operator!=(const GncDate&, const GncDate&);
314};
315
319bool operator<(const GncDate& a, const GncDate& b);
320bool operator>(const GncDate& a, const GncDate& b);
321bool operator==(const GncDate& a, const GncDate& b);
322bool operator<=(const GncDate& a, const GncDate& b);
323bool operator>=(const GncDate& a, const GncDate& b);
324bool operator!=(const GncDate& a, const GncDate& b);
327#endif // __GNC_DATETIME_HPP__
GncDateFormat(const char *fmt, const char *re)
Construct a GncDateFormat with a given format and corresponding regular expression.
const std::string m_fmt
A string representing the format.
Private implementation of GncDate.
GnuCash DateTime class.
std::string format_iso8601() const
Format the GncDateTime into a gnucash-style iso8601 string in UTC.
void now()
Set the GncDateTime to the date and time indicated in the computer's clock.
bool isnull(void)
Test if the GncDateTime has a member pointer.
struct tm utc_tm() const
Obtain a struct tm representing the time in UTC.
std::string format_zulu(const char *format) const
Format the GncDateTime into a std::string in GMT.
GncDate date() const
Obtain the date from the time, as a GncDate, in the current timezone.
std::string format(const char *format) const
Format the GncDateTime into a std::string.
static std::string timestamp()
Get an undelimited string representing the current date and time.
GncDateTime()
Construct a GncDateTime representing the current time in the current timezone.
long offset() const
Obtain the UTC offset in seconds.
GnuCash Date class.
std::string format(const char *format)
Format the GncDate into a std::string.
friend bool operator<(const GncDate &, const GncDate &)
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
bool isnull(void)
Test that the Date has an implementation.
GncDate(GncDate &&)
Move constructor.
void today()
Set the date object to the computer clock's current day.
GncDate & operator=(GncDate &&)
Move assignment operator.
~GncDate()
Default destructor.
GncDate()
Construct a GncDate representing the current day.
gnc_ymd year_month_day() const
Get the year, month, and day from the date as a gnc_ymd.
GncDate & operator=(const GncDate &)
Copy assignment operator.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87