Present  0.9
 All Classes Files Functions Variables Typedefs Friends Macros Pages
time-delta.h
Go to the documentation of this file.
1 /*
2  * Present - Date/Time Library
3  *
4  * Definition of the TimeDelta 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-time-delta-data.h"
22 
23 #ifndef _PRESENT_TIME_DELTA_H_
24 #define _PRESENT_TIME_DELTA_H_
25 
26 /*
27  * Forward Declarations
28  */
29 
30 struct DayDelta;
31 
32 /*
33  * C++ Class / C Struct Definition
34  */
35 
40 struct PRESENT_API TimeDelta {
41  /* Internal data representation */
42  struct PresentTimeDeltaData data_;
43 
44 #ifdef __cplusplus
45 
46  static TimeDelta from_nanoseconds(int_delta nanoseconds);
47 
49  static TimeDelta from_microseconds(int_delta microseconds);
50 
52  static TimeDelta from_milliseconds(int_delta milliseconds);
53 
55  static TimeDelta from_seconds(int_delta seconds);
56 
58  static TimeDelta from_minutes(int_delta minutes);
59 
61  static TimeDelta from_hours(int_delta hours);
62 
64  static TimeDelta from_days(int_delta days);
65 
67  static TimeDelta from_weeks(int_delta weeks);
68 
70  static TimeDelta zero();
71 
73  int_delta nanoseconds() const;
74 
76  int_delta microseconds() const;
78  double microseconds_decimal() const;
79 
81  int_delta milliseconds() const;
83  double milliseconds_decimal() const;
84 
86  int_delta seconds() const;
88  double seconds_decimal() const;
89 
91  int_delta minutes() const;
93  double minutes_decimal() const;
94 
96  int_delta hours() const;
98  double hours_decimal() const;
99 
101  int_delta days() const;
103  double days_decimal() const;
104 
106  int_delta weeks() const;
108  double weeks_decimal() const;
109 
111  DayDelta to_DayDelta_truncated() const;
112 
114  DayDelta to_DayDelta_rounded() const;
115 
117  DayDelta to_DayDelta_abs_ceil() const;
118 
120  bool is_negative() const;
121 
123  void negate();
124 
129  TimeDelta operator-() const;
130 
132  TimeDelta & operator*=(const long & scale_factor);
134  TimeDelta & operator*=(const double & scale_factor);
136  TimeDelta & operator/=(const long & scale_factor);
138  TimeDelta & operator/=(const double & scale_factor);
139 
141  friend const TimeDelta operator*(
142  const TimeDelta & delta,
143  const long & scale_factor);
145  friend const TimeDelta operator*(
146  const TimeDelta & delta,
147  const double & scale_factor);
149  friend const TimeDelta operator/(
150  const TimeDelta & delta,
151  const long & scale_factor);
153  friend const TimeDelta operator/(
154  const TimeDelta & delta,
155  const double & scale_factor);
156 
158  TimeDelta & operator+=(const TimeDelta & other);
160  TimeDelta & operator+=(const DayDelta & other);
162  TimeDelta & operator-=(const TimeDelta & other);
164  TimeDelta & operator-=(const DayDelta & other);
165 
167  friend const TimeDelta operator+(
168  const TimeDelta & lhs,
169  const TimeDelta & rhs);
171  friend const TimeDelta operator+(
172  const TimeDelta & lhs,
173  const DayDelta & rhs);
175  friend const TimeDelta operator+(
176  const DayDelta & lhs,
177  const TimeDelta & rhs);
179  friend const TimeDelta operator-(
180  const TimeDelta & lhs,
181  const TimeDelta & rhs);
183  friend const TimeDelta operator-(
184  const TimeDelta & lhs,
185  const DayDelta & rhs);
187  friend const TimeDelta operator-(
188  const DayDelta & lhs,
189  const TimeDelta & rhs);
190 
192  static short compare(const TimeDelta & lhs, const TimeDelta & rhs);
193 
195  static short compare(const TimeDelta & lhs, const DayDelta & rhs);
197  static short compare(const DayDelta & lhs, const TimeDelta & rhs);
198 
200  friend bool operator==(const TimeDelta & lhs, const TimeDelta & rhs);
202  friend bool operator==(const TimeDelta & lhs, const DayDelta & rhs);
203 
204  friend bool operator!=(const TimeDelta & lhs, const TimeDelta & rhs);
205  friend bool operator!=(const TimeDelta & lhs, const DayDelta & rhs);
206 
208  friend bool operator<(const TimeDelta & lhs, const TimeDelta & rhs);
210  friend bool operator<(const TimeDelta & lhs, const DayDelta & rhs);
211 
213  friend bool operator<=(const TimeDelta & lhs, const TimeDelta & rhs);
215  friend bool operator<=(const TimeDelta & lhs, const DayDelta & rhs);
216 
218  friend bool operator>(const TimeDelta & lhs, const TimeDelta & rhs);
220  friend bool operator>(const TimeDelta & lhs, const DayDelta & rhs);
221 
223  friend bool operator>=(const TimeDelta & lhs, const TimeDelta & rhs);
225  friend bool operator>=(const TimeDelta & lhs, const DayDelta & rhs);
226 #endif
227 };
228 
229 /*
230  * C Method Declarations
231  */
232 
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236 
241 PRESENT_API struct TimeDelta
243 
248 PRESENT_API void
250  struct TimeDelta * const result,
252 
257 PRESENT_API struct TimeDelta
259 
264 PRESENT_API void
266  struct TimeDelta * const result,
268 
273 PRESENT_API struct TimeDelta
275 
280 PRESENT_API void
282  struct TimeDelta * const result,
284 
288 PRESENT_API struct TimeDelta
290 
295 PRESENT_API void
296 TimeDelta_ptr_from_seconds(struct TimeDelta * const result, int_delta seconds);
297 
301 PRESENT_API struct TimeDelta
303 
308 PRESENT_API void
309 TimeDelta_ptr_from_minutes(struct TimeDelta * const result, int_delta minutes);
310 
314 PRESENT_API struct TimeDelta
316 
321 PRESENT_API void
322 TimeDelta_ptr_from_hours(struct TimeDelta * const result, int_delta hours);
323 
327 PRESENT_API struct TimeDelta
329 
334 PRESENT_API void
335 TimeDelta_ptr_from_days(struct TimeDelta * const result, int_delta days);
336 
340 PRESENT_API struct TimeDelta
342 
347 PRESENT_API void
348 TimeDelta_ptr_from_weeks(struct TimeDelta * const result, int_delta weeks);
349 
353 PRESENT_API struct TimeDelta
354 TimeDelta_zero(void);
355 
360 PRESENT_API void
361 TimeDelta_ptr_zero(struct TimeDelta * const result);
362 
366 PRESENT_API int_delta
367 TimeDelta_nanoseconds(const struct TimeDelta * const self);
368 
374 PRESENT_API int_delta
375 TimeDelta_microseconds(const struct TimeDelta * const self);
376 
381 PRESENT_API double
382 TimeDelta_microseconds_decimal(const struct TimeDelta * const self);
383 
389 PRESENT_API int_delta
390 TimeDelta_milliseconds(const struct TimeDelta * const self);
391 
396 PRESENT_API double
397 TimeDelta_milliseconds_decimal(const struct TimeDelta * const self);
398 
404 PRESENT_API int_delta
405 TimeDelta_seconds(const struct TimeDelta * const self);
406 
411 PRESENT_API double
412 TimeDelta_seconds_decimal(const struct TimeDelta * const self);
413 
419 PRESENT_API int_delta
420 TimeDelta_minutes(const struct TimeDelta * const self);
421 
426 PRESENT_API double
427 TimeDelta_minutes_decimal(const struct TimeDelta * const self);
428 
434 PRESENT_API int_delta
435 TimeDelta_hours(const struct TimeDelta * const self);
436 
441 PRESENT_API double
442 TimeDelta_hours_decimal(const struct TimeDelta * const self);
443 
449 PRESENT_API int_delta
450 TimeDelta_days(const struct TimeDelta * const self);
451 
456 PRESENT_API double
457 TimeDelta_days_decimal(const struct TimeDelta * const self);
458 
464 PRESENT_API int_delta
465 TimeDelta_weeks(const struct TimeDelta * const self);
466 
471 PRESENT_API double
472 TimeDelta_weeks_decimal(const struct TimeDelta * const self);
473 
478 PRESENT_API struct DayDelta
479 TimeDelta_to_DayDelta_truncated(const struct TimeDelta * const self);
480 
485 PRESENT_API struct DayDelta
486 TimeDelta_to_DayDelta_rounded(const struct TimeDelta * const self);
487 
492 PRESENT_API struct DayDelta
493 TimeDelta_to_DayDelta_abs_ceil(const struct TimeDelta * const self);
494 
498 PRESENT_API present_bool
499 TimeDelta_is_negative(const struct TimeDelta * const self);
500 
505 PRESENT_API void
506 TimeDelta_negate(struct TimeDelta * const self);
507 
511 PRESENT_API void
512 TimeDelta_multiply_by(struct TimeDelta * const self, const long scale_factor);
513 
517 PRESENT_API void
519  struct TimeDelta * const self,
520  const double scale_factor);
521 
525 PRESENT_API void
526 TimeDelta_divide_by(struct TimeDelta * const self, const long scale_factor);
527 
531 PRESENT_API void
533  struct TimeDelta * const self,
534  const double scale_factor);
535 
540 PRESENT_API void
542  struct TimeDelta * const self,
543  const struct TimeDelta * const other);
544 
549 PRESENT_API void
551  struct TimeDelta * const self,
552  const struct DayDelta * const other);
553 
558 PRESENT_API void
560  struct TimeDelta * const self,
561  const struct TimeDelta * const other);
562 
567 PRESENT_API void
569  struct TimeDelta * const self,
570  const struct DayDelta * const other);
571 
579 PRESENT_API short
581  const struct TimeDelta * const lhs,
582  const struct TimeDelta * const rhs);
583 
591 PRESENT_API short
593  const struct TimeDelta * const lhs,
594  const struct DayDelta * const rhs);
595 
599 PRESENT_API present_bool
601  const struct TimeDelta * const lhs,
602  const struct TimeDelta * const rhs);
603 
607 PRESENT_API present_bool
609  const struct TimeDelta * const lhs,
610  const struct DayDelta * const rhs);
611 
615 PRESENT_API present_bool
617  const struct TimeDelta * const lhs,
618  const struct TimeDelta * const rhs);
619 
623 PRESENT_API present_bool
625  const struct TimeDelta * const lhs,
626  const struct DayDelta * const rhs);
627 
632 PRESENT_API present_bool
634  const struct TimeDelta * const lhs,
635  const struct TimeDelta * const rhs);
636 
641 PRESENT_API present_bool
643  const struct TimeDelta * const lhs,
644  const struct DayDelta * const rhs);
645 
649 PRESENT_API present_bool
651  const struct TimeDelta * const lhs,
652  const struct TimeDelta * const rhs);
653 
657 PRESENT_API present_bool
659  const struct TimeDelta * const lhs,
660  const struct DayDelta * const rhs);
661 
666 PRESENT_API present_bool
668  const struct TimeDelta * const lhs,
669  const struct TimeDelta * const rhs);
670 
675 PRESENT_API present_bool
677  const struct TimeDelta * const lhs,
678  const struct DayDelta * const rhs);
679 
680 #ifdef __cplusplus
681 }
682 #endif
683 
684 #endif /* _PRESENT_TIME_DELTA_H_ */
685 
void TimeDelta_ptr_from_minutes(struct TimeDelta *const result, int_delta minutes)
Create a new TimeDelta based on a positive or negative number of minutes.
struct TimeDelta TimeDelta_from_nanoseconds(int_delta nanoseconds)
Create a new TimeDelta based on a positive or negative number of nanoseconds.
double TimeDelta_microseconds_decimal(const struct TimeDelta *const self)
Get the number of microseconds represented by a TimeDelta, with a fractional part if necessary...
struct TimeDelta TimeDelta_zero(void)
Create a new TimeDelta initialized to zero time.
struct TimeDelta TimeDelta_from_minutes(int_delta minutes)
Create a new TimeDelta based on a positive or negative number of minutes.
struct TimeDelta TimeDelta_from_microseconds(int_delta microseconds)
Create a new TimeDelta based on a positive or negative number of microseconds.
int_delta TimeDelta_minutes(const struct TimeDelta *const self)
Get the number of minutes represented by a TimeDelta.
void TimeDelta_add_DayDelta(struct TimeDelta *const self, const struct DayDelta *const other)
Add a DayDelta to a TimeDelta.
void TimeDelta_ptr_from_milliseconds(struct TimeDelta *const result, int_delta milliseconds)
Create a new TimeDelta based on a positive or negative number of milliseconds.
present_bool TimeDelta_less_than_or_equal(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Determine whether a TimeDelta is less than or equal to another TimeDelta (lhs <= rhs).
present_bool TimeDelta_equal(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Determine whether two TimeDelta instances are equal (lhs == rhs).
present_bool TimeDelta_less_than_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Determine whether a TimeDelta is less than a DayDelta (lhs < rhs).
double TimeDelta_days_decimal(const struct TimeDelta *const self)
Get the number of days represented by a TimeDelta, with a fractional part if necessary.
struct TimeDelta TimeDelta_from_hours(int_delta hours)
Create a new TimeDelta based on a positive or negative number of hours.
struct DayDelta TimeDelta_to_DayDelta_truncated(const struct TimeDelta *const self)
Convert a TimeDelta into a DayDelta, rounding towards zero if the TimeDelta does not represent an exa...
double TimeDelta_hours_decimal(const struct TimeDelta *const self)
Get the number of hours represented by a TimeDelta, with a fractional part if necessary.
void TimeDelta_add(struct TimeDelta *const self, const struct TimeDelta *const other)
Add another TimeDelta to a TimeDelta.
int_delta TimeDelta_milliseconds(const struct TimeDelta *const self)
Get the number of milliseconds represented by a TimeDelta.
present_bool TimeDelta_greater_than(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Determine whether a TimeDelta is greater than another TimeDelta (lhs > rhs).
present_int64 int_delta
Definition: types.h:86
void TimeDelta_negate(struct TimeDelta *const self)
Negate a TimeDelta, making it negative if it was positive or positive if it was negative.
void TimeDelta_multiply_by_decimal(struct TimeDelta *const self, const double scale_factor)
Scale a TimeDelta by multiplying it by a floating point scale factor.
void TimeDelta_subtract(struct TimeDelta *const self, const struct TimeDelta *const other)
Subtract another TimeDelta from a TimeDelta.
struct TimeDelta TimeDelta_from_seconds(int_delta seconds)
Create a new TimeDelta based on a positive or negative number of seconds.
int_delta seconds() const
Get the number of seconds represented by a TimeDelta.
int_delta TimeDelta_seconds(const struct TimeDelta *const self)
Get the number of seconds represented by a TimeDelta.
present_bool TimeDelta_less_than_or_equal_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Determine whether a TimeDelta is less than or equal to a DayDelta (lhs <= rhs).
present_bool TimeDelta_greater_than_or_equal_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Determine whether a TimeDelta is greater than or equal to a DayDelta (lhs >= rhs).
double TimeDelta_weeks_decimal(const struct TimeDelta *const self)
Get the number of weeks represented by a TimeDelta, with a fractional part if necessary.
double TimeDelta_minutes_decimal(const struct TimeDelta *const self)
Get the number of minutes represented by a TimeDelta, with a fractional part if necessary.
int_delta microseconds() const
Get the number of microseconds represented by a TimeDelta.
int_delta milliseconds() const
Get the number of milliseconds represented by a TimeDelta.
int_delta TimeDelta_weeks(const struct TimeDelta *const self)
Get the number of weeks represented by a TimeDelta.
short TimeDelta_compare_to_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Compare a TimeDelta to a DayDelta.
void TimeDelta_divide_by(struct TimeDelta *const self, const long scale_factor)
Scale a TimeDelta by dividing it by an integer scale factor.
struct DayDelta TimeDelta_to_DayDelta_rounded(const struct TimeDelta *const self)
Convert a TimeDelta into a DayDelta, rounding to the nearest day if the TimeDelta does not represent ...
void TimeDelta_ptr_from_hours(struct TimeDelta *const result, int_delta hours)
Create a new TimeDelta based on a positive or negative number of hours.
void TimeDelta_ptr_from_weeks(struct TimeDelta *const result, int_delta weeks)
Create a new TimeDelta based on a positive or negative number of weeks.
void TimeDelta_ptr_from_seconds(struct TimeDelta *const result, int_delta seconds)
Create a new TimeDelta based on a positive or negative number of seconds.
present_bool TimeDelta_equal_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Determine whether a TimeDelta is equal to a DayDelta (lhs == rhs).
present_bool TimeDelta_greater_than_or_equal(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Determine whether a TimeDelta is greater than or equal to another TimeDelta (lhs >= rhs)...
int_delta nanoseconds() const
Get the number of nanoseconds represented by a TimeDelta.
int_delta hours() const
Get the number of hours represented by a TimeDelta.
int_delta minutes() const
Get the number of minutes represented by a TimeDelta.
void TimeDelta_ptr_zero(struct TimeDelta *const result)
Create a new TimeDelta initialized to zero time.
int_delta TimeDelta_days(const struct TimeDelta *const self)
Get the number of days represented by a TimeDelta.
void TimeDelta_ptr_from_days(struct TimeDelta *const result, int_delta days)
Create a new TimeDelta based on a positive or negative number of days.
void TimeDelta_ptr_from_microseconds(struct TimeDelta *const result, int_delta microseconds)
Create a new TimeDelta based on a positive or negative number of microseconds.
void TimeDelta_ptr_from_nanoseconds(struct TimeDelta *const result, int_delta nanoseconds)
Create a new TimeDelta based on a positive or negative number of nanoseconds.
Class or struct representing a positive or negative delta of a number of days or weeks.
Definition: day-delta.h:40
void TimeDelta_subtract_DayDelta(struct TimeDelta *const self, const struct DayDelta *const other)
Subtract a DayDelta from a TimeDelta.
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
double TimeDelta_milliseconds_decimal(const struct TimeDelta *const self)
Get the number of milliseconds represented by a TimeDelta, with a fractional part if necessary...
present_bool TimeDelta_greater_than_DayDelta(const struct TimeDelta *const lhs, const struct DayDelta *const rhs)
Determine whether a TimeDelta is greater than a DayDelta (lhs > rhs).
struct TimeDelta TimeDelta_from_days(int_delta days)
Create a new TimeDelta based on a positive or negative number of days.
int_delta TimeDelta_microseconds(const struct TimeDelta *const self)
Get the number of microseconds represented by a TimeDelta.
struct TimeDelta TimeDelta_from_weeks(int_delta weeks)
Create a new TimeDelta based on a positive or negative number of weeks.
present_bool TimeDelta_less_than(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Determine whether a TimeDelta is less than another TimeDelta (lhs < rhs).
void TimeDelta_divide_by_decimal(struct TimeDelta *const self, const double scale_factor)
Scale a TimeDelta by dividing it by a floating point scale factor.
double TimeDelta_seconds_decimal(const struct TimeDelta *const self)
Get the number of seconds represented by a TimeDelta, with a fractional part if necessary.
int_delta TimeDelta_hours(const struct TimeDelta *const self)
Get the number of hours represented by a TimeDelta.
present_bool TimeDelta_is_negative(const struct TimeDelta *const self)
Determine whether a TimeDelta is negative.
short TimeDelta_compare(const struct TimeDelta *const lhs, const struct TimeDelta *const rhs)
Compare two TimeDelta instances.
void TimeDelta_multiply_by(struct TimeDelta *const self, const long scale_factor)
Scale a TimeDelta by multiplying it by an integer scale factor.
int_delta days() const
Get the number of days represented by a TimeDelta.
struct TimeDelta TimeDelta_from_milliseconds(int_delta milliseconds)
Create a new TimeDelta based on a positive or negative number of milliseconds.
int_delta TimeDelta_nanoseconds(const struct TimeDelta *const self)
Get the number of nanoseconds represented by a TimeDelta.
bool present_bool
Definition: types.h:21
struct DayDelta TimeDelta_to_DayDelta_abs_ceil(const struct TimeDelta *const self)
Convert a TimeDelta into a DayDelta, rounding away from zero if the TimeDelta does not represent an e...
int_delta weeks() const
Get the number of weeks represented by a TimeDelta.