Present  0.9
 All Classes Files Functions Variables Typedefs Friends Macros Pages
timestamp.h
Go to the documentation of this file.
1 /*
2  * Present - Date/Time Library
3  *
4  * Definition of the Timestamp structure and declarations of the corresponding
5  * functions
6  *
7  * This file may be included individually ONLY if being used by a C compiler.
8  * However, it is recommended (and required for C++ projects) to include
9  * "present.h" rather than these individual header files.
10  *
11  * Licensed under the MIT License.
12  * For details, see LICENSE.
13  */
14 
15 #include <time.h>
16 
17 #include "present/internal/cpp-guard.h"
18 #include "present/internal/header-utils.h"
19 #include "present/internal/types.h"
20 
21 #include "present/internal/present-timestamp-data.h"
22 
23 #ifndef _PRESENT_TIMESTAMP_H_
24 #define _PRESENT_TIMESTAMP_H_
25 
26 /*
27  * Forward Declarations
28  */
29 
30 struct ClockTime;
31 struct Date;
32 struct MonthDelta;
33 struct TimeDelta;
34 
35 /*
36  * C++ Class / C Struct Definition
37  */
38 
44 struct PRESENT_API Timestamp {
51 
58  struct {
59  unsigned int invalid_clock_time : 1,
60  invalid_date : 1;
61  } errors;
62 
63  /* Internal data representation */
64  struct PresentTimestampData data_;
65 
66 #ifdef __cplusplus
67 
68  static Timestamp create(const time_t time);
69 
71  static Timestamp create(
72  const struct tm & tm,
73  const TimeDelta & time_zone_offset);
75  static Timestamp create_utc(const struct tm & tm);
77  static Timestamp create_local(const struct tm & tm);
78 
80  static Timestamp create(
81  const Date & date,
82  const ClockTime & clock_time,
83  const TimeDelta & time_zone_offset);
85  static Timestamp create_utc(
86  const Date & date,
87  const ClockTime & clock_time);
89  static Timestamp create_local(
90  const Date & date,
91  const ClockTime & clock_time);
92 
94  static Timestamp now();
95 
97  static Timestamp epoch();
98 
100  time_t get_time_t() const;
101 
103  struct tm get_struct_tm(const TimeDelta & time_zone_offset) const;
105  struct tm get_struct_tm_utc() const;
107  struct tm get_struct_tm_local() const;
108 
110  Date get_date(const TimeDelta & time_zone_offset) const;
112  Date get_date_utc() const;
114  Date get_date_local() const;
115 
117  ClockTime get_clock_time(const TimeDelta & time_zone_offset) const;
119  ClockTime get_clock_time_utc() const;
121  ClockTime get_clock_time_local() const;
122 
124  TimeDelta difference(const Timestamp & other) const;
126  TimeDelta absolute_difference(const Timestamp & other) const;
127 
129  Timestamp & operator+=(const TimeDelta & delta);
131  Timestamp & operator+=(const DayDelta & delta);
133  Timestamp & operator+=(const MonthDelta & delta);
135  Timestamp & operator-=(const TimeDelta & delta);
137  Timestamp & operator-=(const DayDelta & delta);
139  Timestamp & operator-=(const MonthDelta & delta);
140 
142  friend const Timestamp operator+(
143  const Timestamp & lhs,
144  const TimeDelta & rhs);
146  friend const Timestamp operator+(
147  const TimeDelta & lhs,
148  const Timestamp & rhs);
149 
151  friend const Timestamp operator+(
152  const Timestamp & lhs,
153  const DayDelta & rhs);
155  friend const Timestamp operator+(
156  const DayDelta & lhs,
157  const Timestamp & rhs);
158 
160  friend const Timestamp operator+(
161  const Timestamp & lhs,
162  const MonthDelta & rhs);
164  friend const Timestamp operator+(
165  const MonthDelta & lhs,
166  const Timestamp & rhs);
167 
169  friend const Timestamp operator-(
170  const Timestamp & lhs,
171  const TimeDelta & rhs);
172 
174  friend const Timestamp operator-(
175  const Timestamp & lhs,
176  const DayDelta & rhs);
177 
179  friend const Timestamp operator-(
180  const Timestamp & lhs,
181  const MonthDelta & rhs);
182 
184  static short compare(const Timestamp & lhs, const Timestamp & rhs);
185 
187  friend bool operator==(const Timestamp & lhs, const Timestamp & rhs);
188  friend bool operator!=(const Timestamp & lhs, const Timestamp & rhs);
189 
191  friend bool operator<(const Timestamp & lhs, const Timestamp & rhs);
193  friend bool operator<=(const Timestamp & lhs, const Timestamp & rhs);
195  friend bool operator>(const Timestamp & lhs, const Timestamp & rhs);
197  friend bool operator>=(const Timestamp & lhs, const Timestamp & rhs);
198 #endif
199 };
200 
201 /*
202  * C Method Declarations
203  */
204 
205 #ifdef __cplusplus
206 extern "C" {
207 #endif
208 
214 PRESENT_API struct Timestamp
215 Timestamp_from_time_t(const time_t time);
216 
221 PRESENT_API void
223  struct Timestamp * const result,
224  const time_t time);
225 
226 
235 PRESENT_API struct Timestamp
237  const struct tm tm,
238  const struct TimeDelta * const time_zone_offset);
239 
244 PRESENT_API void
246  struct Timestamp * const result,
247  const struct tm tm,
248  const struct TimeDelta * const time_zone_offset);
249 
256 PRESENT_API struct Timestamp
257 Timestamp_from_struct_tm_utc(const struct tm tm);
258 
263 PRESENT_API void
265  struct Timestamp * const result,
266  const struct tm tm);
267 
274 PRESENT_API struct Timestamp
275 Timestamp_from_struct_tm_local(const struct tm tm);
276 
281 PRESENT_API void
283  struct Timestamp * const result,
284  const struct tm tm);
285 
286 
304 PRESENT_API struct Timestamp
306  const struct Date * const date,
307  const struct ClockTime * const clock_time,
308  const struct TimeDelta * const time_zone_offset);
309 
314 PRESENT_API void
316  struct Timestamp * const result,
317  const struct Date * const date,
318  const struct ClockTime * const clock_time,
319  const struct TimeDelta * const time_zone_offset);
320 
336 PRESENT_API struct Timestamp
338  const struct Date * const date,
339  const struct ClockTime * const clock_time);
340 
345 PRESENT_API void
347  struct Timestamp * const result,
348  const struct Date * const date,
349  const struct ClockTime * const clock_time);
350 
366 PRESENT_API struct Timestamp
368  const struct Date * const date,
369  const struct ClockTime * const clock_time);
370 
375 PRESENT_API void
377  struct Timestamp * const result,
378  const struct Date * const date,
379  const struct ClockTime * const clock_time);
380 
381 
389 PRESENT_API struct Timestamp
390 Timestamp_now(void);
391 
396 PRESENT_API void
397 Timestamp_ptr_now(struct Timestamp * const result);
398 
403 PRESENT_API struct Timestamp
404 Timestamp_epoch(void);
405 
410 PRESENT_API void
411 Timestamp_ptr_epoch(struct Timestamp * const result);
412 
413 
417 PRESENT_API time_t
418 Timestamp_get_time_t(const struct Timestamp * const self);
419 
420 
425 PRESENT_API struct tm
427  const struct Timestamp * const self,
428  const struct TimeDelta * const time_zone_offset);
429 
434 PRESENT_API struct tm
435 Timestamp_get_struct_tm_utc(const struct Timestamp * const self);
436 
441 PRESENT_API struct tm
442 Timestamp_get_struct_tm_local(const struct Timestamp * const self);
443 
444 
452 PRESENT_API struct Date
454  const struct Timestamp * const self,
455  const struct TimeDelta * const time_zone_offset);
456 
463 PRESENT_API struct Date
464 Timestamp_get_date_utc(const struct Timestamp * const self);
465 
473 PRESENT_API struct Date
474 Timestamp_get_date_local(const struct Timestamp * const self);
475 
476 
484 PRESENT_API struct ClockTime
486  const struct Timestamp * const self,
487  const struct TimeDelta * const time_zone_offset);
488 
496 PRESENT_API struct ClockTime
497 Timestamp_get_clock_time_utc(const struct Timestamp * const self);
498 
506 PRESENT_API struct ClockTime
507 Timestamp_get_clock_time_local(const struct Timestamp * const self);
508 
509 
513 PRESENT_API struct TimeDelta
515  const struct Timestamp * const self,
516  const struct Timestamp * const other);
517 
522 PRESENT_API struct TimeDelta
524  const struct Timestamp * const self,
525  const struct Timestamp * const other);
526 
527 
531 PRESENT_API void
533  struct Timestamp * const self,
534  const struct TimeDelta * const delta);
535 
539 PRESENT_API void
541  struct Timestamp * const self,
542  const struct DayDelta * const delta);
543 
547 PRESENT_API void
549  struct Timestamp * const self,
550  const struct MonthDelta * const delta);
551 
555 PRESENT_API void
557  struct Timestamp * const self,
558  const struct TimeDelta * const delta);
559 
563 PRESENT_API void
565  struct Timestamp * const self,
566  const struct DayDelta * const delta);
567 
571 PRESENT_API void
573  struct Timestamp * const self,
574  const struct MonthDelta * const delta);
575 
583 PRESENT_API short
585  const struct Timestamp * const lhs,
586  const struct Timestamp * const rhs);
587 
592 PRESENT_API present_bool
594  const struct Timestamp * const lhs,
595  const struct Timestamp * const rhs);
596 
601 PRESENT_API present_bool
603  const struct Timestamp * const lhs,
604  const struct Timestamp * const rhs);
605 
610 PRESENT_API present_bool
612  const struct Timestamp * const lhs,
613  const struct Timestamp * const rhs);
614 
619 PRESENT_API present_bool
621  const struct Timestamp * const lhs,
622  const struct Timestamp * const rhs);
623 
628 PRESENT_API present_bool
630  const struct Timestamp * const lhs,
631  const struct Timestamp * const rhs);
632 
633 #ifdef __cplusplus
634 }
635 #endif
636 
637 #endif /* _PRESENT_TIMESTAMP_H_ */
638 
void Timestamp_ptr_create_local(struct Timestamp *const result, const struct Date *const date, const struct ClockTime *const clock_time)
Create a new Timestamp based on a Date and ClockTime in the system's current local time zone...
struct Date Timestamp_get_date(const struct Timestamp *const self, const struct TimeDelta *const time_zone_offset)
Get the Date component of a Timestamp in a certain time zone (represented by an offset from UTC)...
struct Timestamp Timestamp_from_struct_tm_utc(const struct tm tm)
Create a new Timestamp based on a "struct tm" value (from C's time library) in Coordinated Universa...
void Timestamp_ptr_now(struct Timestamp *const result)
Create a new Timestamp representing the exact time right now.
void Timestamp_add_MonthDelta(struct Timestamp *const self, const struct MonthDelta *const delta)
Add a MonthDelta to a Timestamp.
struct tm Timestamp_get_struct_tm_local(const struct Timestamp *const self)
Convert a Timestamp to a "struct tm" (from C's time library) in the system's current local time zon...
struct Timestamp Timestamp_from_time_t(const time_t time)
Create a new Timestamp based on a "time_t" value (from C's time library).
Class or struct representing a time as seen on a clock.
Definition: clock-time.h:43
struct ClockTime Timestamp_get_clock_time_utc(const struct Timestamp *const self)
Get the ClockTime component of a Timestamp in Coordinated Universal Time.
struct Timestamp Timestamp_from_struct_tm_local(const struct tm tm)
Create a new Timestamp based on a "struct tm" value (from C's time library) in the system's current...
time_t Timestamp_get_time_t(const struct Timestamp *const self)
Convert a Timestamp to a "time_t" (from C's time library).
struct Timestamp Timestamp_epoch(void)
Create a new Timestamp representing the UNIX epoch (Jan.
present_bool Timestamp_equal(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Determine whether two Timestamp instances represent the exact same point in time (lhs == rhs)...
void Timestamp_ptr_from_time_t(struct Timestamp *const result, const time_t time)
Create a new Timestamp based on a "time_t" value (from C's time library).
void Timestamp_ptr_from_struct_tm(struct Timestamp *const result, const struct tm tm, const struct TimeDelta *const time_zone_offset)
Create a new Timestamp based on a "struct tm" value (from C's time library) in a certain time zone...
struct Timestamp Timestamp_now(void)
Create a new Timestamp representing the exact time right now.
struct TimeDelta Timestamp_difference(const struct Timestamp *const self, const struct Timestamp *const other)
Get the difference between two Timestamp instances as a TimeDelta.
present_bool Timestamp_greater_than_or_equal(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Determine whether a Timestamp occurs later than or at the same time as another Timestamp (lhs >= rhs)...
void Timestamp_subtract_MonthDelta(struct Timestamp *const self, const struct MonthDelta *const delta)
Subtract a MonthDelta from a Timestamp.
struct Timestamp Timestamp_create_utc(const struct Date *const date, const struct ClockTime *const clock_time)
Create a new Timestamp based on a Date and ClockTime in Coordinated Universal Time.
struct Date Timestamp_get_date_local(const struct Timestamp *const self)
Get the Date component of a Timestamp in the system's current local time zone.
void Timestamp_ptr_epoch(struct Timestamp *const result)
Create a new Timestamp representing the UNIX epoch (Jan.
struct tm Timestamp_get_struct_tm_utc(const struct Timestamp *const self)
Convert a Timestamp to a "struct tm" (from C's time library) in Coordinated Universal Time...
void Timestamp_ptr_from_struct_tm_local(struct Timestamp *const result, const struct tm tm)
Create a new Timestamp based on a "struct tm" value (from C's time library) in the system's current...
void Timestamp_ptr_from_struct_tm_utc(struct Timestamp *const result, const struct tm tm)
Create a new Timestamp based on a "struct tm" value (from C's time library) in Coordinated Universa...
void Timestamp_subtract_DayDelta(struct Timestamp *const self, const struct DayDelta *const delta)
Subtract a DayDelta from a Timestamp.
struct ClockTime Timestamp_get_clock_time(const struct Timestamp *const self, const struct TimeDelta *const time_zone_offset)
Get the ClockTime component of a Timestamp in a certain time zone (represented by an offset from UTC)...
struct Timestamp Timestamp_create(const struct Date *const date, const struct ClockTime *const clock_time, const struct TimeDelta *const time_zone_offset)
Create a new Timestamp based on a Date and ClockTime in a certain time zone.
struct ClockTime Timestamp_get_clock_time_local(const struct Timestamp *const self)
Get the ClockTime component of a Timestamp in the system's current local time zone.
void Timestamp_ptr_create_utc(struct Timestamp *const result, const struct Date *const date, const struct ClockTime *const clock_time)
Create a new Timestamp based on a Date and ClockTime in Coordinated Universal Time.
struct Timestamp Timestamp_create_local(const struct Date *const date, const struct ClockTime *const clock_time)
Create a new Timestamp based on a Date and ClockTime in the system's current local time zone...
present_bool Timestamp_less_than(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Determine whether a Timestamp occurs earlier than another Timestamp (lhs < rhs).
void Timestamp_ptr_create(struct Timestamp *const result, const struct Date *const date, const struct ClockTime *const clock_time, const struct TimeDelta *const time_zone_offset)
Create a new Timestamp based on a Date and ClockTime in a certain time zone.
struct TimeDelta Timestamp_absolute_difference(const struct Timestamp *const self, const struct Timestamp *const other)
Get the absolute difference between two Timestamp instances as a TimeDelta.
present_bool has_error
This will be true if there were any errors when creating this Timestamp.
Definition: timestamp.h:50
Class or struct representing a positive or negative delta of a number of days or weeks.
Definition: day-delta.h:40
present_bool Timestamp_greater_than(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Determine whether a Timestamp occurs later than another Timestamp (lhs > rhs).
Class or struct representing a calendar date.
Definition: date.h:59
Class or struct representing a positive or negative delta of a number of nanoseconds, seconds, minutes, hours, days, or weeks.
Definition: time-delta.h:40
Class or struct representing an exact point in time.
Definition: timestamp.h:44
struct tm Timestamp_get_struct_tm(const struct Timestamp *const self, const struct TimeDelta *const time_zone_offset)
Convert a Timestamp to a "struct tm" (from C's time library) in a certain time zone (represented by...
void Timestamp_add_DayDelta(struct Timestamp *const self, const struct DayDelta *const delta)
Add a DayDelta to a Timestamp.
short Timestamp_compare(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Compare two Timestamp instances.
void Timestamp_subtract_TimeDelta(struct Timestamp *const self, const struct TimeDelta *const delta)
Subtract a TimeDelta from a Timestamp.
void Timestamp_add_TimeDelta(struct Timestamp *const self, const struct TimeDelta *const delta)
Add a TimeDelta to a Timestamp.
struct Date Timestamp_get_date_utc(const struct Timestamp *const self)
Get the Date component of a Timestamp in Coordinated Universal Time.
present_bool Timestamp_less_than_or_equal(const struct Timestamp *const lhs, const struct Timestamp *const rhs)
Determine whether a Timestamp occurs earlier than or at the same time as another Timestamp (lhs <= rh...
bool present_bool
Definition: types.h:21
Class or struct representing a positive or negative delta of a number of months or years...
Definition: month-delta.h:38
struct Timestamp Timestamp_from_struct_tm(const struct tm tm, const struct TimeDelta *const time_zone_offset)
Create a new Timestamp based on a "struct tm" value (from C's time library) in a certain time zone...