NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
datetimetools.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2021 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "datetimetools.hpp"
20#include "date/iso_week.h"
21#include "windows.h"
22
28{
29 m_hours = std::chrono::hours(0);
30 m_minutes = std::chrono::minutes(0);
31 m_seconds = std::chrono::seconds(0);
32 m_millisecs = std::chrono::milliseconds(0);
33 m_microsecs = std::chrono::microseconds(0);
34}
35
36
46{
47 return std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
48}
49
50
60{
61 return date::year_month_day(date::floor<date::days>(tp));
62}
63
64
74{
76 tp += std::chrono::seconds(t);
77 return dateFromTimePoint(tp);
78}
79
80
90{
91 time_stamp timeStamp;
92
93 timeStamp.m_ymd = dateFromTimePoint(tp);
94 std::chrono::nanoseconds diff = tp - date::floor<date::days>(tp);
95
96 timeStamp.m_hours = std::chrono::duration_cast<std::chrono::hours>(diff);
97 diff = diff - std::chrono::duration_cast<std::chrono::hours>(diff);
98
99 timeStamp.m_minutes = std::chrono::duration_cast<std::chrono::minutes>(diff);
100 diff = diff - std::chrono::duration_cast<std::chrono::minutes>(diff);
101
102 timeStamp.m_seconds = std::chrono::duration_cast<std::chrono::seconds>(diff);
103 diff = diff - std::chrono::duration_cast<std::chrono::seconds>(diff);
104
105 timeStamp.m_millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
106 diff = diff - std::chrono::duration_cast<std::chrono::milliseconds>(diff);
107
108 timeStamp.m_microsecs = std::chrono::duration_cast<std::chrono::microseconds>(diff);
109
110 return timeStamp;
111}
112
113
123{
125 tp += std::chrono::seconds(t);
126 return getTimeStampFromTimePoint(tp);
127}
128
129
139{
140 auto res = date::sys_days(ts.m_ymd)
141 + ts.m_hours
142 + ts.m_minutes
143 + ts.m_seconds
144 + ts.m_millisecs
145 + ts.m_microsecs;
146
147 return res;
148}
149
150
159{
160 auto tp = std::chrono::time_point_cast<std::chrono::seconds>(getTimePointFromTimeStamp(ts));
161 return tp.time_since_epoch().count();
162}
163
164
174{
175 return tp.time_since_epoch().count() * std::micro::num / (double)std::micro::den;
176}
177
178
188{
190
191 tp += std::chrono::microseconds((int64_t)std::rint(d * std::micro::den / std::micro::num));
192
193 return tp;
194}
195
196
207{
208 date::sys_days _sys_days = std::chrono::time_point_cast<date::days>(tp);
209 return date::weekday(_sys_days).iso_encoding();
210}
211
212
222{
223 date::sys_days _sys_days = std::chrono::time_point_cast<date::days>(tp);
224 return unsigned(iso_week::year_weeknum_weekday(_sys_days).weeknum());
225}
226
227
236{
237 TIME_ZONE_INFORMATION timezone;
238 int res = GetTimeZoneInformation(&timezone);
239
241 tz.Bias = std::chrono::minutes(timezone.Bias);
242 tz.DayLightBias = res == TIME_ZONE_ID_DAYLIGHT ? std::chrono::minutes(timezone.DaylightBias) : std::chrono::minutes(0);
243
244 return tz;
245}
246
247
248
249
250
CONSTCD11 unsigned iso_encoding() const NOEXCEPT
Definition: date.h:1881
__time64_t getTime_tFromTimeStamp(const time_stamp &ts)
Convert a time_stamp to a __time64_t.
date::year_month_day dateFromTime_t(__time64_t t)
Return the date extracted from the passed __time64_t.
size_t getWeekDay(sys_time_point tp)
Returns the weekday of the selected timepoint as an unsigned integer with Monday being 1u and Sunday ...
sys_time_point to_timePoint(double d)
Convert a double to a sys_time_point assuming the double is in units of seconds.
time_stamp getTimeStampFromTime_t(__time64_t t)
Return a time_stamp instance converted from the passed __time64_t.
size_t getWeekNum(sys_time_point tp)
Returns the weeknum of the selected timepoint as an unsigned integer.
double to_double(sys_time_point tp)
Convert a sys_time_point to a double. The double is returned in units of seconds.
date::year_month_day dateFromTimePoint(sys_time_point tp)
Return the date extracted from the passed sys_time_point.
sys_time_point sys_time_now()
Returns the current time as a sys_time_point (i.e. a std::chrono::time_point with microseconds precis...
time_stamp getTimeStampFromTimePoint(sys_time_point tp)
Return a time_stamp instance converted from the passed sys_time_point.
time_zone getCurrentTimeZone()
Return the current time_zone of the system.
sys_time_point getTimePointFromTimeStamp(const time_stamp &ts)
Convert a time_stamp to a sys_time_point.
std::chrono::time_point< std::chrono::system_clock, std::chrono::microseconds > sys_time_point
This is a typedef for a custom system_clock time_point with microseconds precision.
sys_time< days > sys_days
Definition: date.h:193
value_type rint(value_type v)
This structure defines all fields necessary to create a time stamp or a formatted date.
time_stamp()
Default constructor for zero-initialisation.
date::year_month_day m_ymd
std::chrono::microseconds m_microsecs
std::chrono::minutes m_minutes
std::chrono::seconds m_seconds
std::chrono::hours m_hours
std::chrono::milliseconds m_millisecs
This structure defines the information for a time zone.