date command

I need to compare 2 dates in european format (dd/mm/yyy).
date -d<my date> %s command converts date into unix epoch (integer), thus make it easy to compare.

The problem is that -d (or --date) option interprets date in US format-ie mm/dd/yyy.
Should any locales be changed to fix this?

You could so that but an easier approach might be to do simply prepend LC_TIME=YOUR_LOCALE to the date utility i.e.

date
Mon Oct 12 09:28:36 EDT 2009
$ LC_TIME=fr_FR.ISO-8859-1 date
lun oct 12 09:29:21 EDT 2009

it didnt work. Actually its is discovered that its not possible to use european date in date command./seems doc supports that/.

So I written my own script.!

Humm, works for me on RHEL5.4.

$ LC_TIME=fr_FR.ISO-8859-1 date -d "07/5/09"
dim jui  5 00:00:00 EDT 2009
$

That will work for me too.
Please try "18/5/09" . :slight_smile:
18 is day of month in EU forat.

Ahha, I see your problem now. I did some digging around in the source code for the GNU date utility. It turns out that date's getdate.y code is not yet internationalized. So you are out of luck as far as using the date utility for this purpose. I checked the ksh93 shell prinf %T functionality and it exhibits the same problem so ksh93 is out also.

---------- Post updated at 06:53 PM ---------- Previous update was at 04:21 PM ----------

I got to thinking further about your problem and, as a result, here is a simple C utility I wrote which enables you to set a variable to the number of seconds since the Epoch based on your input strings and TZ settings.

This handles your particular situation.

/*
** DATE2EPOCH   F.P.Murphy Oct 16th, 2009
**
** USAGE: date2epoch [-d] [-f dateformat] datestring
**
**        -d turn on debugging
**        -f format strings per strptime(2) to match inputted date
**
** OUTPUT:  Prints number of seconds since the Epoch
**
** EXAMPLE: date2epoch -f "%a, %b %d, %Y %T %p" "Tue, Feb 19, 2008 08:00:02 PM"
**
**    NOTE: you may have to set the TZ environmental variable to account for
**          daylight savings time errors in your zonefile. For example on east
**          coast of US, you would currently set TZ to "EST,DST"
*/

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <locale.h>


int
main(int argc,
     char* argv[])
{
   struct tm tm1, tm2;
   char buf[50];
   char format[50];
   char date[80];
   time_t t1;

   int c;
   int debug = 0;
   int errflg = 0;

   extern char *optarg;
   extern int optind, optopt;

   setlocale(LC_ALL, "");
   strcpy(format, "%m/%d/%y");        /* default format */

   while ((c = getopt(argc, argv, "df:h")) != -1) {
        switch(c) {
        case 'd':
            debug = 1;
            break;
        case 'f':
            strcpy(format, optarg);
            break;
        case 'h':
            errflg++;
            break;
        case '?':
            fprintf(stderr, "Unknown option: -%c\n", optopt);
            errflg++;
        }
   }
   if (errflg || (argc - optind != 1)) {
       fprintf(stderr, "Usage: date2epoch [-d] [-f dateformat] datestring\n");
       exit(2);
   }

   strcpy(date, argv[optind]);

   if (debug) {
       fprintf(stderr, "Format: %s\n", format);
       fprintf(stderr, "Date: %s\n", date);
   }

   if (!strptime(date, format, &tm1)) {
       fprintf(stderr, "strptime() error\n");
       exit(1);
   }
   strftime(buf, 50, "%d/%m/%y %H:%M:%S", &tm1);
   if ((t1 = mktime(&tm1)) == -1) {
       fprintf(stderr, "mktime() error\n");
       exit(1);
   }

   printf("%ld", (long) t1);

   exit(0);
}

Here is sample output

$ ./date2epoch -f "%d/%m/%y %H:%M:%S" "18/6/09 12:00:00"
1245344400$
$ date1=$(./date2epoch -f "%d/%m/%y %H:%M:%S" "18/6/09 12:00:00")
$ echo $date1
1245344400
$ echo $TZ
EST,DST
$

...Actually I already had written my own script doing that in korn shell(ksh).