473,379 Members | 1,337 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,379 software developers and data experts.

Calculate the date after subtracting nmbr of days form a date

Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.

I know I could use an array of days in the months and go back by
leaping when 1 is reached. (keeping the 29the of feb and january/year
in mind).
But is there no function in Borland C3.1 which will do this for me?

I couldn't find one in the help?

Regards
Laery
Nov 14 '05 #1
11 6435
th******@hotpop.com (Laery) wrote in
news:c6**************************@posting.google.c om:
I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.
[...]
I couldn't find one in the help?


Look-up difftime and localtime... They're functions that have been in the
Borland libraries at least since v1.5 and should help you do what you're
trying to do.
John
Nov 14 '05 #2
Laery wrote:

Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.


Load the date into a struct tm, taking care to ensure that:

(a) all fields you don't set are cleared to 0, eg with:
struct tm date = (0); /* for now, just trust me on this */
(b) the year field contains the full year less 1900 (so, for 2005,
it would be set to 105).
(c) the month field is in the range 0 to 11.
(d) all other relevant fields are set correctly.

Now subtract the number of days you want, and then pass the
struct's address to mktime(), catching the result in a time_t
object.

If the result is not (time_t)-1 (which would indicate failure to
convert the date as you require), it can be passed to localtime()
or gmtime(), which both return pointers to struct tm from which
you can extract the date information you require.
Nov 14 '05 #3
John <oz***@ozbus.net.au> wrote:
th******@hotpop.com (Laery) wrote in
news:c6**************************@posting.google.c om:
I'm currently adding a new module to an old borland C3.1 application (dos).
And I need to calculate a date by subtracting the number of days from
a given date.

I couldn't find one in the help?


Look-up difftime and localtime... They're functions that have been in the
Borland libraries at least since v1.5 and should help you do what you're
trying to do.


No, they won't. difftime() gives the difference, in seconds, between two
time_t's; it doesn't allow you to change an existing time_t. localtime()
converts from time_t to struct tm, but doesn't do any other
computations.
Using mktime() is the right solution. If BC3.1 doesn't have it yet
(i.e., if it's pre-ISO), you _may_ be able to get away with subtracting
days*24*3600 from a time_t, but do note that this relies on time_t being
a straight number of seconds since the epoch, which it isn't required to
be. Doing so would make your code unportable, but if it's already
Borland-specific, that may not be a problem. Adding a comment explaining
the hack would be a good idea, even so.

Richard
Nov 14 '05 #4
Laery wrote:
Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.

I know I could use an array of days in the months and go back by
leaping when 1 is reached. (keeping the 29the of feb and january/year
in mind).
But is there no function in Borland C3.1 which will do this for me?

I couldn't find one in the help?

Regards
Laery


I have a really robust date library which was published in the C User's
Journal a few years ago. Drop me a line and I will email it to you
(damn, I've got to get that web page up!). I also used it to write a
Unix shell utility called ADU (a date utility) that allows you to do
date math in the Unix shell. If there were a decent shell for Windows
you could use it there too.
Nov 14 '05 #5
On Thu, 24 Feb 2005 11:04:15 +0000 (UTC), infobahn
<in******@btinternet.com> wrote in comp.lang.c:
Laery wrote:

Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.


Load the date into a struct tm, taking care to ensure that:

(a) all fields you don't set are cleared to 0, eg with:
struct tm date = (0); /* for now, just trust me on this */


ITYM struct tm date = { 0 };

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
In article <42***************@btinternet.com>,
in******@btinternet.com says...
Laery wrote:

Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.


Load the date into a struct tm, taking care to ensure that:

(a) all fields you don't set are cleared to 0, eg with:
struct tm date = (0); /* for now, just trust me on this */


It would be more trustworthy with braces instead of parens. :-)

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #7
Jack Klein wrote:

On Thu, 24 Feb 2005 11:04:15 +0000 (UTC), infobahn
<in******@btinternet.com> wrote in comp.lang.c:
(a) all fields you don't set are cleared to 0, eg with:
struct tm date = (0); /* for now, just trust me on this */


ITYM struct tm date = { 0 };


I do indeed. I was clearly tempting fate a little too much with that
comment.
Nov 14 '05 #8
Randy Howard wrote:

In article <42***************@btinternet.com>,
in******@btinternet.com says...
Laery wrote:

Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.


Load the date into a struct tm, taking care to ensure that:

(a) all fields you don't set are cleared to 0, eg with:
struct tm date = (0); /* for now, just trust me on this */


It would be more trustworthy with braces instead of parens. :-)


<sigh>
Some days, it just doesn't pay to fire up your newsreader. :-$
</sigh>
Nov 14 '05 #9
Hello Stan

If you could mail me a copy of the date library you are talking about,
that will be very kind of you. Just mail it to me at

ptiwaryATmahindrabtDOTcom

Thanks in advance.

Nov 14 '05 #10
In article <42***************@btinternet.com>,
in******@btinternet.com says...
<sigh>
Some days, it just doesn't pay to fire up your newsreader. :-$
</sigh>


Unfortunately, I know exactly what you mean.

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #11
Hi,

Thanks for all the responses.

Regards
Laery
th******@hotpop.com (Laery) wrote in message news:<c6**************************@posting.google. com>...
Hi,

I'm currently adding a new module to an old borland C3.1 application
(dos).
And I need to calculate a date by subtracting the number of days from
a given date.

I know I could use an array of days in the months and go back by
leaping when 1 is reached. (keeping the 29the of feb and january/year
in mind).
But is there no function in Borland C3.1 which will do this for me?

I couldn't find one in the help?

Regards
Laery

Nov 14 '05 #12

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

Similar topics

2
by: JP SIngh | last post by:
Hi All I need to calculate the number of working days between the two dates entered on an ASP page. I am not that great a coder in ASP and was wondering if someone can help. Basically the...
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
3
by: shmoopie | last post by:
Hi, I have a php form that I want to use to pass a user specified "start date" and "end date" to a mysql database to retrieve reservations. I want the number of days up to and including the "end...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.