Yesterday in UNIX

Hi,
I want to set a cron job with the yesterday date in a unix server (solaris).

  • I tried TZ=CST+24 date +%Y%m%d but i had TZ=CST+24: not found
  • I tried `perl -mPOSIX -e 'print POSIX::strftime("%Y%m%d",localtime(time() - 86400)) '` it works but I'm not sure that all the clients have perl in their machines.
  • date -v and date -d don't work only -u and -a options work with date in unix:
    date: illegal option -- d
    usage: date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]

Anyone has an idea please.
Thanks

What is the poll for?

What shell did you run the TZ one in?

Perl is the only solution that will work dependably everywhere.

1 Like

Hi.

For some versions of ksh built-in command printf:

...
A %(date-format)T format can be:
use to treat an argument as a date/time string and to format the
date/time according to the date-format as defined for the
date(1) command.
...
man ksh, section printf

So

printf "%(%Y-%m-%d)T\n" "yesterday"

would print ( for today = Fri Feb 13 12:01:29 CST 2015 )

2015-02-12

For ksh 93s+

Best wishes ... cheers, drl

PS:

TZ=CST+24 date +%Y%m%d

produced

20150212

in a system like:

OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86

for both ksh and bash.

And for csh / tcsh

setenv TZ CST+24 ; date +%Y%m%d

produces

20150212
1 Like

For GNU date, what's wrong with:

date -d 'yesterday'

with you format also specified? If you lack GNU date, get a copy at a trusted source. Or, you can search here for my time utility tm2tm.c, which does the same sort of things.

1 Like

Agreed. Removed it.

Given up on Solaris, but is there no GNU-equivalent date command in /usr/sfw or /usr/xpg? ?

Hi, Scott.

My recollection is that I tried to port GNU coreutils to get GNU date for Solaris. I was not successful.

It might be useful for the community here to have someone (with more expertise that I have) do a port. Judging from the posts here, I would say for Solaris and AIX, possibly HPUX.

I never found anything in sfw in Solaris to handle date arithmetic, but it's possible I missed something -- it was years before I stumbled on sfw :slight_smile: I figured if the Sun/Oracle folks couldn't do the port, then it was probably difficult, or required a lot of extra work. The stuff in sfw seemed mostly like stand-alone items, e.g. ggrep ... cheers, drl

Seems like sunfreeware went commercial, but SPARC Solaris 10 code is here: Freeware List for SPARC and Solaris 10

coreutils-8.11-sol10-sparc-local.gz GNU Coreutils are a set of basic file, shell, and text manipulation utilities for the GNU operating system that are expected to exist on every operating system. Previously, they were offered as three individual distributions: fileutils, shellutils, and textutils - installs in /usr/local. Dependencies: libiconv, libintl, gmp, and to obtain /usr/local/lib/libgcc_s.so.1 you will need to have installed libgcc-3.4.6 or gcc-3.4.6 or higher.
The programs included are 
\[        cut        false    ls      pathchk   sha224sum  sync      unexpand
base64    date       fmt      md5sum  pinky     sha256sum  tac       uniq
basename  dd         fold     mkdir   pr        sha384sum  tail      unlink
cat       df         groups   mkfifo  printenv  sha512sum  tee       uptime
chcon     dir        head     mknod   printf    shred      test      users
chgrp     dircolors  hostid   mktemp  ptx       shuf       timeout   vdir
chmod     dirname    id       mv      pwd       sleep      touch     wc
chown     du         install  nice    readlink  sort       tr        who
chroot    echo       join     nl      rm        split      true      whoami 
cksum     env        kill     nohup   rmdir     stat       truncate  yes 
comm      expand     link     nproc   runcon    stdbuf     tsort 
cp        expr       ln       od      seq       stty       tty 
csplit    factor     logname  paste   sha1sum   sum        uname
 
 
coreutils-8.11.tar.gz Source Code. [Details]  
1 Like

Corona688: For now perl is the only solution for the setenv i'm searching an other one.

drl: Thank you, that's what i need and in this format 20150212 . I'll try

setenv TZ CST+24 ; date +%Y%m%d

in my .login Monday because my machine is at work and i'll tell you if it works for me or not.

DGPickett: It's a Solaris and not a GNU so

date -d 

doesn't work.

Well, 'date' is just a program file, you can get a Gnu date and call it gdate or by path.

I wrote tm2tm as a more task oriented tool than date, not cluttered with clock setting. It is easy to compile and use. In this case, time input is clock, delta is back one day, format is YYYY-MM-DD:

$ tm2tm -c -1d '%Y-%m-%d'
2015-02-12
$
1 Like

DGPickett: I can't change the configuration of the client's files. So i have to try other solutions.
Thanks.

You can put gnu date anywhere and call it when you need yesterday, no configuration or other system level change.

Does /usr/sfw/bin on your solaris box have gdate or date ?

And. If you cannot change anything at why are you asking for something that implies some kind of code change?
This works on solaris10 and solaris11 - We use it in lots of scripts.

#!/bin/ksh
# usage /path/to/then.pl 7 for date seven days ago
#          /path/to/then.pl 1  for yesterday
ago()
{
   perl -e ' my $delta = $ARGV[0];
             $delta*=86400;
             $delta=time - $delta;
             @t=localtime( $delta );
             printf("%02d-%02d-%d", $t[4]+1, $t[3], $t[5]+1900); ' $1
}

echo $(ago $1)

Hi,
with awk (work fine between 1901 and 2099):

awk -F/ '{($3 == 1) ? ($2 == 1) ? $0=$1-1"/12/31" : ($2 == 3) ? (($1 % 4)==0) ? $0=$1"/2/29" : $0=$1"/2/28" : ($2 == 2 || $2 == 4 || $2 == 6 || $2 == 8 || $2 == 9 || $2 == 11) ? $0=$1"/"$2-1"/31" : $0=$1"/"$2-1"/30" : $0=$1"/"$2"/"$3-1 ; printf("%04d%02d%02d\n",$1,$2,$3)}'

and example:

for i in "2016/03/01" "2015/01/01" "2015/02/01" "2015/03/01" "2015/04/01" "2015/05/01" "2015/06/01" "2015/07/01" "2015/08/01" "2015/09/01" "2015/10/01" "2015/11/01" "2015/12/01"
do
echo $i | awk -F/ '{($3 == 1) ? ($2 == 1) ? $0=$1-1"/12/31" : ($2 == 3) ? (($1 % 4)==0) ? $0=$1"/2/29" : $0=$1"/2/28" : ($2 == 2 || $2 == 4 || $2 == 6 || $2 == 8 || $2 == 9 || $2 == 11) ? $0=$1"/"$2-1"/31" : $0=$1"/"$2-1"/30" : $0=$1"/"$2"/"$3-1 ; printf("%04d%02d%02d\n",$1,$2,$3)}'
done
20160229
20141231
20150131
20150228
20150331
20150430
20150531
20150630
20150731
20150831
20150930
20151031
20151130

Regards.