3 docCopyright("Steve Dekorte", 2002)
4 docLicense("BSD revised")
5 docDescription("A container for a duration of time.")
9 #include "IoDuration.h"
11 #include "IoCFunction.h"
16 #define DATA(self) ((Duration *)IoObject_dataPointer(self))
18 // extend message object
20 IoDuration
*IoMessage_locals_durationArgAt_(IoMessage
*self
, void *locals
, int n
)
22 IoObject
*v
= IoMessage_locals_valueArgAt_(self
, (IoObject
*)locals
, n
);
23 if (!ISDURATION(v
)) IoMessage_locals_numberArgAt_errorForType_(self
, (IoObject
*)locals
, n
, "Duration");
27 // ---------------------------------------------
31 IoTag
*IoDuration_newTag(void *state
)
33 IoTag
*tag
= IoTag_newWithName_("Duration");
34 IoTag_state_(tag
, state
);
35 IoTag_cloneFunc_(tag
, (IoTagCloneFunc
*)IoDuration_rawClone
);
36 IoTag_freeFunc_(tag
, (IoTagFreeFunc
*)IoDuration_free
);
37 IoTag_compareFunc_(tag
, (IoTagCompareFunc
*)IoDuration_compare
);
38 IoTag_writeToStreamFunc_(tag
, (IoTagWriteToStreamFunc
*)IoDuration_writeToStream_
);
39 IoTag_readFromStreamFunc_(tag
, (IoTagReadFromStreamFunc
*)IoDuration_readFromStream_
);
43 void IoDuration_writeToStream_(IoDuration
*self
, BStream
*stream
)
45 BStream_writeTaggedDouble_(stream
, Duration_asSeconds(DATA(self
)));
48 void IoDuration_readFromStream_(IoDuration
*self
, BStream
*stream
)
50 Duration_fromSeconds_(DATA(self
), BStream_readTaggedDouble(stream
));
53 IoDuration
*IoDuration_proto(void *state
)
55 IoMethodTable methodTable
[] = {
56 {"years", IoDuration_years
},
57 {"setYears", IoDuration_setYears
},
58 {"days", IoDuration_days
},
59 {"setDays", IoDuration_setDays
},
60 {"hours", IoDuration_hours
},
61 {"setHours", IoDuration_setHours
},
62 {"minutes", IoDuration_minutes
},
63 {"setMinutes", IoDuration_setMinutes
},
64 {"seconds", IoDuration_seconds
},
65 {"setSeconds", IoDuration_setSeconds
},
66 {"totalSeconds", IoDuration_asNumber
},
68 {"asString", IoDuration_asString
},
69 {"asNumber", IoDuration_asNumber
},
71 {"fromNumber", IoDuration_fromNumber
},
72 /*Tag_addMethod(tag, "fromString", IoDuration_fromString),*/
74 {"print", IoDuration_printDuration
},
75 {"+=", IoDuration_add
},
76 {"-=", IoDuration_subtract
},
81 IoObject
*self
= IoObject_new(state
);
83 IoObject_setDataPointer_(self
, Duration_new());
84 IoObject_tag_(self
, IoDuration_newTag(state
));
85 IoState_registerProtoWithFunc_((IoState
*)state
, self
, IoDuration_proto
);
87 IoObject_addMethodTable_(self
, methodTable
);
91 IoDuration
*IoDuration_rawClone(IoDuration
*proto
)
93 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
94 IoObject_setDataPointer_(self
, Duration_new());
95 Duration_copy_(DATA(self
), DATA(proto
));
99 IoDuration
*IoDuration_new(void *state
)
101 IoDuration
*proto
= IoState_protoWithInitFunction_((IoState
*)state
, IoDuration_proto
);
102 return IOCLONE(proto
);
105 IoDuration
*IoDuration_newWithSeconds_(void *state
, double s
)
107 IoDuration
*self
= IoDuration_new(state
);
108 IoDuration_fromSeconds_(self
, s
);
112 int IoDuration_compare(IoDuration
*self
, IoDuration
*other
)
114 if (ISDURATION(other
))
116 return Duration_compare(DATA(self
), DATA(other
));
119 return IoObject_defaultCompare(self
, other
);
122 void IoDuration_free(IoDuration
*self
)
124 Duration_free(DATA(self
));
127 Duration
*IoDuration_duration(IoDuration
*self
)
132 IoDuration
*IoDuration_fromSeconds_(IoDuration
*self
, double s
)
134 Duration_fromSeconds_(DATA(self
), s
);
138 double IoDuration_asSeconds(IoDuration
*self
)
140 return Duration_asSeconds(DATA(self
));
143 // years --------------------------------------------------------
145 IoObject
*IoDuration_years(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
148 docSlot("years", "Returns a number containing the year of the receiver. ")
151 return IONUMBER(Duration_years(DATA(self
)));
154 IoObject
*IoDuration_setYears(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
157 docSlot("setYears(aNumber)", "Sets the year of the receiver. Returns self.")
160 Duration_setYears_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
164 // days --------------------------------------------------------
166 IoObject
*IoDuration_days(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
170 "Returns a number containing the day of the month of the receiver. ")
173 return IONUMBER(Duration_days(DATA(self
)));
176 IoObject
*IoDuration_setDays(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
179 docSlot("setDays(aNumber)", "Sets the day of the receiver. Returns self.")
182 Duration_setDays_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
186 // hours --------------------------------------------------------
188 IoObject
*IoDuration_hours(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
192 "Returns a number containing the hour of the day(0-23) of the receiver. ")
195 return IONUMBER(Duration_hours(DATA(self
)));
198 IoObject
*IoDuration_setHours(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
201 docSlot("setHours(aNumber)", "Sets the hour of the receiver. Returns self.")
204 Duration_setHours_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
208 // minutes --------------------------------------------------------
210 IoObject
*IoDuration_minutes(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
214 "Returns a number containing the minute of the hour(0-59) of the receiver. ")
217 return IONUMBER(Duration_minutes(DATA(self
)));
220 IoObject
*IoDuration_setMinutes(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
223 docSlot("setMinutes(aNumber)",
224 "Sets the minute of the receiver. Returns self.")
227 Duration_setMinutes_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
231 // seconds --------------------------------------------------------
233 IoObject
*IoDuration_seconds(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
237 "Returns a number containing the seconds of the minute(0-59) of the receiver.
238 This number may contain fractions of seconds. ")
241 return IONUMBER(Duration_seconds(DATA(self
)));
244 IoObject
*IoDuration_setSeconds(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
247 docSlot("setSeconds(aNumber)",
248 "Sets the second of the receiver. Return self.")
251 Duration_setSeconds_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
255 // conversion --------------------------------------------------------
257 IoObject
*IoDuration_asString(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
260 docSlot("asString(formatString)",
261 """Returns a string representation of the receiver. The formatString argument is optional. If present, the returned string will be formatted according to ANSI C date formating rules.
264 %y years without century as two-digit decimal number (00-99)
265 %Y year with century as four-digit decimal number
268 %H hour as two-digit 24-hour clock decimal integer (00-23)
269 %M minute as a two-digit decimal integer (00-59)
270 %S second as a two-digit decimal integer (00-59)
272 The default format is "%Y %d %H:%M:%S".
278 if (IoMessage_argCount(m
) == 1)
280 format
= CSTRING(IoMessage_locals_symbolArgAt_(m
, locals
, 0));
283 ba
= Duration_asUArrayWithFormat_(DATA(self
), format
);
284 return IoState_symbolWithUArray_copy_(IOSTATE
, ba
, 0);
287 IoObject
*IoDuration_printDuration(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
290 docSlot("print", "Prints the receiver. Returns self.")
293 Duration_print(DATA(self
));
298 docSlot("totalSeconds",
299 "Same as a asNumber.")
302 IoObject
*IoDuration_asNumber(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
306 "Returns a number representation of the receiver.
307 (where 1 is equal to one second) ")
310 return IONUMBER(Duration_asSeconds(DATA(self
)));
313 IoObject
*IoDuration_fromNumber(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
316 docSlot("fromNumber(aNumber)",
317 "Sets the receiver to the Duration specified by
318 aNumber(same format number as returned by Duration asNumber). Returns self. ")
321 Duration_fromSeconds_(DATA(self
), IoMessage_locals_doubleArgAt_(m
, locals
, 0));
325 // math --------------------------------------------------------
327 IoObject
*IoDuration_add(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
330 docSlot("+=(aDuration)", "Add aDuration to the receiver. Returns self. ")
333 IoDuration
*d
= IoMessage_locals_durationArgAt_(m
, locals
, 0);
334 Duration_add_(DATA(self
), DATA(d
));
338 IoObject
*IoDuration_subtract(IoDuration
*self
, IoObject
*locals
, IoMessage
*m
)
341 docSlot("-=(aDuration)", "Subtract aDuration to the receiver. Returns self. ")
344 IoDuration
*d
= IoMessage_locals_durationArgAt_(m
, locals
, 0);
345 Duration_subtract_(DATA(self
), DATA(d
));