ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
calendar.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
14# include "alib/time/time.hpp"
21#endif // !DOXYGEN
22
23namespace alib::lang::system {
24
25// #################################################################################################
26// CalendarDateTime
27// #################################################################################################
29{
30 Year=
31 Month=
32 Day=
33 Hour=
34 Minute=
35 Second=
36 Millisecond= 0;
37 DayOfWeek= -1;
38}
39
40void CalendarDateTime::Set( const DateTime& timeStamp, lang::Timezone timezone )
41{
42 Clear();
43
44 #if defined( _WIN32 )
45 SYSTEMTIME st= timeStamp.ToSystemTime( timezone );
46
47 Year= st.wYear;
48 Day= st.wDay;
49 DayOfWeek= st.wDayOfWeek;
50 Month= st.wMonth;
51 Hour= st.wHour;
52 Minute= st.wMinute;
53 Second= st.wSecond;
54
55 #elif defined (__GLIBCXX__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
56 struct tm tm;
57 time_t tt= timeStamp.InEpochSeconds();
58 if ( timezone == lang::Timezone::UTC )
59 {
60 tm.tm_isdst= 0; // daylight saving off
61 gmtime_r( &tt, &tm );
62 }
63 else
64 {
65 tm.tm_isdst= -1; // daylight saving auto
66 localtime_r( &tt, &tm );
67 }
68
69 Year= tm.tm_year + 1900;
70 Day= tm.tm_mday;
71 DayOfWeek= tm.tm_wday;
72 Month= tm.tm_mon + 1;
73 Second= tm.tm_sec;
74 Hour= tm.tm_hour;
75 Minute= tm.tm_min;
76
77 #else
78 #pragma message ("Unknown Platform in file: " __FILE__ )
79 #endif
80}
81
83{
85
86 #if defined( _WIN32 )
87
88 SYSTEMTIME st;
89 st.wYear= (WORD) Year;
90 st.wDay= (WORD) Day;
91 st.wDayOfWeek= (WORD) DayOfWeek;
92 st.wMonth= (WORD) Month;
93 st.wHour= (WORD) Hour;
94 st.wMinute= (WORD) Minute;
95 st.wSecond= (WORD) Second;
96 st.wMilliseconds= 0;
97
98 result= DateTime::FromSystemTime( st, timezone );
99
100 #elif defined (__GLIBCXX__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
101 struct tm tm;
102 tm.tm_year= Year - 1900;
103 tm.tm_mday= Day;
104 tm.tm_mon= Month -1;
105 tm.tm_hour= Hour;
106 tm.tm_min= Minute;
107 tm.tm_sec= Second;
108
109 time_t tt;
110 if ( timezone == lang::Timezone::UTC )
111 {
112 tm.tm_isdst= 0; // daylight saving off
113 tt= timegm( &tm );
114 }
115 else
116 {
117 tm.tm_isdst= -1; // daylight saving auto
118 tt= mktime( &tm );
119 }
120
121 result= DateTime::FromEpochSeconds( tt );
122
123
124 #else
125 #pragma message ("Unknown Platform in file: " __FILE__ )
126 #endif
127
128 return result;
129}
130
131
132
133// #################################################################################################
134// CalendarDuration
135// #################################################################################################
137{
138 Days=
139 Hours=
140 Minutes=
141 Seconds=
144 Nanoseconds= 0;
145}
146#define NanosPerDay INT64_C( 86400000000000 ) ///< Constant denoting number of nanoseconds of a day.
147#define NanosPerHour INT64_C( 3600000000000 ) ///< Constant denoting number of nanoseconds of an hour.
148#define NanosPerMinute INT64_C( 60000000000 ) ///< Constant denoting number of nanoseconds of a minute.
149#define NanosPerSecond INT64_C( 1000000000 ) ///< Constant denoting number of nanoseconds of a second.
150#define NanosPerMillisecond INT64_C( 1000000 ) ///< Constant denoting number of nanoseconds of a millisecond.
151#define NanosPerMicrosecond INT64_C( 1000 ) ///< Constant denoting number of nanoseconds of a microsecond.
152
153
155{
156 Clear();
157 decltype(nanos) fract;
158 if ( nanos > NanosPerDay ) { Days= static_cast<int>( fract= nanos / NanosPerDay ); nanos-= fract * NanosPerDay; }
159 if ( nanos > NanosPerHour ) { Hours= static_cast<int>( fract= nanos / NanosPerHour ); nanos-= fract * NanosPerHour; }
160 if ( nanos > NanosPerMinute ) { Minutes= static_cast<int>( fract= nanos / NanosPerMinute ); nanos-= fract * NanosPerMinute; }
161 if ( nanos > NanosPerSecond ) { Seconds= static_cast<int>( fract= nanos / NanosPerSecond ); nanos-= fract * NanosPerSecond; }
162 if ( nanos > NanosPerMillisecond ) { Milliseconds= static_cast<int>( fract= nanos / NanosPerMillisecond ); nanos-= fract * NanosPerMillisecond; }
163 if ( nanos > NanosPerMicrosecond ) { Microseconds= static_cast<int>( fract= nanos / NanosPerMicrosecond ); }
164}
165
167{
168 return Days * NanosPerDay
169 + Hours * NanosPerHour
170 + Minutes * NanosPerMinute
171 + Seconds * NanosPerSecond
172 + Milliseconds * NanosPerMillisecond
173 + Microseconds * NanosPerMicrosecond
174 + Nanoseconds;
175}
176
177// #################################################################################################
178// CalendarDate
179// #################################################################################################
180void CalendarDate::Set( const DateTime& dateTime, lang::Timezone timezone )
181{
182 CalendarDateTime cdt(dateTime, timezone);
183 stamp= uint32_t( cdt.Year ) << 12
184 | uint32_t( cdt.Month ) << 8
185 | uint32_t( cdt.Day ) << 3
186 | uint32_t( cdt.DayOfWeek ) ;
187}
188void CalendarDate::Set( int year, int month, int day, int dayOfWeek )
189{
190 ALIB_ASSERT_ERROR( year >= 0 && year <= 1048575, "CAMP", "CalendarDate: Years must be between 0 and 1,048,575." )
191 ALIB_ASSERT_ERROR( month >= 1 && month <= 12 , "CAMP", "CalendarDate: Months must be between 1 and 12." )
192 ALIB_ASSERT_ERROR( day >= 1 && day <= 31 , "CAMP", "CalendarDate: Days must be between 1 and 31." )
193 ALIB_ASSERT_ERROR( dayOfWeek <= 6 , "CAMP", "CalendarDate: Day of week must be either negative or between 0 and 6." )
194
195 // get day of week, if not given
196 if( dayOfWeek< 0 )
197 {
198 dayOfWeek= CalendarDateTime( CalendarDateTime( year, month, day, 12, 0, 0 ).Get(),
200 }
201 #if ALIB_DEBUG
202 else
203 {
204 CalendarDateTime cdt( year, month, day, 12, 0, 0 );
205 cdt= CalendarDateTime( cdt.Get( )); // get day of week
206 ALIB_ASSERT_ERROR( dayOfWeek == cdt.DayOfWeek, "CAMP",
207 "Day of week does not correspond to given date. Should be: ", cdt.DayOfWeek )
208 }
209 #endif
210 stamp= uint32_t( year ) << 12
211 | uint32_t( month ) << 8
212 | uint32_t( day ) << 3
213 | uint32_t( dayOfWeek ) ;
214}
215
216DateTime CalendarDate::Get( lang::Timezone timezone, int hour, int minute, int second ) const
217{
218 return CalendarDateTime( Year(), Month(), Day(), hour, minute, second).Get( timezone );
219}
220
222{
223 // use the system for it
224 return CalendarDate( CalendarDateTime( Year(), Month(), Day(), 12 ).Get()
225 + DateTime::Duration::FromAbsoluteDays( daysToAdd ),
227}
228
230{
231 auto day = Day ();
232 auto month= Month();
233 auto year = Year ();
234
235 // can we do it manually?
236 if( ( month != 2 && day != 30 )
237 || ( month == 2 && day != 28 ) )
238 {
239 if( ( day != 31 )
240 && ( day != 29 || month != 2 ) )
241 {
242 day++;
243 }
244 else
245 {
246 day= 1;
247 if( ++month == 13 )
248 {
249 month= 1;
250 ++year;
251 }
252 }
253 Set( year, month, day, (DayOfWeek() + 1) % 7 );
254 return *this;
255 }
256
257 // use the system for it
258 CalendarDateTime cdt( year, month, day, 12 );
259 return *this= CalendarDate( cdt.Get() + DateTime::Duration::FromAbsoluteDays(1), lang::Timezone::UTC );
260}
261
263{
264 auto day = Day();
265
266 // can we do it manually?
267 if( day > 1 )
268 {
270 | ( uint32_t( day-1 ) << 3 )
271 | ( ( (stamp & 7) + 6 ) % 7 ) ; // +6 corresponds to -1 in modulo op
272 return *this;
273 }
274
275 // use the system for it
276 return *this= CalendarDate( Get() - DateTime::Duration::FromAbsoluteDays(1), lang::Timezone::UTC );
277}
278
279} // namespace [ alib::lang::system]
int Minute
The calendar minute (0..59).
Definition calendar.hpp:63
int Year
The calendar year (e.g., 2022).
Definition calendar.hpp:51
int Month
The calendar month (1..12).
Definition calendar.hpp:54
ALIB_API void Set(const DateTime &timeStamp, lang::Timezone timezone=lang::Timezone::Local)
Definition calendar.cpp:40
int Day
The calendar day (1..31).
Definition calendar.hpp:57
int Hour
The calendar hour (0..23).
Definition calendar.hpp:60
ALIB_API void Clear()
Sets all public values to 0.
Definition calendar.cpp:28
ALIB_API DateTime Get(lang::Timezone timezone=lang::Timezone::Local) const
Definition calendar.cpp:82
int Millisecond
The calendar millisecond (0..999).
Definition calendar.hpp:69
int Second
The calendar second (0..59).
Definition calendar.hpp:66
ALIB_API CalendarDate operator--()
Definition calendar.cpp:262
ALIB_API CalendarDate operator+(int daysToAdd) const
Definition calendar.cpp:221
CalendarDate()=default
Default constructor leaving this object uninitialized (random value). **************.
ALIB_API DateTime Get(lang::Timezone timezone=lang::Timezone::Local, int hour=12, int minute=0, int second=0) const
Definition calendar.cpp:216
uint32_t stamp
Encoded date value.
Definition calendar.hpp:364
ALIB_API CalendarDate operator++()
Definition calendar.cpp:229
ALIB_API void Set(int year, int month, int day, int dayOfWeek=-1)
Definition calendar.cpp:188
int Nanoseconds
The number of nanoseconds (not the total, hence 0-999) within the duration.
Definition calendar.hpp:227
int Milliseconds
The number of milliseconds (not the total, hence 0-999) within the duration.
Definition calendar.hpp:221
int Seconds
The number of seconds (not the total, hence 0-59) within the duration.
Definition calendar.hpp:218
int Microseconds
The number of microseconds (not the total, hence 0-999) within the duration.
Definition calendar.hpp:224
int Days
The number of days within the duration.
Definition calendar.hpp:209
int Hours
The number of hours (not the total, hence 0-23) within the duration.
Definition calendar.hpp:212
ALIB_API void FromNanoSeconds(int64_t nanos)
Definition calendar.cpp:154
int Minutes
The number of minutes (not the total, hence 0-59) within the duration.
Definition calendar.hpp:215
ALIB_API void Clear()
Sets all public values to 0.
Definition calendar.cpp:136
static DateTime FromEpochSeconds(time_t epochSeconds)
Definition datetime.hpp:74
time_t InEpochSeconds() const
Definition datetime.hpp:60
static ALIB_API DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
ALIB_API SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) const
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
This is the reference documentation of sub-namespace system of module ALib BaseCamp.
Definition basecamp.cpp:75
constexpr TIntegral LowerMask()
Timezone
Denotes whether a time value represents local time or UTC.
@ UTC
Denotes UTC (coordinated universal time).
@ Get
Denotes to search data.
lang::system::CalendarDateTime CalendarDateTime
Type alias in namespace alib.
Definition calendar.hpp:743