work with dates

F

Fernando

Hello,
I want do a program what find the date of the one before day at day what the
program run (if today is 20031030, the program will find 20031029). I have
the next:

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); //return system time
ptr = localtime(&ltime); // return time in the form of tm structure
strftime(str,80,"%Y%m%d",ptr);
printf("%s\n",str);
}

but I don´t know how substract one day at this array. Can someone help me?

Thaks
 
A

Artie Gold

Fernando said:
Hello,
I want do a program what find the date of the one before day at day what the
program run (if today is 20031030, the program will find 20031029). I have
the next:

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); //return system time

Since time() returns a value that represents the time, in seconds,
since some event (typically the `epoch', i.e. 1/1/1970), just
subtract a day's worth of seconds before converting.
ptr = localtime(&ltime); // return time in the form of tm structure
strftime(str,80,"%Y%m%d",ptr);
printf("%s\n",str);
}

but I don´t know how substract one day at this array. Can someone help me?
HTH,
--ag
 
R

rihad

Fernando said:
Hello,
I want do a program what find the date of the one before day at day what the
program run (if today is 20031030, the program will find 20031029). I have
the next:

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); //return system time

Since time() returns a value that represents the time, in seconds,
since some event (typically the `epoch', i.e. 1/1/1970), just
subtract a day's worth of seconds before converting.

time_t doesn't have to be in seconds!

7.23.2.4 The time function

Synopsis

[#1]

#include <time.h>
time_t time(time_t *timer);

Description

[#2] The time function determines the current calendar time.
The encoding of the value is unspecified.


But here's what the C-FAQ has to say on this problem:

13.14: How can I add N days to a date? How can I find the difference
between two dates?

A: The ANSI/ISO Standard C mktime() and difftime() functions
provide some support for both problems. mktime() accepts non-
normalized dates, so it is straightforward to take a filled-in
struct tm, add or subtract from the tm_mday field, and call
mktime() to normalize the year, month, and day fields (and
incidentally convert to a time_t value). difftime() computes
the difference, in seconds, between two time_t values; mktime()
can be used to compute time_t values for two dates to be
subtracted.

Another approach to both problems is to use "Julian day
numbers". Code for handling Julian day numbers can be found
in the Snippets collection (see question 18.15c), the
Simtel/Oakland archives (file JULCAL10.ZIP, see question 18.16),
and the "Date conversions" article mentioned in the References.

http://www.eskimo.com/~scs/C-faq/top.html
 
A

Artie Gold

rihad said:
Fernando said:
Hello,
I want do a program what find the date of the one before day at day what the
program run (if today is 20031030, the program will find 20031029). I have
the next:

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); //return system time

Since time() returns a value that represents the time, in seconds,
since some event (typically the `epoch', i.e. 1/1/1970), just
subtract a day's worth of seconds before converting.


time_t doesn't have to be in seconds!

7.23.2.4 The time function

Synopsis

[#1]

#include <time.h>
time_t time(time_t *timer);

Description

[#2] The time function determines the current calendar time.
The encoding of the value is unspecified.


But here's what the C-FAQ has to say on this problem:

13.14: How can I add N days to a date? How can I find the difference
between two dates?

A: The ANSI/ISO Standard C mktime() and difftime() functions
provide some support for both problems. mktime() accepts non-
normalized dates, so it is straightforward to take a filled-in
struct tm, add or subtract from the tm_mday field, and call
mktime() to normalize the year, month, and day fields (and
incidentally convert to a time_t value). difftime() computes
the difference, in seconds, between two time_t values; mktime()
can be used to compute time_t values for two dates to be
subtracted.

Another approach to both problems is to use "Julian day
numbers". Code for handling Julian day numbers can be found
in the Snippets collection (see question 18.15c), the
Simtel/Oakland archives (file JULCAL10.ZIP, see question 18.16),
and the "Date conversions" article mentioned in the References.

http://www.eskimo.com/~scs/C-faq/top.html

Mea culpa!

Thanks,
--ag
 
A

Al Bowers

Fernando said:
Hello,
I want do a program what find the date of the one before day at day what the
program run (if today is 20031030, the program will find 20031029). I have
the next:

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); //return system time
ptr = localtime(&ltime); // return time in the form of tm structure
strftime(str,80,"%Y%m%d",ptr);
printf("%s\n",str);
}

but I don´t know how substract one day at this array. Can someone help me?
You can substract in the broken down time.
For example ptr->tm_hour -= 24; /* substract 24 hours */
then call function mktime to get the new time_t value.

#include <stdio.h>
#include <time.h>

int main() {
struct tm *ptr;
time_t ltime;
char str[80];

ltime = time(NULL); /* return system time */
if(ltime != (time_t)-1)
{
ptr = localtime(&ltime); /* return broken down time */
strftime(str,80,"%Y%m%d",ptr);
puts(str);
ptr->tm_hour-=24;
ltime = mktime(ptr);
ptr = localtime(&ltime);
strftime(str,80,"%Y%m%d",ptr);
puts(str);
}
else puts("Time is not available");
return 0;
}
 
T

those who know me have no need of my name

in comp.lang.c i read:
ptr->tm_hour-=24;
ltime = mktime(ptr);
ptr = localtime(&ltime);

no need to call localtime again, mktime changes the struct's members.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top