📄 calendar.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file libdatetime.sgml dated Wed Dec 21 12:57:15 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/lib/calendar.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:59 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 13.14</title><link href="mktime.html" rev=precedes><link href="y2k.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="mktime.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="y2k.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 13.14</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I addNdays to a date?How can I find the difference between two dates?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The ANSI/ISO Standard C <TT>mktime</TT> and<TT>difftime</TT> functionsprovidesome(limited)support forboth problems.<TT>mktime</TT> accepts non-normalized dates,so it is straightforward totake a filled-in <TT>struct tm</TT>,add or subtract from the <TT>tm_mday</TT> field,and call <TT>mktime</TT> to normalize the year, month, and day fields(andincidentallyconvert to a <TT>time_t</TT> value).<TT>difftime</TT> computes the difference,in seconds,between two <TT>time_t</TT> values;<TT>mktime</TT> can be used to compute <TT>time_t</TT> values for two dates tobe subtracted.</p><p>However,these solutionsare guaranteed to work correctlyonlyfor datesin the rangewhich can be represented as <TT>time_t</TT>'s<a href="fn73.html" rel=subdocument>[footnote]</a>.The <TT>tm_mday</TT> field is an <TT>int</TT>,so day offsets of more than 32,736or somay cause overflow.(See below for an alternative solution without these limitations.)Note alsothatatdaylight saving timechangeovers,local daysare not24 hours long,so be carefulif you try to divideby 86,400 seconds/day.</p><p>Here is a code fragment tocompute the date 90 days past October 24, 1994:<pre>#include <stdio.h>#include <time.h>tm1.tm_mon = 10 - 1;tm1.tm_mday = 24;tm1.tm_year = 1994 - 1900;tm1.tm_hour = tm1.tm_min = tm1.tm_sec = 0;tm1.tm_isdst = -1;tm1.tm_mday += 90;if(mktime(&tm1) == -1) fprintf(stderr, "mktime failed\n");else printf("%d/%d/%d\n", tm1.tm_mon+1, tm1.tm_mday, tm1.tm_year+1900);</pre>(Setting <TT>tm_isdst</TT> to -1helps to guard against daylight saving time anomalies;setting <TT>tm_hour</TT> to 12 would, too.)</p><p>Here isa piece ofcode tocompute the differencein daysbetweenFebruary 28 and March 1 in the year 2000:<pre> struct tm tm1, tm2; time_t t1, t2; tm1.tm_mon = 2 - 1; tm1.tm_mday = 28; tm1.tm_year = 2000 - 1900; tm1.tm_hour = tm1.tm_min = tm1.tm_sec = 0; tm1.tm_isdst = -1; tm2.tm_mon = 3 - 1; tm2.tm_mday = 1; tm2.tm_year = 2000 - 1900; tm2.tm_hour = tm2.tm_min = tm2.tm_sec = 0; tm2.tm_isdst = -1; t1 = mktime(&tm1); t2 = mktime(&tm2); if(t1 == -1 || t2 == -1) fprintf(stderr, "mktime failed\n"); else { long d = (difftime(t2, t1) + 86400L/2) / 86400L; printf("%ld\n", d); }</pre>(The addition of <TT>86400L/2</TT> rounds the difference to the nearest day;see also question <a href="../fp/round.html">14.6</a>.)</p><p>Another approach to both problems,which will work over a much wider range of dates,is to use``Julian day numbers''.AJulian day number is thenumber of days since January 1, 4013 BC.<a href="julepoch.html" rel=subdocument>[footnote]</a>Given <TT>ToJul</TT> and <TT>FromJul</TT> routinesdeclared as<pre>/* returns Julian for month, day, year */long ToJul(int month, int day, int year);/* returns month, day, year for jul */void FromJul(long jul, int *monthp, int *dayp, int *yearp);</pre>adding <TT>n</TT> days to a date can be implemented as<pre> int n = 90; int month, day, year; FromJul(ToJul(10, 24, 1994) + n, &month, &day, &year);</pre>and the number of days between two dates is<pre> ToJul(3, 1, 2000) - ToJul(2, 28, 2000)</pre>Code for handlingJulian day numberscan be found inthe Snippets collection(see question <a href="../resources/miscsrcs.html">18.15c</a>),the Simtel/Oakland archives(file JULCAL10.ZIP,see question <a href="../resources/sources.html">18.16</a>),andthe ``Date conversions'' article mentioned in the References.</p><p>See also questions<a href="mktime.html">13.13</a>,<a href="../misc/zeller.html">20.31</a>,and<a href="../misc/leapyear.html">20.32</a>.</p><p>Additional links:<br><br><a href="yesterday.msb.html">further explanation</a> by Mark Brader<br><br><a href="calendar.br.html">more code</a>for date-difference and day-of-week calculationby Branko Radovanovic</p><p>References:K&R2 Sec. B10 p. 256<br>ISO Secs. 7.12.2.2,7.12.2.3<br>H&S Secs. 18.4,18.5 pp. 401-2<br>David Burki, ``Date Conversions''<br></p><!-- aend --><p><hr><a href="mktime.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="y2k.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/lib/calendar.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:59 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -