[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] Support ISO 8601 format dates for utilities such as touch
From:       Nick Stoughton <nstoughton () aether ! com>
Date:       2014-07-25 19:36:08
Message-ID: CADO7NSE7oV5kx7sTbLqWxOCX5ABEfEZZBiU4Fa=XQi38JAO61w () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


POSIX specifies the -d option to touch as:
-d  date_time
Use the specified date_time instead of the current time.
The option-argument shall be a string of the form:
YYYY-MM-DDThh:mm:SS[.frac][tz]
or:

YYYY-MM-DDThh:mm:SS[,frac][tz]
where:

YYYY are at least four decimal digits giving the year.

MM, DD, hh, mm, and SS are as with -t time.

T is the time designator, and can be replaced by a single <space>.

[.frac] and [,frac] are either empty, or a <period> ( '.' ) or a <comma>
( ',' ) respectively, followed by one or more decimal digits, specifying
a fractional second.

[tz] is either empty, signifying local time, or the letter 'Z', signifying
UTC. If [tz] is empty, the resulting time shall be affected by the value
of the TZ environment variable.

This patch adds support for 'T' as the time designator and supports tz.
It does not implement fractional seconds. The parse_datestr() function
is used in both touch and date, so both these utilities benefit.
---
 libbb/time.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/libbb/time.c b/libbb/time.c
index aa19a47..820b1f9 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -7,10 +7,12 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 #include "libbb.h"
+#include "rtc_.h"

 void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
 {
  char end = '\0';
+ char sep = '\0';
  const char *last_colon = strrchr(date_str, ':');

  if (last_colon != NULL) {
@@ -41,11 +43,11 @@ void FAST_FUNC parse_datestr(const char *date_str,
struct tm *ptm)
  &ptm->tm_mon, &ptm->tm_mday,
  &ptm->tm_hour, &ptm->tm_min,
  &end) >= 5
- /* yyyy-mm-dd HH:MM */
- || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
- &ptm->tm_mon, &ptm->tm_mday,
+ /* yyyy-mm-dd HH:MM (also accepts ISO-8601 format dates) */
+ || sscanf(date_str, "%u-%u-%u%c%u:%u%c", &ptm->tm_year,
+ &ptm->tm_mon, &ptm->tm_mday, &sep,
  &ptm->tm_hour, &ptm->tm_min,
- &end) >= 5
+ &end) >= 6 && (sep == ' ' || sep == 'T')
  ) {
  ptm->tm_year -= 1900; /* Adjust years */
  ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
@@ -65,6 +67,11 @@ void FAST_FUNC parse_datestr(const char *date_str,
struct tm *ptm)
  /* xxx:SS */
  if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
  end = '\0';
+ if (end == 'Z') {
+ time_t utc = rtc_tm2time(ptm, 1);
+ localtime_r(&utc, ptm);
+ end = '\0';
+ }
  /* else end != NUL and we error out */
  }
  } else
-- 
1.9.3

*Nick Stoughton*
 *Aether Things Inc *
 *San Francisco*
  +1 (510) 388 1413

[Attachment #5 (text/html)]

<div dir="ltr"><div><div><div><div>POSIX specifies the -d option to touch \
as:</div><div>-d   date_time</div><div>Use the specified date_time instead of the \
current time.</div><div>The option-argument shall be a string of the form:</div> \
<div>YYYY-MM-DDThh:mm:SS[.frac][tz]</div><div>or:</div><div><br></div><div>YYYY-MM-DDThh:mm:SS[,frac][tz]</div><div>where:</div><div><br></div><div>YYYY \
are at least four decimal digits giving the year.</div><div><br></div> <div>MM, DD, \
hh, mm, and SS are as with -t time.</div><div><br></div><div>T is the time \
designator, and can be replaced by a single \
&lt;space&gt;.</div><div><br></div><div>[.frac] and [,frac] are either empty, or a \
&lt;period&gt; ( &#39;.&#39; ) or a &lt;comma&gt;</div> <div>( &#39;,&#39; ) \
respectively, followed by one or more decimal digits, specifying</div><div>a \
fractional second.</div><div><br></div><div>[tz] is either empty, signifying local \
time, or the letter &#39;Z&#39;, signifying</div> <div>UTC. If [tz] is empty, the \
resulting time shall be affected by the value</div><div>of the TZ environment \
variable.</div><div><br></div><div>This patch adds support for &#39;T&#39; as the \
time designator and supports tz.</div> <div>It does not implement fractional seconds. \
The parse_datestr() function  </div><div>is used in both touch and date, so both \
these utilities benefit.</div><div>---</div><div>  libbb/time.c | 15 \
+++++++++++----</div><div>  1 file changed, 11 insertions(+), 4 \
deletions(-)</div><div><br></div><div>diff --git a/libbb/time.c \
b/libbb/time.c</div><div>index aa19a47..820b1f9 100644</div><div>--- \
a/libbb/time.c</div><div>+++ b/libbb/time.c</div><div> @@ -7,10 +7,12 @@</div><div>   \
* Licensed under GPLv2, see file LICENSE in this source tree.</div><div>   \
*/</div><div>  #include &quot;libbb.h&quot;</div><div>+#include \
&quot;rtc_.h&quot;</div><div>  </div><div>  void FAST_FUNC parse_datestr(const char \
*date_str, struct tm *ptm)</div> <div>  {</div><div>  <span class="" \
style="white-space:pre">	</span>char end = &#39;\0&#39;;</div><div>+<span class="" \
style="white-space:pre">	</span>char sep = &#39;\0&#39;;</div><div>  <span class="" \
style="white-space:pre">	</span>const char *last_colon = strrchr(date_str, \
&#39;:&#39;);</div> <div>  </div><div>  <span class="" \
style="white-space:pre">	</span>if (last_colon != NULL) {</div><div>@@ -41,11 +43,11 \
@@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)</div><div>  \
<span class="" style="white-space:pre">					</span>&amp;ptm-&gt;tm_mon, \
&amp;ptm-&gt;tm_mday,</div> <div>  <span class="" \
style="white-space:pre">					</span>&amp;ptm-&gt;tm_hour, \
&amp;ptm-&gt;tm_min,</div><div>  <span class="" \
style="white-space:pre">					</span>&amp;end) &gt;= 5</div><div>-<span class="" \
style="white-space:pre">		</span>/* yyyy-mm-dd HH:MM */</div> <div>-<span class="" \
style="white-space:pre">		</span> || sscanf(date_str, &quot;%u-%u-%u %u:%u%c&quot;, \
&amp;ptm-&gt;tm_year,</div><div>-<span class="" \
style="white-space:pre">					</span>&amp;ptm-&gt;tm_mon, &amp;ptm-&gt;tm_mday,</div> \
<div>+<span class="" style="white-space:pre">		</span>/* yyyy-mm-dd HH:MM (also \
accepts ISO-8601 format dates) */</div><div>+<span class="" \
style="white-space:pre">		</span> || sscanf(date_str, &quot;%u-%u-%u%c%u:%u%c&quot;, \
&amp;ptm-&gt;tm_year,</div> <div>+<span class="" \
style="white-space:pre">					</span>&amp;ptm-&gt;tm_mon, &amp;ptm-&gt;tm_mday, \
&amp;sep,</div><div>  <span class="" \
style="white-space:pre">					</span>&amp;ptm-&gt;tm_hour, \
                &amp;ptm-&gt;tm_min,</div><div>
-<span class="" style="white-space:pre">					</span>&amp;end) &gt;= \
5</div><div>+<span class="" style="white-space:pre">					</span>&amp;end) &gt;= 6 \
&amp;&amp; (sep == &#39; &#39; || sep == &#39;T&#39;)</div><div>  <span class="" \
style="white-space:pre">		</span>) {</div> <div>  <span class="" \
style="white-space:pre">			</span>ptm-&gt;tm_year -= 1900; /* Adjust years \
*/</div><div>  <span class="" style="white-space:pre">			</span>ptm-&gt;tm_mon -= 1; \
/* Adjust month from 1-12 to 0-11 */</div> <div>@@ -65,6 +67,11 @@ void FAST_FUNC \
parse_datestr(const char *date_str, struct tm *ptm)</div><div>  <span class="" \
style="white-space:pre">			</span>/* xxx:SS */</div><div>  <span class="" \
style="white-space:pre">			</span>if (sscanf(last_colon + 1, &quot;%u%c&quot;, \
&amp;ptm-&gt;tm_sec, &amp;end) == 1)</div> <div>  <span class="" \
style="white-space:pre">				</span>end = &#39;\0&#39;;</div><div>+<span class="" \
style="white-space:pre">			</span>if (end == &#39;Z&#39;) {</div><div>+<span class="" \
style="white-space:pre">				</span>time_t utc = rtc_tm2time(ptm, 1);</div> \
<div>+<span class="" style="white-space:pre">				</span>localtime_r(&amp;utc, \
ptm);</div><div>+<span class="" style="white-space:pre">				</span>end = \
&#39;\0&#39;;</div><div>+<span class="" style="white-space:pre">			</span>}</div> \
<div>  <span class="" style="white-space:pre">			</span>/* else end != NUL and we \
error out */</div><div>  <span class="" style="white-space:pre">		</span>}</div><div> \
<span class="" style="white-space:pre">	</span>} else</div> <div>--  \
</div><div>1.9.3</div></div><div><br></div><div> <strong>Nick  Stoughton</strong><br>
<div style="font-size:medium">
	<span style="color:rgb(105,105,105)"><strong>Aether Things Inc  </strong></span>
	<div style="margin:0px;font-size:12px;display:inline!important">
		<div style="font-size:medium;display:inline!important">
			<b><b><font color="#444444">San Francisco</font></b></b></div>
	</div>
</div>
<div style="font-size:medium">
	+1 (510) 388 1413</div>
<div>
	  </div></div>
</div></div></div>



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic