⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yesterday.msb.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<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 &lt;9603300430.AA19052@sqrex.sq.com&gt;<br>Date: Fri, 29 Mar 96 23:30:02 EST<p>Bruce Coghill writes:<br>&gt; In my C manual the time.h and time functions are described<br>&gt; and how I can pull off each element in the structure (like year,<br>&gt; month, day, day-of-the-year), but it isn't clear to me how to subtract<br>&gt; 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&nbsp;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 (&amp;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 (&amp;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&nbsp;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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -