Time calculation

Hi Gurus,

I need to get one hour before time is yyyymmddhh format.
ex.

date +"%Y%m%d%H"

gives 2017052814 but I need 2017052813

Thankx

Had you posted the version info of your OS and date command, our proposals might be more focussed... did you try

date +"%Y%m%d%H" -d"-1hour"
2017052809

SunOS narandb 5.10 Generic_150400-30 sun4v sparc sun4v

below not work

date +"%Y%m%d%H" -d"-1hour"

Solaris date command does not work as RudiC showed you for another flavor of that command.
Solaris needs a workaround - usually perl, which Solaris supports and has with a normal install.

hour()
{
    perl -e '
    use POSIX qw(strftime);
    my $now = time - 3600;   # one hour is 3600 seconds
    my $now_string = strftime "%Y%m%d%H", localtime($now);
    print $now_string, "\n";
    '
}

#usage:
datestr=$( hour )
echo $datestr
1 Like

Perhaps a one-liner:

perl -MPOSIX -le 'print strftime "%Y%m%d%H", localtime(time - 3600)'

aia - correct and simpler. Not necessarily good for someone obviously new to perl to get what is going on.

I often post 'long' versions of something so that each step is clear. Your example is completely correct, however.

And if the OP continues on Solaris this kind of thing with perl becomes more important.

2 Likes

Hi jim mcnamara,

I can appreciate your post for what it is. My suggestion is presented without the intent to diminish your effort. There's nothing wrong with 'long' steps.