Time difference

Hi,

I have a tab delimited file with GMT time. How to convert the GMT to PST time and store the line only if date falls to 2 days ago date.
Say today is 16, date should be of 14. or else remove the line

abc  -       -       [13/Jun/2010:06:30:04    +0000]
efg  -       -       [14/Jun/2010:23:30:35    +0000]
hij  -       -       [15/Jun/2010:08:00:56    +0000]
kln  -       -       [15/Jun/2010:01:00:34    +0000]
abc  -       -       [16/Jun/2010:06:30:04    +0000]
efg  -       -       [17/Jun/2010:23:30:35    +0000]
hij  -       -       [15/Jun/2010:08:00:56    +0000]
 kln  -       -       [16/Jun/2010:01:00:34    +0000]

Hi, if you have GNU date you can do this to print dates up to 2 days ago:

while IFS="$IFS[]:/" read c1 c2 c3 day mon year hour min sec tz; do
  if [ $(date -d "2 days ago" +%s) -le $(date -d "$day $mon $year $hour:$min:$sec +$tz" +%s) ] ; then
    printf "%s\t%s\t%s\t[%s\t%s]\n" $c1 $c2 $c3 $(TZ=PST8PDT date -d "$day $mon $year $hour:$min:$sec +$tz" "+%d/%b/%Y:%H:%M:%S %z")
  fi
done<infile

output:

abc     -       -       [15/Jun/2010:23:30:04   -0700]
efg     -       -       [17/Jun/2010:16:30:35   -0700]
kln     -       -       [15/Jun/2010:18:00:34   -0700]

Otherwise it gets a lot more complicated and you have to look for shell libraries that are posted on this forum.

Check the FAQ article:

http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

You should find what you're after in there :slight_smile:

date: invalid date `13 Jun 2010 06:30:04 ++0000]' ./time_pst.sh: line 4: [: 1276600837: unary operator expected

I am getting this error when i run the script .

---------- Post updated at 11:42 PM ---------- Previous update was at 06:22 AM ----------

date: invalid date `13 Jun 2010 06:30:04 ++0000]' error when i run the script. Can you please tell me what is the problem

---------- Post updated 06-18-10 at 01:53 AM ---------- Previous update was 06-17-10 at 11:42 PM ----------

Please tell me why is this error I am getting

I am guessing you do not have GNU date?

Hi,

How to check about the GNU date.
If not can you please tell me what is the other solution which can be used

date --version

Should say date (GNU coreutils)

date --version

date (coreutils) 5.2.1

So which date format I should use

Long shot (I do not have your coreutils version), what output does this give:

while IFS="$IFS[]:/" read c1 c2 c3 day mon year hour min sec tz; do
  if [ $(date -d "2 days ago" +%s) -le $(date -d "$day $mon $year $hour:$min:$sec +$tz" +%s) ] ; then
    printf "%s\t%s\t%s\t[%s\t%s]\n" $c1 $c2 $c3 $(TZ=PST8PDT date -d "$day $mon $year $hour:$min:$sec $tz" "+%d/%b/%Y:%H:%M:%S %z")
  fi
done<infile

The output gives me is

date: invalid date `13 Jun 2010 06:30:04 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `14 Jun 2010 23:30:35 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `15 Jun 2010 08:00:56 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `15 Jun 2010 01:00:34 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `16 Jun 2010 06:30:04 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `17 Jun 2010 23:30:35 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `15 Jun 2010 08:00:56 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected
date: invalid date `16 Jun 2010 01:00:34 ++0000]'
./a.sh: line 3: [: 1276834662: unary operator expected

It seems your date command does not understand +tz
How about this?

while IFS="$IFS[]:/" read c1 c2 c3 day mon year hour min sec tz; do
  if [ $(date -u -d "2 days ago" +%s) -le $(date -u -d "$((day+tz)) $mon $year $hour:$min:$sec" +%s) ] ; then
    printf "%s\t%s\t%s\t[%s\t%s]\n" $c1 $c2 $c3 $(TZ=PST8PDT date -d "$day $mon $year $hour:$min:$sec $tz" "+%d/%b/%Y:%H:%M:%S %z")
  fi
done<infile

or:

while IFS="$IFS[]:/" read c1 c2 c3 day mon year hour min sec tz; do
  if [ $(TZ=GMT date -d "2 days ago" +%s) -le $(TZ=GMT date -d "$((day+tz)) $mon $year $hour:$min:$sec" +%s) ] ; then
    printf "%s\t%s\t%s\t[%s\t%s]\n" $c1 $c2 $c3 $(TZ=PST8PDT date -d "$day $mon $year $hour:$min:$sec $tz" "+%d/%b/%Y:%H:%M:%S %z")
  fi
done<infile