Convert set time in seconds to solaris

I have time like 2013-04-29 08:17:58. And I need to convert it in seconds from the point in time. No perl, please, and my OS is sunny. +% s does not work. nawk 'BEGIN {print srand ()}' converts the current time to seconds, but does not convert the set time to seconds.

thanks

+4
source share
2 answers

Here is a shell function that does not 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

Please note that the return value depends on your time zone.

$ TZ=GMT d2ts 2013-04-29 08:17:58
1367223478

How it works:

  • The first line converts the parameters to a format suitable for touch(here "2013-04-29
  • 08:17:58 "->" 201304290817.58 ")
  • , , ..
  • ls,
+5

C/++ UNIX :

struct tm tm;
time_t t;

tm.tm_isdst = -1;
if (strptime("2013-04-29 08:17:58", "%Y-%m-%d %H:%M:%S", &tm) != NULL) {
    t = mktime(tm);
}
cout << "seconds since epoch: " << t;

. Opengroup strptime() .

strace/touch . , , ...

0

Source: https://habr.com/ru/post/1502775/


All Articles