Dd/mm/yy or dd/mm/yyyy to UNIX epoch

Hi Experts,

I'm facing an issue with date. My requirement is as follows

./script "dd/mm/yy" or ./script "dd/mm/yyyy" 

epoch time

Im using solaris 10 and have downloaded gnu date.

I have tried using

[root@###### /usr/local/bin]$/usr/local/bin/date -d '25/02/2013'
/usr/local/bin/date: invalid date '25/02/2013'

Any ideas.

Thanks in Advance.

Hi, this code works with ksh

HOUR=12
MIN=41
SEC=11
DAY=01
MONTH=01
YEAR=2012
set -A MONTHS 0 0 31 59 90 120 151 181 212 243 273 304 334 365
echo "b=0;if(${MONTH}>2) if (${YEAR}%4==0) b=1; \
${SEC}+${MIN}*60+${HOUR}*3600+(${MONTHS[${MONTH}]} + ${DAY} + b-1)*86400+\
(${YEAR}-1970)*31536000+((${YEAR}-1969)/4)*86400" | bc

I have finished a bash script for it,it is as follows

#!/usr/bin/bash
#
# SHA : Tue Feb 26 02:19:26 EST 2013
# This procedure takes date in dd/mm/yyyy or dd/mm/yy format as input and outputs in UNIX epoch time

USAGE ()
{

echo -e "\n"

echo "`/usr/xpg4/bin/basename "$0"`  dd/mm/yyyy or dd/mm/yy"

echo -e "\n";echo "Example :  `/usr/xpg4/bin/basename "$0"` 24/02/2013 or `/usr/xpg4/bin/basename "$0"` 24/02/13"

echo -e "\n"

exit

}


INP_VALIDATION ()
{

if [ $# -eq 0 ]

then

 USAGE

fi

dd=`echo "$1" | /usr/xpg4/bin/awk -F'/' '{print $1}'`

mm=`echo "$1" | /usr/xpg4/bin/awk -F'/' '{print $2}'`

yy=`echo "$1" | /usr/xpg4/bin/awk -F'/' '{print $3}'`

YRLNGTH=`echo "$yy" | /usr/xpg4/bin/awk  '{print length}'`


if [ $dd -le 31 ] && [ $dd -ge 01 ]

then

DD=`echo "$dd"`

else

USAGE

fi

if [ $mm -le 12 ] && [ $mm -ge 01 ]

then

MM=`echo "$mm"`

else

USAGE

fi




 if [ $YRLNGTH -eq 02 ]

   then

  CURYR=`date +%y`

 if [ $yy -gt $CURYR ]

   then

  oyy=`echo "19$yy"`

  YY=`echo "$oyy"`

 elif [ $yy -le $CURYR ]

    then

  nyy=`echo "20$yy"`

  YY=`echo "$nyy"`

else

 USAGE

 fi



 elif [ $YRLNGTH -eq 04 ]

     then

   YY=`echo "$yy"`

    else

    USAGE

 fi

export DATEINP=`echo "$YY/$MM/$DD"`


}

INP_VALIDATION $1

## USING GNU COMPILED DATE

/usr/local/bin/date --date "$DATEINP" +%s


The output is as follows

[root@####/]$getdate 26/02/2013
1361797200
[root@#### /]$getdate 26/02/13
1361797200
[root@##### /]$


Any ideas to improvise the one i wrote would be highly appreciated

Thanks in advance:b:

Whenever you do VAR=`echo asdf` that's -- you might as well do VAR="asdf" and avoid the middleman.

You can replace most of your echo | awk with one shell statement.

There's no point exporting a variable if you're feeding it into something's commandline arguments directly.

IFS="/" read yy mm dd <<EOF
$1
EOF

YRLNGTH="${#yy}"

...

DATEINP="$YY/$MM/$DD"

Perhaps if your function fails to validate date pass it on to gnudate anyway:

if INP_VALIDATION "$*"
then
    /usr/local/bin/date --date "$DATEINP" +%s
else
    /usr/local/bin/date --date "$*" +%s
fi

Above requires all USAGE calls in INP_VALIDATION to be changed to return 1

This will allow you to do:

$ getdate today -1day
1361765980
$ getdate next tuesday
1362405600
$ getdate 17 Dec 2012
1355666400

Thank you all, will amend as per recommendation:b:.

Hi all,
Can any1 suggest me how can i get the epoch time of file.
i mean...
ls -l test.txt | awk '{print $6,""$7,""$8}' gives me Nov 30 2011...
now is there any way i can convert this into epoch time for this particular file only...
Thanks

With GNU-ls you can use

$ ls -l --time-style=+%s
total 9224
-rwxr-xr-x. 1 root root      123 1336547198 alsaunmute
-rwxr-xr-x. 1 root root    27776 1334668172 arch
lrwxrwxrwx. 1 root root        4 1355497199 awk -> gawk
-rwxr-xr-x. 1 root root    26264 1334668172 basename
-rwxr-xr-x. 1 root root   938736 1335178427 bash
-rwxr-xr-x. 1 root root    48568 1334668172 cat

throwing error:

ls: Not a recognized flag: -
ls: Not a recognized flag: -
ls: Not a recognized flag: y
ls: Not a recognized flag: =
ls: Not a recognized flag: +
ls: Not a recognized flag: %

GNU not available...
m working in ksh shell

---------- Post updated at 03:23 PM ---------- Previous update was at 03:20 PM ----------

Need to create a script:
where i will pass filename as an argument and have to check if this file shud not be older than 2 days. If file is older than 2 days send mail that file not updated and if its not older than move it to updated folder.

Thanks:

@vipin kumar
use stat command in this way:

stat -c %Z test.txt

and read this

man stat

---------- Post updated at 11:42 AM ---------- Previous update was at 11:17 AM ----------

@vipin kumar
If you can't use stat command try this

FILE=test.txt
if [[ ! -f $FILE ]]
then
echo $FILE not exists
else
N=$(find . ! -name . -prune -name $FILE -mtime +2|wc -l)
if [[ $N -eq 0 ]]
then
echo $FILE is updated
else
echo $FILE is older
fi
fi

If you have ksh93 available, you can also use the printf builtin to convert the date and time:

$ printf "%(%s)T\n" "Mar 27 09:17"
1364372220

This does NOT work with /usr/bin/printf , only the builtin version!

---------- Post updated at 11:49 ---------- Previous update was at 11:47 ----------

To be more precise:

$ ls -l x
-rw-r--r--. 1 hergp rzadmin 255 Mar 27 09:17 x
$ printf "%(%s)T\n" "$(ls -l x | awk '{ print $6, $7, $8 }')"
1364372220

for existing file this code worked..but when i created new file say vip.txt...thn its showing file not exists instead it shud show that vip.txt is updated bcoz it has been created or modified today not 2 days back..

in short,
if sysdate=27 march 2013
and file date=25 march 2012 or older than that thn it will show not updated else updated.

---------- Post updated at 04:28 PM ---------- Previous update was at 04:27 PM ----------

Its showing ()T as an ouptut

Then you don't have the kornshell 93, I am afraid.

@vipin kumar
You're saying that

FILE=mynewfile.txt
touch $FILE
if [[ ! -f $FILE ]]
then
echo $FILE not exists
else
N=$(find . ! -name . -prune -name $FILE -mtime +2|wc -l)
if [[ $N -eq 0 ]]
then
echo $FILE is updated
else
echo $FILE is older
fi
fi

and the result is?

mynewfile.txt not exists

It's strange... check your procedure

@franzpizzo

hey, that script worked perfectly fine...plz ignore my previous msg.
Thanks a ton.
Need one more favour.. can u plz explain me that what does this line is doing ,it wud be good if u can explain in detail.
N=$(find . ! -name . -prune -name $FILE -mtime +2|wc -l).

Thanks.

The command

find . ! -name . -prune -name $FILE -mtime +2

search in the current directory (the first parameter "." ) and not in the subdir (the second block of parameters "! -name . -prune") a file named $FILE ("-name $FILE") older than 2 days (-mtime +2)

Good coding

and why we are using wc -l in the end. is it really necessary...

The wc -l counts the number of lines returned by the find command - i.e. how many files it found. The script logic uses this value (if it finds zero files, then it was updated less than 2 days ago), so yes, you need it.

EDIT: Normally you could just check the return status of the command, but (iirc) find will only return failure if it can't parse its parameters or it can't locate the path you specified.

great...thanx...
bro..i have one more issue..what if i want to pass file name as command line argument..in that case i shud remove wc -l or is there sm thing else...jus last favour:)

thankxxx

If you still have gnudate on your system it offers the -r option:

$ date -r myfile.txt +%s
1364355039

To test if file exists and is younger than two days you could use:

FILE="$1"

if [ -n "$(find "$FILE" -mtime -2)" ]
then
    echo file is young
else
    echo file does not exist or is old
fi