common_util
Loading...
Searching...
No Matches
datetime.h
Go to the documentation of this file.
1
19#pragma once
20
21#include <cstdint>
22#include <string>
23#include <iostream>
24#include <regex>
25#include <vector>
26#include <utility>
27
28namespace cutl
29{
45
51 {
52 public:
57 static constexpr int second = 1000;
62 static constexpr int min = 60 * second;
67 static constexpr int hour = 60 * min;
72 static constexpr int day = 24 * hour;
73
74 public:
80 datetime(uint64_t ms);
81
87 datetime(const datetime &other);
88
94
95 private:
96 datetime();
97
98 public:
104 static datetime now();
105
122 static datetime get(const std::string &time_text, int isdst = -1);
123
124 public:
130 uint64_t timestamp() const;
139 std::string format(datetime_format dfmt = datetime_format::datetime_format_a, bool local = true, bool show_milliseconds = true) const;
140 // fmt, usages like std::put_time
141
150 std::string format(const std::string &fmt, bool local = true, bool show_milliseconds = true) const;
151
157 std::string utctime() const
158 {
159 return format(datetime_format::datetime_format_a, false);
160 }
161
169
176 datetime operator+(uint64_t ms);
177
184 datetime operator-(uint64_t ms);
185
192 datetime &operator+=(uint64_t ms);
193
200 datetime &operator-=(uint64_t ms);
201
208 int64_t operator-(const datetime &other) const;
209
210 private:
211 using time_regex_type = std::pair<std::string, std::regex>;
212 using time_regex_vec_type = std::vector<time_regex_type>;
213 static std::string supported_time_formats(const time_regex_vec_type &fmtlist);
214 static bool verify_time(const struct tm &time);
215
216 private:
217 uint64_t timestamp_ms_;
218 };
219
227 std::ostream &operator<<(std::ostream &os, const datetime &dt);
228
229} // namespace
A simple, feature-rich modern C++ date-time class.
Definition datetime.h:51
static constexpr int hour
Constants value: hour, expressed in milliseconds.
Definition datetime.h:67
std::string format(const std::string &fmt, bool local=true, bool show_milliseconds=true) const
Format the datetime object to a string with a custom format string.
static constexpr int min
Constants value: min, expressed in milliseconds.
Definition datetime.h:62
static datetime now()
Get a datetime object for the current system time.
std::string utctime() const
Get the string of UTC time described by datetime object.
Definition datetime.h:157
static constexpr int day
Constants value: day, expressed in milliseconds.
Definition datetime.h:72
std::string format(datetime_format dfmt=datetime_format::datetime_format_a, bool local=true, bool show_milliseconds=true) const
format the datetime object to a string
datetime & operator-=(uint64_t ms)
Define the subtraction and assignment operator.
uint64_t timestamp() const
Get the timestamp in milliseconds.
~datetime()
Destroy the datetime object.
datetime operator+(uint64_t ms)
Define the addition operator.
datetime & operator=(const datetime &other)
Define the assignment operator.
datetime(const datetime &other)
Construct a new datetime object.
datetime operator-(uint64_t ms)
Define the subtraction operator.
datetime(uint64_t ms)
Construct a new datetime object.
datetime & operator+=(uint64_t ms)
Define the addition and assignment operator.
static constexpr int second
Constants value: second, expressed in milliseconds.
Definition datetime.h:57
static datetime get(const std::string &time_text, int isdst=-1)
Constructs a datetime object from a local time string.
int64_t operator-(const datetime &other) const
Define the subtraction operator between two datetime objects.
datetime_format
the string datetime format for parsing and formatting
Definition datetime.h:35
std::ostream & operator<<(std::ostream &os, const datetime &dt)
Define the output stream operator for datetime object.