?
Solved

timelocal

Posted on 2002-04-08
4
Medium Priority
?
1,750 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
[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
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 200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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

764 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