6 Replies Latest reply: Apr 20, 2012 12:50 PM by Bill Robinson RSS

Epoch time in NSH

Alexandru Ghionghios

Hi there,

 

I want to get the current standard epoch time (seconds from 1970) in a nsh script. Is there any way to do that as date +%s is not working in nsh? Or maybe you have an ideea of how can I convert something like this 2012-04-18-17-09-25 to epoch time in nsh.

 

Thanks,

 

Alex

  • 1. Re: Epoch time in NSH
    Bill Robinson

    Modload zsh/datetime

    Use strftime.

     

    You need to be using n/zsh 4.3.4

  • 2. Re: Epoch time in NSH
    Wenchi Liao

    if the datetime module is there, EPOCHSECONDS will be the current time as well:

     

    $ echo $EPOCHSECONDS

    1334773831

    $

     

    If the newer zsh isn't available, awk can generate it as well:

     

    $ echo $EPOCHSECONDS

    1334774032

    $ echo "" | awk 'END {print srand()}'

    1334774034

    $

  • 3. Re: Epoch time in NSH
    Bill Robinson

    Are you using the nsh awk or the system awk?

  • 4. Re: Epoch time in NSH
    Wenchi Liao

    It shouldn't matter. awk should have the srand() function, and that's what makes this work. With no arguments, srand() will return the current time in seconds.

     

    playing around with this a bit more. It seems gnu awk always (some versions?) implicitly seed to 1, so gawk may need srand twice:

     

    $ echo "" | gawk 'END{print srand()}'

    1

    $ echo "" | gawk 'END{print srand()}'

    1

    $ echo "" | gawk 'END{srand(); print srand()}'

    1334788707

    echo "" | gawk 'END{srand(); print srand()}'

    1334788708

     

    nsh awk only needs one... eh..

  • 5. Re: Epoch time in NSH
    Alexandru Ghionghios

    Thanks for your answers.

     

    I am using nsh's awk. I am able to do it this way, as you suggested:

     

    echo "" | awk 'END {print srand()}'

     

    Meantime, I found perl on the application server so I was able to do it using perl:

     

    perl -e "print time"

     

    I cannot update nsh to a newer version. This is the version I got: BladeLogic Network Shell 8.0.7.731. Don't have gawk ar nawk...

     

     

    Thanks a lot for your help!!

     

    Regards,

     

    Alex

  • 6. Re: Epoch time in NSH
    Bill Robinson

    You can update nsh – actually zsh.

     

    To check the version run:

    Nsh –version

    That will show like:

    $ nsh --version

    zsh 4.3.4 (i686-pc-linux-gnu)

     

    if it’s 4.0.4 you can goto the NSH/bin directory and change the nsh symlink to point to the zsh-4_3_4 binary instead of zsh.  4.3.4 is the default for bsa 8.1+ and it works fine in older versions for the most part.