473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

work with dates

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
Nov 13 '05 #1
5 8170
Fernando wrote:
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
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #2
On Thu, 30 Oct 2003 18:57:09 GMT, Artie Gold <ar*******@austin.rr.com> wrote:
Fernando wrote:
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

Nov 13 '05 #3
rihad wrote:
On Thu, 30 Oct 2003 18:57:09 GMT, Artie Gold <ar*******@austin.rr.com> wrote:

Fernando wrote:
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

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #4


Fernando wrote:
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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #5
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.
strftime(str,80,"%Y%m%d",ptr);


--
a signature
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Riley | last post by:
The date fields being saved by a VB program were being saved as #2003-11-22#. For reasons unknown to me these dates began to be saved as "11/22/2003" All of these dates were made dates with the...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
2
by: Rachel Suddeth | last post by:
Is there a way to have the non-selectable dates (those before MinDate and after MaxDate) draw differently so my users can see right away what dates aren't allowed? I'm not seeing it... ...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
2
by: angi35 | last post by:
Hi, I'm working in Access 2000. I have a form with a series of date fields, showing the progress of a project from start to completion. There's a set of fields/controls for projected dates (when...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.