Solved

Converting dates

Posted on 2003-10-22
3
328 Views
Last Modified: 2010-04-01
Hi,
I'm new to C++ and I want to do a little console exercise that seems simple:
I want to introduce a date in string format(year month day hour minute second -2002 03 25 11 34 57). After that I want to convert it to a date. Then I want to add a number of seconds to this date and show on screen the new date.
I tried this using the tm structure  to store the input date, then I converted it to long using mktime() then added the number of seconds and converted the new date to date using localtime(). The problem is that it is only working for years between 1970 and 1999.
Can anyone tell me how to make it work for a larger period of time? An example is apreciated.
Thanks in advance!
0
Comment
Question by:AltEu
3 Comments
 
LVL 7

Accepted Solution

by:
burcarpat earned 50 total points
ID: 9604999
check out boost date-time at,

    http://www.boost.org/libs/date_time/doc/index.html

the lib is cross-platform and works with many platforms and compilers w/o any problems.  it has a bunch of classes that can deal with your problem

[ about boost.org :: boost.org is an organization supported by many c++ standards committee members and provides 100% free, peer-reviewed, cross-platform libraries.  many of the boost libraries, such as their smart pointer library, are already in the drafts of the next revision of the c++ standard ]

-- ba
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9605299
Prefer the _mktime64 & _localtime64 combination over their smaller mktime and localtime equivalents.

struct tm oldDate;
// assuming oldDate is already set, and upgraded with the required number of seconds

_tzset( ); // ensure localtime is _really_ local time !!

__time64_t t64Date = _mktime64 ( &oldDate );

struct tm newDate = *( _localtime64 ( &t64Date ) );

This will allow dates up until the end of December 3000. The start date is still 1970 though.
0
 

Author Comment

by:AltEu
ID: 9636390
Hi,
Sorry I didn't commented earlier, I was kind of busy. The lib is quite good.
Thanx for the tip, burcapat!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now