Solved

timelocal

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

17 Experts available now in Live!

Get 1:1 Help Now