[PATCH] Support ISO 8601 format dates for utilities such as touch

Nick Stoughton nstoughton at aether.com
Fri Jul 25 19:36:08 UTC 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20140725/0ecf1669/attachment.html>


More information about the busybox mailing list