?? yesterday.msb.html
字號(hào):
<html><!-- Mirrored from c-faq.com/lib/yesterday.msb.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:01:39 GMT --><head><title></title></head><body>From: msb@sq.com (Mark Brader)<br>Subject: Re: routine for yesterday's date<br>Message-ID: your message <9603300430.AA19052@sqrex.sq.com><br>Date: Fri, 29 Mar 96 23:30:02 EST<p>Bruce Coghill writes:<br>> In my C manual the time.h and time functions are described<br>> and how I can pull off each element in the structure (like year,<br>> month, day, day-of-the-year), but it isn't clear to me how to subtract<br>> a day.<p>This is question 13.14 on the FAQ list, but I'd like to expand a biton what it says there.<p>Bruce, you have gotten as far as calling <TT>time()</TT> and <TT>localtime()</TT>. Thenext thing is to use the normalizing feature of <TT>mktime()</TT>. Suppose thatyou have a variable of type <TT>struct tm</TT> called <TT>x</TT>, and you've just put thecurrent time into it using <TT>localtime()</TT>. Now you basically just want todo something like this:<p><pre> x.tm_day--; /* back 1 day */ (void) mktime (&x); /* normalize */</pre><p>And probably someone is going to post to suggest just that. The abovecode will work most of the time, but it has two problems. First, <TT>mktime()</TT>is allowed to fail, so it's as well to check for it and report the error.<p>Second, it finds the date 24 hours ago. If there was a daylight savingtime transition, 24 hours ago might be today or the day before yesterday.<p>You might think to fix the first problem by adding the line:<pre> x.tm_isdst = -1;</pre>at the top, effectively forcing <TT>mktime()</TT> to work by wall clock time.Now you are asking for the same time yesterday, rather than the time 24hours ago. But this doesn't fix the problem -- in the case of a daylightsaving time transition, ``the same time yesterday'' might be ambiguous ornot exist. In such a case a failure of <TT>mktime()</TT> is allowed (or perhapsrequired, depending on how one interprets the standard).<p>The correct fix is to let the daylight saving time transition take itseffect by leaving <TT>tm_isdst</TT> alone, but to ask for the time 24 hours beforea time near noon today. Even if that time is 11 am or 1 pm, it will stillbe yesterday. So the final version of the code is:<p><pre> x.tm_day--; /* back 1 day (well, 24 hours) */ x.tm_hour = 12; /* from some time between noon and 1 pm */ /* and normalize */ if (mktime (&x) == (time_t) -1) { fprintf (stderr, "mktime failed!!\n"); exit(1); }</pre><p>The structure now contains the correct year, month, and day values foryesterday. As always, remember that the representation of a <TT>struct tm</TT>requires you to add 1900 to the year and 1 to the month to get the correctvalues if displaying them numerically.<p>There is a terser version of the ``final'' code: replace the first two lines by<pre> x.tm_hour = -12; /* 12 hours before sometime between * midnight and 1 am this morning */</pre>but the meaning of this is less obvious, so I don't recommend it.<p><pre>-- Mark Brader "To err is human, but to really mess things upmsb@sq.com you need a timetable planner!"SoftQuad Inc., Toronto -- Richard Porter</pre><p>My text in this article is in the public domain.</body><!-- Mirrored from c-faq.com/lib/yesterday.msb.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:01:39 GMT --></html>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -