SystemCall run(block) can now exit the run if it returns false
[io/quag.git] / libs / basekit / source / Duration.c
blob10c321361a0758efbcf304d54a4bdee193fdac7a
1 /*#io
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 */
6 #define DURATION_C
7 #include "Duration.h"
8 #undef DURATION_C
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <math.h>
14 typedef struct
16 double years;
17 double days;
18 double hours;
19 double minutes;
20 double seconds;
21 } DurationComponents;
23 Duration *Duration_new(void)
25 Duration *self = (Duration *)io_calloc(1, sizeof(Duration));
26 return self;
29 Duration *Duration_newWithSeconds_(double s)
31 Duration *self = Duration_new();
32 self->seconds = s;
33 return self;
36 void Duration_copy_(Duration *self, const Duration *other)
38 memcpy(self, other, sizeof(Duration));
41 void Duration_free(Duration *self)
43 io_free(self);
46 int Duration_compare(const Duration *self, const Duration *other)
48 if (self->seconds == other->seconds)
50 return 0;
53 return self->seconds > other->seconds ? 1 : -1;
56 // components --------------------------------------------------------
58 #define SECONDS_IN_YEAR (60 * 60 * 24 * 365)
59 #define SECONDS_IN_DAY (60 * 60 * 24)
60 #define SECONDS_IN_HOUR (60 * 60)
61 #define SECONDS_IN_MINUTE (60)
63 DurationComponents Duration_asComponents(const Duration *self)
65 DurationComponents c;
66 double t = self->seconds;
67 c.years = (int)(t / SECONDS_IN_YEAR); t -= (int)(c.years * SECONDS_IN_YEAR);
68 c.days = (int)(t / SECONDS_IN_DAY); t -= (int)(c.days * SECONDS_IN_DAY);
69 c.hours = (int)(t / SECONDS_IN_HOUR); t -= (int)(c.hours * SECONDS_IN_HOUR);
70 c.minutes = (int)(t / SECONDS_IN_MINUTE); t -= (int)(c.minutes * SECONDS_IN_MINUTE);
71 c.seconds = (t);
72 return c;
75 void Duration_fromComponents_(Duration *self, DurationComponents c)
77 double t = c.years * SECONDS_IN_YEAR;
78 t += c.days * SECONDS_IN_DAY;
79 t += c.hours * SECONDS_IN_HOUR;
80 t += c.minutes * SECONDS_IN_MINUTE;
81 t += c.seconds;
82 self->seconds = t;
85 // years --------------------------------------------------------
87 int Duration_years(const Duration *self)
89 return (int)Duration_asComponents(self).years;
92 void Duration_setYears_(Duration *self, double y)
94 DurationComponents c = Duration_asComponents(self);
95 c.years = y;
96 Duration_fromComponents_(self, c);
99 // days --------------------------------------------------------
101 int Duration_days(const Duration *self)
103 return (int)Duration_asComponents(self).days;
106 void Duration_setDays_(Duration *self, double d)
108 DurationComponents c = Duration_asComponents(self);
109 c.days = d;
110 Duration_fromComponents_(self, c);
113 // hours --------------------------------------------------------
115 int Duration_hours(const Duration *self)
117 return (int)Duration_asComponents(self).hours;
120 void Duration_setHours_(Duration *self, double m)
122 DurationComponents c = Duration_asComponents(self);
123 c.hours = m;
124 Duration_fromComponents_(self, c);
127 // minutes --------------------------------------------------------
129 int Duration_minutes(const Duration *self)
131 return (int)Duration_asComponents(self).minutes;
134 void Duration_setMinutes_(Duration *self, double m)
136 DurationComponents c = Duration_asComponents(self);
137 c.minutes = m;
138 Duration_fromComponents_(self, c);
141 // seconds --------------------------------------------------------
143 double Duration_seconds(const Duration *self)
145 return Duration_asComponents(self).seconds;
148 void Duration_setSeconds_(Duration *self, double s)
150 DurationComponents c = Duration_asComponents(self);
151 c.seconds = s;
152 Duration_fromComponents_(self, c);
155 // total seconds --------------------------------------------------------
157 double Duration_asSeconds(const Duration *self)
159 return self->seconds;
162 void Duration_fromSeconds_(Duration *self, double s)
164 self->seconds = s;
167 // strings --------------------------------------------------------
169 UArray *Duration_asUArrayWithFormat_(const Duration *self, const char *format)
171 DurationComponents c = Duration_asComponents(self);
172 char s[128];
173 UArray *ba = UArray_newWithCString_(format?format:"%Y years %d days %H:%M:%S");
175 snprintf(s, 128, "%i", (int)c.years);
176 UArray_replaceCString_withCString_(ba, "%Y", s);
178 snprintf(s, 128, "%04i", (int)c.years);
179 UArray_replaceCString_withCString_(ba, "%y", s);
181 snprintf(s, 128, "%02i", (int)c.days);
182 UArray_replaceCString_withCString_(ba, "%d", s);
184 snprintf(s, 128, "%02i", (int)c.hours);
185 UArray_replaceCString_withCString_(ba, "%H", s);
187 snprintf(s, 128, "%02i", (int)c.minutes);
188 UArray_replaceCString_withCString_(ba, "%M", s);
190 snprintf(s, 128, "%02f", c.seconds);
191 UArray_replaceCString_withCString_(ba, "%S", s);
193 return ba;
196 void Duration_print(const Duration *self)
198 UArray *ba = Duration_asUArrayWithFormat_(self, NULL);
199 UArray_print(ba);
200 UArray_free(ba);
203 // math --------------------------------------------------------
205 void Duration_add_(Duration *self, const Duration *other)
207 self->seconds += other->seconds;
210 void Duration_subtract_(Duration *self, const Duration *other)
212 self->seconds -= other->seconds;