[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Date processing
From: |
Benedikt Stefansson |
Subject: |
Re: Date processing |
Date: |
Mon, 15 Nov 1999 17:02:14 -0700 |
Hi Steve,
In my experience the C time library is really messy to work with. For
example I can't for the life of me understand why this program crashes
with a seg fault:
#include <time.h>
int
main(int argc,const char ** argv)
{
struct tm *tm1;
struct tm *tm2;
double diff;
char * c;
c = (char *)strptime("1/1/1900","%m/%d/%Y",tm1);
c = (char *)strptime("11/15/1999","%m/%d/%Y",tm2);
diff = difftime(mktime(tm1),mktime(tm2));
printf("Difference is %f\n",diff);
}
Besides, even if it did work the difftime function returns the number in
seconds between the two dates. One could of course get back the days as
diff/60/60/24 but I would be loath to trust that.
By a weird coincidence I spent last night trying to figure out how to do
complicated date manipulations in a Swarm program. My main concern was to
be able to work with date strings directly (11/15/1999 or 11-15-1999),
i.e. without having to manipulate the tm structs.
While this is not an Objective-C solution the quick and dirty way I came
up with was to 'borrow' a library in pure C which is used in a great Perl
module called Date::Calc. It is thoroughly documented in the man page for
that module (available from CPAN or any of it's mirrors).
The library (by Steven Beyer) is released under the GNU LGPL. I've
butchered a file from the source code for DateCalc.c and associated
header file which I called timefunctions.c (attached in tarball). I've
also implemented a new function which returns the number of days between
two date strings:
long int
DateCalc_Delta_Days_Str(const char * date1,const char * date2));
The timetest.c example program in the tarball shows how to do the
calculation. You could do your date calculations in main.m or call the C
functions from any object.
To run the example and calculate the days elapsed since January 1st 1900
and today's date save the timetest.c file and the timefunctions.c:
$ gcc -o timetest timetest.c
$ timetest 11/15/1999
For anyone using this I recommend downloading the Date::Calc Perl module,
even if you don't use Perl the man pages are quite useful. Also bear in
mind the LGPL.
Benedikt
"M. Lang / S. Railsback" wrote:
> Has anybody got an object that converts a date (Year, Julian day; or
> Year-month-date) to a standard integer format (days since 1/1/1900;
> etc.) as in Excel?
>
> And vice-versa, of course?
>
> Thanks
>
> Steve
> --
> address@hidden
> Lang, Railsback & Assoc.
> 250 California Ave., Arcata CA 95521
> 707-822-0453; Fax 822-1868
>
> ==================================
> Swarm-Support is for discussion of the technical details of the day
> to day usage of Swarm. For list administration needs (esp.
> [un]subscribing), please send a message to <address@hidden>
> with "help" in the body of the message.
timetest.tar.gz
Description: GNU Zip compressed data