Subtract 2 days from timestamp

Hi,

I am running on Solaris 11.3.

I have a timestamp in this format date +%Y%m%d%H%M . [ex: 201609140905]
I found some forums saying to use date --d however that didn't work.

Is there a way how I can subtract days from timestamp in that format?

Thanks

Hi.

There may be a GNU date on your system:

$ which gdate
/usr/bin/gdate
$ gdate   
Wed Sep 14 07:48:45 CDT 2016

However, you could also use ksh :

#!/usr/bin/env ksh

# @(#) s1       Demonstrate date arithmetic with ksh printf.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
# C=$HOME/bin/context && [ -f $C ] && . $C
version =o version
echo ksh - ${.sh.version}

# %Y%m%d%H%M . [ex: 201609140905]
pl " Results, today:"
printf "%(%Y%m%d%H%M)T\n" now

pl " Results, 2 days ago:"
printf "%(%Y%m%d%H%M)T\n" "2 days ago"

pl " Results, almost any time:"
arbitrary_date=201409140905
printf "%(%Y%m%d%H%M)T\n" "$arbitrary_date"

pl " Results, 2 days before almost anytime:"
printf "%(%Y%m%d%H%M)T\n" "$arbitrary_date 2 days ago"

exit 0

producing:

$ ./s1
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
version (local) 1.77
ksh - Version JM 93u 2011-02-08

-----
 Results, today:
201609140747

-----
 Results, 2 days ago:
201609120747

-----
 Results, almost any time:
201409140905

-----
 Results, 2 days before almost anytime:
201409120905

See man pages or Google search results for details.

Best wishes ... cheers, drl

can you try

$ date --date="2 day ago"
date --date...

is a non-standard GNU extension.

The current POSIX standard for the date utility is here: date

It'd be a breeze if you know perl...

Modern solaris ought to have GNU date somewhere, either as gdate or buried under /usr/gnu/bin/ or something but a lot of installers seem to have bypassed it somehow.

If you have perl, you can use my generic date script which emulates some of GNU date's -d feature.

Pitchin' in my 2 cents...

#!/usr/bin/perl

use warnings;
use Time::Local;
use Time::localtime;

use constant dt => "201609140905";
use constant ti => 2*24*60*60;

$y = substr(dt, 0, 4) - 1900;
$m = substr(dt, 4, 2) - 1;
$d = substr(dt, 6, 2);
$H = substr(dt, 8, 2);
$M = substr(dt, 10, 2);
$S = substr(dt, 12);

$epoch = timelocal($S,$M,$H,$d,$m,$y) - ti;
$tm = localtime($epoch);

printf("%04d%02d%02d",$tm->year+1900,$tm->mon+1,$tm->mday);
printf("%02d%02d\n",$tm->hour,$tm->min);

Save the file and give it a name...for example "my_date_time.pl" and run it on the cmd line as # my_date_time.pl

Solaris 11.3 has gnu date as gdate mostly by default.
By default i mean someone takes text install, it will get solaris-small-server group package which includes a lot of gnu utilites.

From AI installer or using manifest for zone install you can install solaris-minimal-server which will not include those and many others (good for zones)
Installation Group Package Content for SPARC Based Systems -
Oracle(R) Solaris 11.3 Package Group Lists

Also other gnu utilities are available with g prefix (gsed, gawk, gtar etc.)

Hope the helps
Regards
Peasant.

This kind of answer really frustrates the person asking the question. it's not constructive and doesn't really aid in solving the issue. I'm sure a lot of our questions can be answered similarly but doesn't give an immediate solution.

---------- Post updated at 12:21 PM ---------- Previous update was at 12:14 PM ----------

are you working comparing files based on a timestamp?

if so you can do something like

if [[ $i -nt $NEWER ]];

See:

Introduction to if

specifically:

[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.

The reason for such an answer is to encourage the OP to come up with a solution instead of having others write it for them...the forumites are here to help but dont expect us to spoon feed others...

which is why I answered the way I did. To point them in the correct direction rather than giving them the code to do it themselves but by just saying

x would be simple if you programmed in y. would answer just about every question on here.

all you are really doing is being snarky.

There's little to discuss until the OP gets back to this thread in any case.

I'm not trying to start a flame war here but IMO a nudge in the right direction isn't snarky :cool: