Solved

Converting dates

Posted on 2003-10-22
3
327 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
Comment Utility
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_
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 …
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.

772 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

12 Experts available now in Live!

Get 1:1 Help Now