Unix Shell - Date/Time problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Date/Time problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Date/Time problem
Stefanie

2004-10-06, 2:54 am

Hi all,

I am writing a Korn Shell script in which I need to read a line from a
logfile, parse the date from it and check whether that date is older
than five minutes.

The date has this format: "Oct 6 07:51:55 2004"

The problem now is, how can I format this date into unix timestamp
format, so I can compare how old it is?


Thanks,

Stefanie
Michael Tosch

2004-10-06, 7:50 am

In article <b8a63f96.0410060003.56eda17d@posting.google.com>, doofer_spam@yahoo.de (Stefanie) writes:
> Hi all,
>
> I am writing a Korn Shell script in which I need to read a line from a
> logfile, parse the date from it and check whether that date is older
> than five minutes.
>
> The date has this format: "Oct 6 07:51:55 2004"
>
> The problem now is, how can I format this date into unix timestamp
> format, so I can compare how old it is?
>
>
> Thanks,
>
> Stefanie


perl -le 'print time'

prints the date in Unix time format, i.e. seconds since 1970.
This is easy to calculate.

--
Michael Tosch
IT Specialist
HP Managed Services
Technology Solutions Group
Hewlett-Packard GmbH
Phone: +49 2407 575 313
Mail: michael.tosch:hp.com


Laurenz Albe

2004-10-06, 7:50 am

Stefanie <doofer_spam@yahoo.de> wrote:
> I am writing a Korn Shell script in which I need to read a line from a
> logfile, parse the date from it and check whether that date is older
> than five minutes.
>
> The date has this format: "Oct 6 07:51:55 2004"
>
> The problem now is, how can I format this date into unix timestamp
> format, so I can compare how old it is?


The most elegant solution is to use GNU date.

date -d 'Oct 6 07:51:55 2004' +%s

would do the trick.

Yours,
Laurenz Albe
Stephane CHAZELAS

2004-10-06, 7:50 am

2004-10-6, 01:03(-07), Stefanie:
> I am writing a Korn Shell script in which I need to read a line from a
> logfile, parse the date from it and check whether that date is older
> than five minutes.
>
> The date has this format: "Oct 6 07:51:55 2004"

[...]

With recent versions of ksh93, you can do:

$ printf '%(%s)T\n' 'Oct 6 07:51:55 2004'
1097049115

You could use GNU date:

$ date -d 'Oct 6 07:51:55 2004' +%s
1097045515

(notice the 1 hour difference because GNU date assumes the date
is in local time while ksh93 assumes it's in the GMT timezone).

You could also do:

date="Oct 6 07:51:55 2004"
set -f
IFS=" :"
set -- $date
months=JanFebMarAprMayJunJulAugSepOctNov
Dec
m=${months%%$1*}
typeset -Z2 month="$((${#m} / 3 + 1))" day=$2
touch -t"$6$month$day$3$4.$5" file1
TZ=GMT touch -t"$(TZ=GMT+0:5 date +%Y%m%d%H%M.%S)" file2
if [[ file1 -ot file2 ]]; then
echo "$date is more than 5 minutes old"
fi

--
Stephane
Ed Morton

2004-10-07, 2:56 am



Stefanie wrote:
> Hi all,
>
> I am writing a Korn Shell script in which I need to read a line from a
> logfile, parse the date from it and check whether that date is older
> than five minutes.
>
> The date has this format: "Oct 6 07:51:55 2004"
>
> The problem now is, how can I format this date into unix timestamp
> format, so I can compare how old it is?
>
>
> Thanks,
>
> Stefanie


See question 6 in the FAQ: http://home.comcast.net/~j.p.h/cus-faq.html#G

Regards,

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com