2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
23 Duration
*Duration_new(void)
25 Duration
*self
= (Duration
*)io_calloc(1, sizeof(Duration
));
29 Duration
*Duration_newWithSeconds_(double s
)
31 Duration
*self
= Duration_new();
36 void Duration_copy_(Duration
*self
, const Duration
*other
)
38 memcpy(self
, other
, sizeof(Duration
));
41 void Duration_free(Duration
*self
)
46 int Duration_compare(const Duration
*self
, const Duration
*other
)
48 if (self
->seconds
== other
->seconds
)
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
)
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
);
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
;
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
);
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
);
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
);
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
);
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
);
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
)
167 // strings --------------------------------------------------------
169 UArray
*Duration_asUArrayWithFormat_(const Duration
*self
, const char *format
)
171 DurationComponents c
= Duration_asComponents(self
);
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
);
196 void Duration_print(const Duration
*self
)
198 UArray
*ba
= Duration_asUArrayWithFormat_(self
, NULL
);
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
;