New doc system done for core
[io.git] / libs / basekit / source / Duration.c
blob7d2134d74e3a0d2f255a8addb8a242af26c29ad6
1 //metadoc Duration copyright Steve Dekorte 2002
2 //metadoc Duration license BSD revised
4 #define DURATION_C
5 #include "Duration.h"
6 #undef DURATION_C
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
12 typedef struct
14 double years;
15 double days;
16 double hours;
17 double minutes;
18 double seconds;
19 } DurationComponents;
21 Duration *Duration_new(void)
23 Duration *self = (Duration *)io_calloc(1, sizeof(Duration));
24 return self;
27 Duration *Duration_newWithSeconds_(double s)
29 Duration *self = Duration_new();
30 self->seconds = s;
31 return self;
34 void Duration_copy_(Duration *self, const Duration *other)
36 memcpy(self, other, sizeof(Duration));
39 void Duration_free(Duration *self)
41 io_free(self);
44 int Duration_compare(const Duration *self, const Duration *other)
46 if (self->seconds == other->seconds)
48 return 0;
51 return self->seconds > other->seconds ? 1 : -1;
54 // components --------------------------------------------------------
56 #define SECONDS_IN_YEAR (60 * 60 * 24 * 365)
57 #define SECONDS_IN_DAY (60 * 60 * 24)
58 #define SECONDS_IN_HOUR (60 * 60)
59 #define SECONDS_IN_MINUTE (60)
61 DurationComponents Duration_asComponents(const Duration *self)
63 DurationComponents c;
64 double t = self->seconds;
65 c.years = (int)(t / SECONDS_IN_YEAR); t -= (int)(c.years * SECONDS_IN_YEAR);
66 c.days = (int)(t / SECONDS_IN_DAY); t -= (int)(c.days * SECONDS_IN_DAY);
67 c.hours = (int)(t / SECONDS_IN_HOUR); t -= (int)(c.hours * SECONDS_IN_HOUR);
68 c.minutes = (int)(t / SECONDS_IN_MINUTE); t -= (int)(c.minutes * SECONDS_IN_MINUTE);
69 c.seconds = (t);
70 return c;
73 void Duration_fromComponents_(Duration *self, DurationComponents c)
75 double t = c.years * SECONDS_IN_YEAR;
76 t += c.days * SECONDS_IN_DAY;
77 t += c.hours * SECONDS_IN_HOUR;
78 t += c.minutes * SECONDS_IN_MINUTE;
79 t += c.seconds;
80 self->seconds = t;
83 // years --------------------------------------------------------
85 int Duration_years(const Duration *self)
87 return (int)Duration_asComponents(self).years;
90 void Duration_setYears_(Duration *self, double y)
92 DurationComponents c = Duration_asComponents(self);
93 c.years = y;
94 Duration_fromComponents_(self, c);
97 // days --------------------------------------------------------
99 int Duration_days(const Duration *self)
101 return (int)Duration_asComponents(self).days;
104 void Duration_setDays_(Duration *self, double d)
106 DurationComponents c = Duration_asComponents(self);
107 c.days = d;
108 Duration_fromComponents_(self, c);
111 // hours --------------------------------------------------------
113 int Duration_hours(const Duration *self)
115 return (int)Duration_asComponents(self).hours;
118 void Duration_setHours_(Duration *self, double m)
120 DurationComponents c = Duration_asComponents(self);
121 c.hours = m;
122 Duration_fromComponents_(self, c);
125 // minutes --------------------------------------------------------
127 int Duration_minutes(const Duration *self)
129 return (int)Duration_asComponents(self).minutes;
132 void Duration_setMinutes_(Duration *self, double m)
134 DurationComponents c = Duration_asComponents(self);
135 c.minutes = m;
136 Duration_fromComponents_(self, c);
139 // seconds --------------------------------------------------------
141 double Duration_seconds(const Duration *self)
143 return Duration_asComponents(self).seconds;
146 void Duration_setSeconds_(Duration *self, double s)
148 DurationComponents c = Duration_asComponents(self);
149 c.seconds = s;
150 Duration_fromComponents_(self, c);
153 // total seconds --------------------------------------------------------
155 double Duration_asSeconds(const Duration *self)
157 return self->seconds;
160 void Duration_fromSeconds_(Duration *self, double s)
162 self->seconds = s;
165 // strings --------------------------------------------------------
167 UArray *Duration_asUArrayWithFormat_(const Duration *self, const char *format)
169 DurationComponents c = Duration_asComponents(self);
170 char s[128];
171 UArray *ba = UArray_newWithCString_(format?format:"%Y years %d days %H:%M:%S");
173 snprintf(s, 128, "%i", (int)c.years);
174 UArray_replaceCString_withCString_(ba, "%Y", s);
176 snprintf(s, 128, "%04i", (int)c.years);
177 UArray_replaceCString_withCString_(ba, "%y", s);
179 snprintf(s, 128, "%02i", (int)c.days);
180 UArray_replaceCString_withCString_(ba, "%d", s);
182 snprintf(s, 128, "%02i", (int)c.hours);
183 UArray_replaceCString_withCString_(ba, "%H", s);
185 snprintf(s, 128, "%02i", (int)c.minutes);
186 UArray_replaceCString_withCString_(ba, "%M", s);
188 snprintf(s, 128, "%02f", c.seconds);
189 UArray_replaceCString_withCString_(ba, "%S", s);
191 return ba;
194 void Duration_print(const Duration *self)
196 UArray *ba = Duration_asUArrayWithFormat_(self, NULL);
197 UArray_print(ba);
198 UArray_free(ba);
201 // math --------------------------------------------------------
203 void Duration_add_(Duration *self, const Duration *other)
205 self->seconds += other->seconds;
208 void Duration_subtract_(Duration *self, const Duration *other)
210 self->seconds -= other->seconds;