[H-GEN] time in C/C++

James McPherson James.McPherson at Aus.Sun.COM
Sat Dec 25 23:56:00 EST 1999


[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics.  Please observe the list's charter.          ]

>Frank Brand writes:
>
>I am writing a small financial programme (using graphapp as a Linux replacement
>for VB...its not that GUI user friendly but it is quite neat just the same).
>The programme is an implementation of the RBA formula for pricing a bond.
>The most difficult part of the process is calculating the days between two
>dates so its not exactly a major taxing of the intellect.
>My query is to asak what people think about the best way of getting the time
>between dates. Originally, I thought to use difftime() but I believe that this
>method can lead to errors under some circumstances.
>The whole process is simple , even if the formula is not (it is a fairly
>complex formula but once the individual parameters are known it is just a case
>of substituting values).
>For safety's sake I thought I would just write my own function as the price of
>the bond is critically dependent on getting the date differences right.
>Anyone want to offer an opinion.

sure says muggins!

if you use difftime on the output from time(2) and mktime(2) then you _should_ 
be ok - you'll get a result in seconds which you could then divide by 86400 to 
get a decimal number of days which can then be tidied up using a few more 
comparison operations.

if you have a starting point struct tm where you assume midnight as the 
reference time, and an ending point struct tm where you also assume midnight is 
the reference time, then it should be a simple operation.


eg (don't try compiling this ;>) :


try starting at 1 January 2000 and ending on 30 June 2000:

struct tm startingpoint = {
	0, /* seconds */
	0, /* minutes */
	0, /* hours */
	1, /* day of month */
	0, /* month = january == 0 on 0-11 scale */
	100, /* years since 1900!! */
	,  /* day of week */
	1, /* day of year */
	0}; /* 1 if DST for your TZ */
	
struct tm endingpoint = {
	0,
	0,
	0,
	30,
	5,
	100,
	,
	,
	0};
	
time_t start_tm = mktime(startingpoint);
time_t end_tm = mktime(endingpoint);
time_t difference = difftime(endingpoint,startingpoint);

double daysdifference = difference / 86400.0;



I just love showing off my ignorance on a sunday arvo ;)

still, is that what you were thinking of?

cheers,
James C. McPherson
--
Customer Technical Support Engineer      828 Pacific Highway 
Sun Microsystems Australia Pty Ltd       Gordon NSW 2072
Support Helpline: 1-800-555-786


--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list