Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 libraries for date math?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HyperFlexed




PostPosted: Sun Aug 08, 2010 3:37 pm   Post subject: libraries for date math?

I've created a typedef struct as follows:
code:
typedef struct datetime {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
} datetime_t;


For me this is a really convenient way of representing a date and time. Any libraries I've found (i.e. time.h) seem to give back the time in an integer representing time since the epoch (Jan 1, 1970 00:00:00). This means if I wanted to pass 2 datetime_t into a function, and have it return another datetime_t representing the time between them, I have to do all kinds of complicated math (leap years, leap seconds, differing number of days in a month, etc.).

I'd be hard pressed to believe that there are no C libraries for this purpose. Anyone with some experience know where I can look? I have done a basic google, but I'm unsure of what to look for specifically.

Thanks in advance Smile
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Aug 08, 2010 4:38 pm   Post subject: RE:libraries for date math?

All the libraries work with integer representation of time for the exact reason of avoiding complicated math. It's the standard.

Your representation, should one consider timezones, daylight-savings, etc. is very hard to work with, and not even unique (that is, two distinct points in time could have the same representation).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
TheGuardian001




PostPosted: Sun Aug 08, 2010 4:44 pm   Post subject: Re: libraries for date math?

The localtime function, in time.h appears to be what you're looking for. It takes a Unix epoch time, and converts it into a local time (It uses the tm struct, which is similar to yours)

The year is, for some reason, returned as the number of years since 1900 (Way to think ahead, whoever designed that one,) So you'll still have to manually make that one useful (I don't consider getting "110" as a year to be too helpful.)

Do all of your calculations on the time using the standard time since Unix epoch, then convert the final struct into a usable time using localtime().
HyperFlexed




PostPosted: Sun Aug 08, 2010 4:57 pm   Post subject: Re: libraries for date math?

I just began to realize many of the difficulties as I started writing a small bit of code to do this math. I'll have a look at localtime.

The thing that gets me is if I were to set my start time in the middle of a month, and the end time in the middle of another, how would I say whether or not the extra days at the beginning and end add up to an extra month when all the months have a different number of days...

I think for now I'm just going to calculate the difference between the two tm structs and parse out the text that asctime feeds back to populate my datetime_t object.

seriously, we need to start using stardates or something Razz
Tony




PostPosted: Sun Aug 08, 2010 5:22 pm   Post subject: RE:libraries for date math?

We do. It's called UTC.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: