Present  0.9
 All Classes Files Functions Variables Typedefs Friends Macros Pages
clock-time.h
Go to the documentation of this file.
1 /*
2  * Present - Date/Time Library
3  *
4  * Definition of the ClockTime 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-clock-time-data.h"
22 
23 #ifndef _PRESENT_CLOCK_TIME_H_
24 #define _PRESENT_CLOCK_TIME_H_
25 
26 /*
27  * Forward Declarations
28  */
29 
30 struct TimeDelta;
31 
32 /*
33  * C++ Class / C Struct Definition
34  */
35 
43 struct PRESENT_API ClockTime {
50 
57  struct {
58  unsigned int hour_out_of_range : 1,
59  minute_out_of_range : 1,
60  second_out_of_range : 1,
61  nanosecond_out_of_range : 1;
62  } errors;
63 
64  /* Internal data representation */
65  struct PresentClockTimeData data_;
66 
67 #ifdef __cplusplus
68 
69  static ClockTime create(int_hour hour);
70 
72  static ClockTime create(int_hour hour, int_minute minute);
73 
75  static ClockTime create(
76  int_hour hour,
77  int_minute minute,
78  int_second second);
79 
81  static ClockTime create(
82  int_hour hour,
83  int_minute minute,
84  int_second second,
85  int_nanosecond nanosecond);
86 
88  static ClockTime create_with_decimal_seconds(
89  int_hour hour,
90  int_minute minute,
91  double second);
92 
94  static ClockTime midnight(void);
95 
97  static ClockTime noon();
98 
100  int_hour hour() const;
101 
103  int_minute minute() const;
104 
106  int_second second() const;
107 
109  int_nanosecond nanosecond() const;
110 
112  double second_decimal() const;
113 
115  TimeDelta time_since_midnight() const;
116 
118  ClockTime & operator+=(const TimeDelta & delta);
120  ClockTime & operator-=(const TimeDelta & delta);
121 
123  friend const ClockTime operator+(
124  const ClockTime & lhs,
125  const TimeDelta & rhs);
127  friend const ClockTime operator+(
128  const TimeDelta & lhs,
129  const ClockTime & rhs);
130 
132  friend const ClockTime operator-(
133  const ClockTime & lhs,
134  const TimeDelta & rhs);
135 
137  static short compare(const ClockTime & lhs, const ClockTime & rhs);
138 
140  friend bool operator==(const ClockTime & lhs, const ClockTime & rhs);
141  friend bool operator!=(const ClockTime & lhs, const ClockTime & rhs);
142 
144  friend bool operator<(const ClockTime & lhs, const ClockTime & rhs);
146  friend bool operator<=(const ClockTime & lhs, const ClockTime & rhs);
148  friend bool operator>(const ClockTime & lhs, const ClockTime & rhs);
150  friend bool operator>=(const ClockTime & lhs, const ClockTime & rhs);
151 #endif
152 };
153 
154 /*
155  * C Method Declarations
156  */
157 
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
161 
172 PRESENT_API struct ClockTime
174 
179 PRESENT_API void
180 ClockTime_ptr_from_hour(struct ClockTime * const result, int_hour hour);
181 
193 PRESENT_API struct ClockTime
195 
200 PRESENT_API void
202  struct ClockTime * const result,
203  int_hour hour,
205 
219 PRESENT_API struct ClockTime
221  int_hour hour,
224 
229 PRESENT_API void
231  struct ClockTime * const result,
232  int_hour hour,
235 
252 PRESENT_API struct ClockTime
254  int_hour hour,
258 
263 PRESENT_API void
265  struct ClockTime * const result,
266  int_hour hour,
270 
283 #define ClockTime_create(...) \
284  PRESENT_OVERLOAD_MAX_4(__VA_ARGS__, \
285  ClockTime_from_hour_minute_second_nanosecond, \
286  ClockTime_from_hour_minute_second, \
287  ClockTime_from_hour_minute, \
288  ClockTime_from_hour, \
289  dummy)(__VA_ARGS__)
290 
304 #define ClockTime_ptr_create(result, ...) \
305  PRESENT_OVERLOAD_MAX_4(__VA_ARGS__, \
306  ClockTime_ptr_from_hour_minute_second_nanosecond, \
307  ClockTime_ptr_from_hour_minute_second, \
308  ClockTime_ptr_from_hour_minute, \
309  ClockTime_ptr_from_hour, \
310  dummy)(result, __VA_ARGS__)
311 
321 PRESENT_API struct ClockTime
323  int_hour hour,
325  double second);
326 
331 PRESENT_API void
333  struct ClockTime * const result,
334  int_hour hour,
336  double second);
337 
341 PRESENT_API struct ClockTime
342 ClockTime_midnight(void);
343 
348 PRESENT_API void
349 ClockTime_ptr_midnight(struct ClockTime * const result);
350 
354 PRESENT_API struct ClockTime
355 ClockTime_noon(void);
356 
361 PRESENT_API void
362 ClockTime_ptr_noon(struct ClockTime * const result);
363 
367 PRESENT_API int_hour
368 ClockTime_hour(const struct ClockTime * const self);
369 
373 PRESENT_API int_minute
374 ClockTime_minute(const struct ClockTime * const self);
375 
379 PRESENT_API int_second
380 ClockTime_second(const struct ClockTime * const self);
381 
386 PRESENT_API int_nanosecond
387 ClockTime_nanosecond(const struct ClockTime * const self);
388 
393 PRESENT_API double
394 ClockTime_second_decimal(const struct ClockTime * const self);
395 
399 PRESENT_API struct TimeDelta
400 ClockTime_time_since_midnight(const struct ClockTime * const self);
401 
408 PRESENT_API void
410  struct ClockTime * const self,
411  const struct TimeDelta * const delta);
412 
419 PRESENT_API void
421  struct ClockTime * const self,
422  const struct TimeDelta * const delta);
423 
431 PRESENT_API short
433  const struct ClockTime * const lhs,
434  const struct ClockTime * const rhs);
435 
439 PRESENT_API present_bool
441  const struct ClockTime * const lhs,
442  const struct ClockTime * const rhs);
443 
447 PRESENT_API present_bool
449  const struct ClockTime * const lhs,
450  const struct ClockTime * const rhs);
451 
456 PRESENT_API present_bool
458  const struct ClockTime * const lhs,
459  const struct ClockTime * const rhs);
460 
464 PRESENT_API present_bool
466  const struct ClockTime * const lhs,
467  const struct ClockTime * const rhs);
468 
473 PRESENT_API present_bool
475  const struct ClockTime * const lhs,
476  const struct ClockTime * const rhs);
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 
482 #endif /* _PRESENT_CLOCK_TIME_H_ */
483 
present_int64 int_nanosecond
Definition: types.h:60
present_bool ClockTime_equal(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Determine whether two ClockTime instances are equal (lhs == rhs).
present_bool ClockTime_greater_than(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Determine whether a ClockTime is later than another ClockTime (lhs > rhs).
void ClockTime_ptr_midnight(struct ClockTime *const result)
Create a new ClockTime initialized to midnight (00:00).
int_hour ClockTime_hour(const struct ClockTime *const self)
Get the hour component of a ClockTime (0 to 23, inclusive).
int_hour hour() const
Get the hour component of a ClockTime (0 to 23, inclusive).
int_second ClockTime_second(const struct ClockTime *const self)
Get the second component of a ClockTime (0 to 59, inclusive).
void ClockTime_ptr_from_hour(struct ClockTime *const result, int_hour hour)
Create a new ClockTime based on an hour of the day.
Class or struct representing a time as seen on a clock.
Definition: clock-time.h:43
void ClockTime_ptr_from_hour_minute_second(struct ClockTime *const result, int_hour hour, int_minute minute, int_second second)
Create a new ClockTime based on an hour, a minute, and a second.
struct ClockTime ClockTime_from_hour(int_hour hour)
Create a new ClockTime based on an hour of the day.
present_int16 int_second
Definition: types.h:59
present_bool ClockTime_less_than(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Determine whether a ClockTime is earlier than another ClockTime (lhs < rhs).
int_minute ClockTime_minute(const struct ClockTime *const self)
Get the minute component of a ClockTime (0 to 59, inclusive).
void ClockTime_ptr_from_hour_minute_second_nanosecond(struct ClockTime *const result, int_hour hour, int_minute minute, int_second second, int_nanosecond nanosecond)
Create a new ClockTime based on an hour, a minute, a second, and a nanosecond.
struct TimeDelta ClockTime_time_since_midnight(const struct ClockTime *const self)
Get a TimeDelta with the time since midnight of a ClockTime.
present_int16 int_minute
Definition: types.h:58
present_bool ClockTime_greater_than_or_equal(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Determine whether a ClockTime is later than or the same as another ClockTime (lhs >= rhs)...
present_bool has_error
This will be true if there were any errors when creating this ClockTime.
Definition: clock-time.h:49
void ClockTime_ptr_from_hour_minute(struct ClockTime *const result, int_hour hour, int_minute minute)
Create a new ClockTime based on an hour and a minute.
void ClockTime_subtract_TimeDelta(struct ClockTime *const self, const struct TimeDelta *const delta)
Subtract a TimeDelta from a ClockTime.
struct ClockTime ClockTime_from_hour_minute_second_nanosecond(int_hour hour, int_minute minute, int_second second, int_nanosecond nanosecond)
Create a new ClockTime based on an hour, a minute, a second, and a nanosecond.
present_int16 int_hour
Definition: types.h:57
int_nanosecond nanosecond() const
Get the nanosecond component of a ClockTime (less than 10^9, the number of nanoseconds in a second)...
double ClockTime_second_decimal(const struct ClockTime *const self)
Get the second component of a ClockTime as a decimal, based on both the second and nanosecond compone...
void ClockTime_ptr_create_with_decimal_seconds(struct ClockTime *const result, int_hour hour, int_minute minute, double second)
Create a new ClockTime based on an hour, a minute, and a decimal second.
struct ClockTime ClockTime_noon(void)
Create a new ClockTime initialized to noon (12:00).
present_bool ClockTime_less_than_or_equal(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Determine whether a ClockTime is earlier than or or the same as another ClockTime (lhs <= rhs)...
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
int_minute minute() const
Get the minute component of a ClockTime (0 to 59, inclusive).
struct ClockTime ClockTime_create_with_decimal_seconds(int_hour hour, int_minute minute, double second)
Create a new ClockTime based on an hour, a minute, and a decimal second.
short ClockTime_compare(const struct ClockTime *const lhs, const struct ClockTime *const rhs)
Compare two ClockTime instances.
void ClockTime_add_TimeDelta(struct ClockTime *const self, const struct TimeDelta *const delta)
Add a TimeDelta to a ClockTime.
void ClockTime_ptr_noon(struct ClockTime *const result)
Create a new ClockTime initialized to noon (12:00).
int_second second() const
Get the second component of a ClockTime (0 to 59, inclusive).
struct ClockTime ClockTime_midnight(void)
Create a new ClockTime initialized to midnight (00:00).
struct ClockTime ClockTime_from_hour_minute_second(int_hour hour, int_minute minute, int_second second)
Create a new ClockTime based on an hour, a minute, and a second.
bool present_bool
Definition: types.h:21
struct ClockTime ClockTime_from_hour_minute(int_hour hour, int_minute minute)
Create a new ClockTime based on an hour and a minute.
int_nanosecond ClockTime_nanosecond(const struct ClockTime *const self)
Get the nanosecond component of a ClockTime (less than 10^9, the number of nanoseconds in a second)...