bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch] add 'hint' for dateconversion in 'date'


From: walter harms
Subject: [patch] add 'hint' for dateconversion in 'date'
Date: Sun, 19 Jun 2005 16:53:52 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040114

Hi,
this patch will add the option -D to date. It can be used to suply -d with a hint. that is usefull if the translation is ambiguous. i do a lot of date translations and found that this may be helpfull for other also.
The patch itself is only a few lines but depends on strptime().

The usage lines are already fixed. please check my spelling.

I posted the same idea to the busybox ml (just in case you hear from it).

Example: translate epoch

without hint date has no idea what to do

./date -d "946720800"
./date: invalid date `946720800'

now it gets a hint what it is and prints a usefull day
./date -d "946720800"  -D"%s"
Sam Jan  1 12:00:00 CET 2000

We also have long options build in.
./date -d "946720800"  --hint="%s"
Sam Jan  1 12:00:00 CET 2000


happy hacking,
        walter


--- date.c.bak  2005-06-19 16:24:28.000000000 +0200
+++ date.c      2005-06-19 16:26:06.000000000 +0200
@@ -76,10 +76,11 @@
 /* If non-zero, display time in RFC-(2)822 format for mail or news. */
 static int rfc_format = 0;
 
-#define COMMON_SHORT_OPTIONS "Rd:f:r:s:u"
+#define COMMON_SHORT_OPTIONS "D:Rd:f:r:s:u"
 
 static struct option const long_options[] =
 {
+  {"hint", optional_argument, NULL, 'D'},
   {"date", required_argument, NULL, 'd'},
   {"file", required_argument, NULL, 'f'},
   {"iso-8601", optional_argument, NULL, 'I'},
@@ -123,6 +124,7 @@
       fputs (_("\
 Display the current time in the given FORMAT, or set the system date.\n\
 \n\
+  -D, --hint=HINTSTRING     interpred time described by -d STRING with 
HINTSTRING, do not guess\n\
   -d, --date=STRING         display time described by STRING, not `now'\n\
   -f, --file=DATEFILE       like --date once for each line of DATEFILE\n\
   -ITIMESPEC, --iso-8601[=TIMESPEC]  output date/time in ISO 8601 format.\n\
@@ -284,11 +286,25 @@
   return status;
 }
 
+/* 
+   we could easly use strftime here but we want to be with the flow and
+   use the show_date stuff
+*/
+
+static time_t get_date2(const char *datestr,const char *fmt)
+{
+  struct tm tm;
+  strptime(datestr,fmt,&tm);  
+  return timegm(&tm);
+}
+
+
 int
 main (int argc, char **argv)
 {
   int optc;
   const char *datestr = NULL;
+  const char *hintstr = NULL;
   const char *set_datestr = NULL;
   struct timespec when;
   int set_date = 0;
@@ -320,6 +336,9 @@
       case 'd':
        datestr = optarg;
        break;
+      case 'D':
+       hintstr = strdupa(optarg);
+       break;
       case 'f':
        batch_file = optarg;
        break;
@@ -359,6 +378,14 @@
                           + (batch_file ? 1 : 0)
                           + (reference ? 1 : 0));
 
+  if (hintstr != NULL && datestr == NULL)
+    {
+      error (0, 0,
+       _("Hint given but no date to convert"));
+      usage (EXIT_FAILURE);
+    }
+
+
   if (option_specified_date > 1)
     {
       error (0, 0,
@@ -449,7 +476,11 @@
            }
          else
            {
-             when.tv_sec = get_date (datestr, NULL);
+             if (hintstr != NULL)
+               when.tv_sec = get_date2(datestr,hintstr);
+             else
+               when.tv_sec = get_date (datestr, NULL);
+
              when.tv_nsec = 0; /* FIXME: get_date should set this.  */
              valid_date = (when.tv_sec != (time_t) -1);
            }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]