-
Adding to a string date
Given the date as a string, how can i achieve this date as a struct tm*?
I need a datestring as input, then add a variable number of days to this date, and then display the date as a string again.
I tried the following:
Code:
struct tm *ts;
time_t binDate(0);
int iDays = 300;
time(&binDate);
ts=localtime(&binDate);
ts->tm_year = iYearFromStr;
ts->tm_mon = iMonthFromStr;
ts->tm_mday = iDayFromStr;
binDate = mktime(ts);//Convert to binary time
binDate += 86400*iDays;// Add the number of days
ts = localtime(&binDate ); // Regn tilbake til struct
-
You're on the right track.
Just use binDate for incrementing the time in terms of seconds. Then convert the result into a struct tm if need be.
Code:
#include <time.h>
#include <stdio.h>
#define NUM_SECONDS_IN_DAY (60 * 60 * 24)
int main()
{
// number of seconds elapsed since midnight (00:00:00), January 1, 1970, UTC
time_t tval;
time(&tval);
printf("Current Time: %s\n", ctime(&tval));
// add 5 days to tval
tval += NUM_SECONDS_IN_DAY * 5;
printf("5 Days Later: %s\n", ctime(&tval));
return 0;
}//main
gg
-
Thanks for the reply. I just figured it out myself as well:
Code:
struct tm *ts;
time_t binDate(0);
int iDays = 300;
time(&binDate);
ts=localtime(&binDate);
ts->tm_year = iYearFromStr+100;//!!!!After 2000 you have to add 100 to year
ts->tm_mon = iMonthFromStr;
ts->tm_mday = iDayFromStr;
binDate = mktime(ts);//Convert to binary time
binDate += 86400*iDays;// Add the number of days
ts = localtime(&binDate ); // Back to structformat
ts->tm_year-=100;
printf("DDMMYY:%02i%02i%02i",ts->tm_mday,ts->tm_mon,ts->tm_year );
-
You're both assuming that time_t stores seconds
Use mktime() to normalise a time value, like so
Code:
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm ts;
time_t now, later;
int iDays = 300;
time(&now);
printf( "Now = %s", asctime(localtime(&now)));
ts = *localtime(&now);
ts.tm_mday += iDays; // some time from now
later = mktime( &ts ); // Convert to binary time
printf( "Later = %s", asctime(localtime(&later)));
return 0;
}
-
I implemented a function for this:
Code:
/*************************************************\
* dateAdd() *
* Adds a given number of days to a string date *
* Input: *
* ts ->Timestruct, not allocated, hence** *
* achDate ->Datebuffer in format CCYY.MM.DD *
* iAdd ->Number of days to add *
* Output: *
* void *
\*************************************************/
void dateAdd(struct tm **ts, char *achDate, int iAdd){
time_t tBuf(0);
char achBuf[3];
// Init vars
achBuf[2]=0;
time(&tBuf);
*ts=localtime(&tBuf);
// Fetch date from string
memcpy(achBuf, achDate+2, 2); (*ts)->tm_year = atoi(achBuf);
memcpy(achBuf, achDate+5, 2); (*ts)->tm_mon = atoi(achBuf);
memcpy(achBuf, achDate+8, 2); (*ts)->tm_mday = atoi(achBuf);
// Calculate new date
(*ts)->tm_year += 100;
tBuf = mktime(*ts); // Convert to binary time
tBuf += 86400*iAdd; // Add number of days
*ts = localtime(&tBuf); // Update time struct
(*ts)->tm_year -= 100; // I only want a two digit year
}
-
>> You're both assuming that time_t stores seconds
time() returns a value in seconds - no need to assume.
gg
-
So the case is closed. Thank's for the assistance.
-
>> You're both assuming that time_t stores seconds
>time() returns a value in seconds - no need to assume.
http://groups.google.com/groups?hl=e...ws.net&rnum=18
-
I think that link is somewhat mis-leading Dave. I don't think a time_t with those properties would be allowed.
From reading n869 (and from K&R-II), time_t is an arithmetic type which at least suggests that you should be able to do
if ( t1 < t2 )
That is, you could call time() at different points in the program and be able to estabish a 'before' relationship without the need to check the sign of a difftime() call.
What the ANSI-C standard does not say about time_t are
- what the underlying data type is (int, long, double)
- the epoch time (typically 1/1/1970)
- the resolution (typically seconds)
-
It's strange that the IEEE Std 1003.1, 2003 Edition (my link above) says that the resolution is in seconds from the Epoch.
One of the differences between the ISO and ANSI standard perhaps?
However, to use the strategy I posted earlier, you would have to account for leap year seconds - so Salem's and knutso's method is much easier anyways.
I've never used these functions so this is what I did to prove it to myself.
Code:
#include <time.h>
#include <stdio.h>
time_t seconds_between_years(int y1, int y2)
{
struct tm tm1={0}, tm2={0};
tm1.tm_year = y1 - 1900;
tm2.tm_year = y2 - 1900;
time_t t1 = mktime(&tm1);
time_t t2 = mktime(&tm2);
return (t1 > t2) ? (t1 - t2) : (t2 - t1);
}//seconds_between_years
int main()
{
int year;
for (year = 1990; year < 2004; year++)
{
printf("%d - %d = %d seconds\n",
year + 1, year,
seconds_between_years(year, year + 1));
}//for
return 0;
}//main
gg
-
>I don't think a time_t with those properties would be allowed.
>you should be able to do
>if ( t1 < t2 )
I didn't go through all of this, but early on it at least suggested that this might not necessarily be so. (Then again, parsing comp.std.c for useful information is definitely not my strong suit.)
I'm not trying to argue in favor of whatever bizarre implementation might exist. I just threw the previous link out there as some example of an unusual scenario that could possibly(?) exist... I thought I was continuing your argument to use the time functions.
-
>From reading n869 (and from K&R-II), time_t is an arithmetic type which at least suggests that you should be able to do
>if ( t1 < t2 )
Iffy. time_t must be an arithmetic type because sometimes values of type time_t have to be compared with -1. However, the standard says nothing about the arithmetic properties of time_t aside from that, so while t1 < t2 will probably work, the result isn't guaranteed to be portable. That's the whole reason difftime was introduced by the standards committee, to allow programmers to find the difference of two times while still allowing the implementation to choose the best format possible for representing time_t.