Math with dates

Hi,

i'm trying to make a count down in secondes between now and a date in the future.

now i have been able to do that using

#include <time.h>

time_t tijd1;
time_t tijd2;
struct tm * time1;
struct tm * time2;

time ( &tijd2 );
time2 = localtime ( &tijd2 );

time1 = time2;
time1->tm_year = 2019 - 1900;
time1->tm_mon = 8;
time1->tm_mday = 5;
time1->tm_sec = 0;
time1->tm_min = 0;
time1->tm_hour = 0;
time1->tm_isdst = 1;

tijd1 = mktime ( time1 );

and then doing tijd1 - tijd2;

which works great till i use for time1->tm_year the year 2039 (yes i need to go this far)
which result in tijd1 to return -1

I understand that this is because the assigned variable for time_t in time.h is probebly only a long int and that the value in 2039 goes beyond its range.

becides rewritting time.h (which i dont know how to do) has anyone any suggestions on how i could solve this.

Thanks



You could use the boost.date_time library http://www.boost.org/doc/libs/release/doc/html/date_time.html -- it is very portable and robust

1
2
3
4
5
6
7
8
9
10
11
12
#include <boost/date_time.hpp>
#include <iostream>
namespace pt = boost::posix_time;
int main()
{
    pt::ptime now = pt::microsec_clock::local_time();
    pt::ptime then = pt::time_from_string("2052-8-5 12:34:56.789");
    std::cout << "The difference between " << now << '\n'
              << "and " << then << '\n'
              << "is " << then - now << '\n'
              << "or " << (then-now).total_seconds()  << " seconds\n";
}

The difference between 2012-Jan-10 22:34:54.258115
and 2052-Aug-05 12:34:56.789000
is 355622:00:02.530885
or 1280239202 seconds
Last edited on
thanks i will try this

see if i can do what i want with it.
Topic archived. No new replies allowed.