0

I have a date in format "YYYYMMDDHH" and I want to add or subtract hours from it. The GNU date (date -d option) works like a charm in Linux but cant do this in non-GNU env like Solaris. Can someone please help me in how I can do this?

raj_arni
  • 959
  • 2
  • 15
  • 29
  • Are you nuts? Why -1? I am looking for the answer in shell script and that answer gives TCL or perl etc.... just want to be a hero by giving negative votes? what you will get by that? – raj_arni May 06 '14 at 21:16
  • Someone presumably voted this down because it is not a programming question and is therefore off-topic for this site. In fact, it looks like someone has already answered a very similar question on [unix.stackexchange.com](http://unix.stackexchange.com/questions/42345/date-computations-without-gnu-tools). – dg99 May 06 '14 at 22:26
  • Thanks dg99. Your answer is helpful. – raj_arni May 07 '14 at 00:38

2 Answers2

1

Just so that in case anyone lands here looking for answer to above "non-GNU date" question: following question is more appropriate with good answers: Date computations without GNU tools

Community
  • 1
  • 1
raj_arni
  • 959
  • 2
  • 15
  • 29
1

You should check if gdate is not already installed under your release of Solaris (might be in /usr/gnu/bin/date, /usr/sfw/bin/[g]date, /usr/local/bin/[g]date, /usr/csw/bin/[g]date or just /usr/bin/gdate depending on the version). If not, it should be easy to find a package containing GNU date and install it.

Anyway, here is a shell function that should just work under a stock Solaris release and that do what I believe you want:

f()
{
  echo $1 | perl -MTime::Local -nle '
  use POSIX 'strftime';
  $op='$2'*3600;
  $sec=timelocal(0,0,$4,$3,$2-1,$1) if /(\d{4})(\d{2})(\d{2})(\d{2})/;
  $sec=$sec+$op;
  print strftime "%Y%m%d%H\n", localtime($sec);'
}

$ f 2014010112 -24
2013123112
jlliagre
  • 29,783
  • 6
  • 61
  • 72