Solved

Converting dates

Posted on 2003-10-22
3
336 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 128
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 88
c++, dynamic object by json 1 65
Need some help with design 17 48
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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