1 //metadoc Duration copyright Steve Dekorte 2002
2 //metadoc Duration license BSD revised
21 Duration
*Duration_new(void)
23 Duration
*self
= (Duration
*)io_calloc(1, sizeof(Duration
));
27 Duration
*Duration_newWithSeconds_(double s
)
29 Duration
*self
= Duration_new();
34 void Duration_copy_(Duration
*self
, const Duration
*other
)
36 memcpy(self
, other
, sizeof(Duration
));
39 void Duration_free(Duration
*self
)
44 int Duration_compare(const Duration
*self
, const Duration
*other
)
46 if (self
->seconds
== other
->seconds
)
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
)
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
);
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
;
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
);
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
);
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
);
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
);
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
);
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
)
165 // strings --------------------------------------------------------
167 UArray
*Duration_asUArrayWithFormat_(const Duration
*self
, const char *format
)
169 DurationComponents c
= Duration_asComponents(self
);
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
);
194 void Duration_print(const Duration
*self
)
196 UArray
*ba
= Duration_asUArrayWithFormat_(self
, NULL
);
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
;