计算从日期减去nmbr天数后的日期 [英] Calculate the date after subtracting nmbr of days form a date

查看:114
本文介绍了计算从日期减去nmbr天数后的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在为旧的borland C3.1应用程序添加一个新模块

(dos)。

我需要通过减去

a给定日期的天数来计算日期。


我知道我可以使用一天的天数在这几个月里回来的时候

在达到1时跳跃。 (记住2月29日和1月份/ b
)。

但是Borland C3.1中没有这个功能可以为我做这个吗?

我在帮助中找不到一个?


问候

Laery

解决方案

th******@hotpop.com (Laery )写在

新闻:c6 ************************** @ posting.google.c om:

我正在向旧的borland C3.1应用程序添加一个新模块
(dos)。
我需要通过减去数字来计算日期来自
给定日期的日子。


[...]

我在帮助中找不到一个?




查找difftime和localtime ......它们是至少从v1.5开始在

Borland库中的函数,并且应该帮助你做你所做的事情。 >
试图做。

John


Laery写道:




我正在为旧的borland C3.1应用程序添加一个新模块
(dos)。
我需要通过减去天数来计算日期来自
给定日期。




将日期加载到struct tm中,注意确保:


(a)你没有设置的所有字段都被清除为0,例如:

struct tm date =(0); / *现在,只要相信我这个* /

(b)年份字段包含全年减去1900(因此,对于2005年,

它将被设置到105)。

(c)月份字段在0到11的范围内。

(d)所有其他相关字段设置正确。


现在减去你想要的天数,然后将

struct'的地址传递给mktime(),在time_t中获取结果

对象。


如果结果不是(time_t)-1(这表示无法按照您的要求转换日期),它可以传递给localtime()

或gmtime(),它们都返回指向struct tm的指针,从中你可以提取所需的日期信息。


John< oz *** @ ozbus.net.au>写道:

th******@hotpop.com ( Laery)在
新闻中写道:c6 ************************** @ posting.google.c om:
< blockquote class =post_quotes>我正在向旧的borland C3.1应用程序(dos)添加一个新模块。
我需要通过减去
的天数来计算日期给定的日期。


我在帮助中找不到一个?



查找difftime和localtime。 ..他们是至少从v1.5以来一直在Borland图书馆工作的功能,应该帮助你做你想做的事。




不,他们不会。 difftime()给出两个

time_t'之间的差异,以秒为单位;它不允许你改变现有的time_t。 localtime()

从time_t转换为struct tm,但是没有做任何其他的

计算。

使用mktime()是正确的解决如果BC3.1还没有它

(即,如果它是预先ISO),你_may_能够减去

天* 24 * 3600来自time_t,但请注意,这取决于time_t是

a自纪元以来的直接秒数,它不需要

be 。这样做会使你的代码不可移植,但如果它已经是特定于Borland的b
,这可能不是问题。添加评论解释

黑客是个好主意,即便如此。


Richard


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

解决方案

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


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.


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


这篇关于计算从日期减去nmbr天数后的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆