The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > Linux
.
google unix.com



Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date command Mudshark UNIX for Dummies Questions & Answers 1 11-03-2008 03:07 PM
want to get previous date from date command in ksh rinku Shell Programming and Scripting 4 09-04-2008 08:58 AM
help with date command netrom Shell Programming and Scripting 9 08-06-2008 02:37 AM
Help with a Date Command !!! kumarsaravana_s UNIX for Dummies Questions & Answers 1 05-28-2007 04:33 PM
date command vshyam4949 UNIX for Dummies Questions & Answers 2 01-30-2006 11:40 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-12-2009
mitan_shahverdy mitan_shahverdy is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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?
  #2 (permalink)  
Old 10-12-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,927
You could so that but an easier approach might be to do simply prepend LC_TIME=YOUR_LOCALE to the date utility i.e.
Code:
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
  #3 (permalink)  
Old 10-14-2009
mitan_shahverdy mitan_shahverdy is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
th

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.!
  #4 (permalink)  
Old 10-14-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,927
Humm, works for me on RHEL5.4.
Code:
$ LC_TIME=fr_FR.ISO-8859-1 date -d "07/5/09"
dim jui  5 00:00:00 EDT 2009
$
  #5 (permalink)  
Old 10-15-2009
mitan_shahverdy mitan_shahverdy is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
That will work for me too.
Please try "18/5/09" .
18 is day of month in EU forat.
  #6 (permalink)  
Old 10-16-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,927
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.
Code:
/*
** 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
Code:
$ ./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
$
  #7 (permalink)  
Old 10-17-2009
mitan_shahverdy mitan_shahverdy is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
Thanks

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0