I have a time like 2013-04-29 08:17:58. And i need to convert it to seconds since epoch time. No perl please and my OS is solaris. +%s does not work. nawk 'BEGIN{print srand()}' converts the current time to seconds but does not convert a given time to seconds.
Thanks
Here is a shell function that doesn't require perl:
function d2ts
{
typeset d=$(echo "$@" | tr -d ':- ' | sed 's/..$/.&/')
typeset t=$(mktemp) || return -1
typeset s=$(touch -t $d $t 2>&1) || { rm $t ; return -1 ; }
[ -n "$s" ] && { rm $t ; return -1 ; }
truss -f -v 'lstat,lstat64' ls -d $t 2>&1 | nawk '/mt =/ {printf "%d\n",$10}'
rm $t
}
$ d2ts 2013-04-29 08:17:58
1367216278
Note that the returned value depends on your timezone.
$ TZ=GMT d2ts 2013-04-29 08:17:58
1367223478
How it works:
touch
(here "2013-04-29 ls
command to get the file modification time and prints it as an integerIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With