Solved

timelocal

Posted on 2002-04-08
4
1,725 Views
Last Modified: 2007-12-19
I have a Date in "yyyymmdd" and I need to add or remove a amount of time.

I'm a perl programmer and in perl I can use timelocal method to convert a date array (+- struct tm) in time_t structure make the calculation and after that I use localtime to reconvert the date to the format I need (+- struct tm).

Is there a way of doing this in C/C++?
Did someone did that?

There is a better way of doing it?
0
Comment
Question by:oleber
  • 3
4 Comments
 
LVL 1

Expert Comment

by:jmccay
ID: 6925382
All the functions you need should be in the "time.h" header file.  time() will return a time_t of the current date and time, and ctime will convert it back.  If you need more I suggest looking these functions up.
   If you are using Visual Studio, look up CTime and CTimeSpan.  Hope this helps.
0
 
LVL 10

Author Comment

by:oleber
ID: 6925439
The code I'm trying to do is something like

  string str("20010405"); /* Come from a DB Query */
  struct tm myTime;
  myTime.tm_sec = 0;
  myTime.tm_min = 0;
  myTime.tm_hour = 0;
  myTime.tm_mday = atoi(str.substr(6,2).c_str());
  myTime.tm_mon = atoi(str.substr(4,2).c_str());
  myTime.tm_year = atoi(str.substr(0,4).c_str());
  time_t t = timelocal(myTime);
  t += 60*60*24*nDays;
  struct tm* pTime = localtime(t);
  char sResult[9];
  sprintf(sResult,"%.04d%.02d%.02d", pTime->tm_year, pTime->tm_mon, pTime->tm_mday);
  printf("Result date: %s",sResult);

So I need the timelocal method. My work have to work in Windows and Unix (Sun Machine) so I'm working just with ANSI C/C++
0
 
LVL 1

Accepted Solution

by:
jmccay earned 50 total points
ID: 6925636
I should note that the time functions only use a long to hong the time and the base time on windows is Jan 1, 1970 00:00:00 and the maximum time it can represent is Jan 18, 2038 19:14:07.

Here is what that should be:

int nDays = 2;
string str("20010405"); /* Come from a DB Query */
struct tm myTime;
myTime.tm_sec = 0;
myTime.tm_min = 0;
myTime.tm_hour = 0;
myTime.tm_mday = atoi(str.substr(6,2).c_str());
myTime.tm_mon = atoi(str.substr(4,2).c_str());
// The year is the number of years since 1900
int nYear = atoi(str.substr(0,4).c_str());
myTime.tm_year = (nYear > 1900)? nYear - 1900:nYear;
// -1 lets the library compute Day Light Savings time stuff
myTime.tm_isdst = -1;
// mktime will convert the tm structure into a the time_t
time_t s = mktime(&myTime);
// add the days
s += 60*60*24*nDays;
get the local time
struct tm* pTime = localtime( &s );
char sResult[9];
// get the results.  You could also use strftime
sprintf(sResult,"%.04d%.02d%.02d", pTime->tm_year+1900, pTime->tm_mon, pTime->tm_mday);
printf("Result date: %s",sResult);

   I hope that helps.  You made need to verify what the ranges are on Sun systems, but I would bet it is the same since storing time as a number this way is pretty standard.
0
 
LVL 1

Expert Comment

by:jmccay
ID: 6925646
I had a mistake.  The follow:

" I should note that the time functions only use a long to hong the time and the base time on windows
is Jan 1, 1970 00:00:00 and the maximum time it can represent is Jan 18, 2038 19:14:07."

should be:
" I should note that the time functions only use a long to hold the time. The base time on windows
is Jan 1, 1970 00:00:00 (24 hour) and the maximum time it can represent is Jan 18, 2038 19:14:07."
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

743 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