Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting dates

Posted on 2003-10-22
3
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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.

688 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