Solved

timelocal

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

860 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