Solved

timelocal

Posted on 2002-04-08
4
1,739 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

733 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