Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting dates

Posted on 2003-10-22
3
Medium Priority
?
350 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 150 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

886 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