# Date/time comparisons and absolute/relative date/times on Linux in C++?

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm looking for pointers on how to perform date/time comparisons in Linux, including absolute/relative dates/times. I'm trying to write a class that contains both tm and timeval structs, but I just for the life of me work out how to do something like the following. I want to define a date/time window, using start_time and end_time. end_time should be the current date/time and start_time should be end_time - 1 minute. That's a relatively simple example of what I'm trying to do among other things. Suggestions and discussion are appreciated. Thanks [Edited by - stodge on July 5, 2006 7:13:38 AM]

##### Share on other sites
You can read the current time as a struct timeval with gettimeofday() and then modify the structure to your liking... Something like that:

struct timeval start_time, end_time;/* read current time */gettimeofday( &end_time, NULL );/* copy end_time start_time */memcpy( &start_time, &end_time, sizeof(struct timeval) );/* set back start time by one minute */start_time.tv_sec -= 60;

Of course, that has potential wrapping issues in the timeval structure, but unless you're dealing with the "distant past" (1st of January 1970) or "distant future" (2038 for 32bit values), you should be fine with a little business logic.

##### Share on other sites
Thanks. One thing I thought I could do was use the tm struct as a relative time. For example, zero a tm struct and set tm_mday to 1. Then add it to another tm struct, normalise it, and hey presto, the new date is one day in the future. This doesn't appear to work as I expected.

##### Share on other sites
I think I have most of it working. I can add one or more days, months, years etc by modifying a tm struct.

I wanted to do something like this in pseudo-code:

TimeType time1 = get_current_time
TimeType delta = get_zero_time + 1 year

TimeType time2 = time1 + delta

time2's year is 2007.

But it doesn't work. delta becomes -1 second, which means time2 = time1 - 1 second.

##### Share on other sites
I can now do:

TimeType time1, time2, time3;time1 = Reactor::GetSingleton().GetCurrentTime();time2 = time1;time2.NextYear();time3 = time2 + time1;time3.Dump();

The only one left is time2 += time1.

Progress!