date command


 
Thread Tools Search this Thread
Operating Systems Linux date command
# 1  
Old 10-12-2009
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  
Old 10-12-2009
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  
Old 10-14-2009
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  
Old 10-14-2009
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  
Old 10-15-2009
That will work for me too.
Please try "18/5/09" . Smilie
18 is day of month in EU forat.
# 6  
Old 10-16-2009
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  
Old 10-17-2009
Thanks

...Actually I already had written my own script doing that in korn shell(ksh).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

3. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

4. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

5. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

6. Shell Programming and Scripting

date command

Hi is it possible to give the date command like 24th July 2009 ive tried DATE=`date "+%d%m%Y" echo $DATE that only replies 2009 correctly. not sure how to display the month in full or if unix knows how to do i.e 2nd, 24th, 3rd (1 Reply)
Discussion started by: magnia
1 Replies

7. UNIX for Dummies Questions & Answers

date command

Hi All. I'm using date -a to 'drift' the time forward / backwards. The question is - how do I know when its finished 'drifting' ? On some systems I have another time reference I can use but not always. thanks (1 Reply)
Discussion started by: Mudshark
1 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. UNIX for Dummies Questions & Answers

Date Command

we're using HP-UX I need to change the year. What is the date command? Thanks (2 Replies)
Discussion started by: saldana
2 Replies

10. UNIX for Dummies Questions & Answers

date command

Using the date command how do get yesterday's date?? e.g. date '+%b%e%Y' July 30 2002 I need to get July 29 2002 using the date command. Thanx (p.s. sorry if it's a very obvious question) (6 Replies)
Discussion started by: niamo1
6 Replies
Login or Register to Ask a Question