Equivalent Date from Gnu to Solaris

equivalent date from GNU to solaris

Tcl has a good free-form date scanner, if you have Tcl installed (try which tclsh). A shell function:

tcldate() {
d=${1:-now} # the date string
f=${2:-%c} # the output format
echo "puts [clock format [clock scan {$d}] -format {$f}]" | tclsh
}

In action on an ancient Solaris 8 box with bash 2.03 and tcl 8.3.3

$ tcldate
Tue Jul 23 13:27:17 2013
$ i=4
$ tcldate "$i days"
Sat Jul 27 13:27:34 2013
$ tcldate "$i days" "%Y-%m-%d"
2013-07-27
$ tcldate "20130101 + $i days" "%Y-%m-%d"
2013-01-05

This even handles daylight savings transitions:

$ tcldate "2014-03-09 00:30 + 1 hour" "%D %T %Z"
03/09/14 01:30:00 EST
$ tcldate "2014-03-09 00:30 + 2 hour" "%D %T %Z"
03/09/14 03:30:00 EDT
$ tcldate "2013-11-03 00:30 + 1 hour" "%D %T %Z"
11/03/13 01:30:00 EDT
$ tcldate "2013-11-03 00:30 + 2 hour" "%D %T %Z"
11/03/13 01:30:00 EST

YYYYMMDDHH date manipulation in shell script in Unix

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

equivalent to hwclock in Solaris?

On Solaris the hardware and system clock are in sync and are both set to UTC (~GMT). You can use the "date -u" command to display that clock.

YYYY-MM-DD format date in shell script

In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date).

As such:

# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1

# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1

# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal

In bash (<4.2):

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')

# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')

# print current date directly
echo $(date '+%Y-%m-%d')

Other available date formats can be viewed from the date man pages (for external non-bash specific command):

man date

Convert date formats in bash

#since this was yesterday
date -dyesterday +%Y%m%d

#more precise, and more recommended
date -d'27 JUN 2011' +%Y%m%d

#assuming this is similar to yesterdays `date` question from you
#http://stackoverflow.com/q/6497525/638649
date -d'last-monday' +%Y%m%d

#going on @seth's comment you could do this
DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d

#or a method to read it from stdin
read -p " Get date >> " DATE; printf " AS YYYYMMDD format >> %s" `date
-d"$DATE" +%Y%m%d`

#which then outputs the following:
#Get date >> 27 june 2011
#AS YYYYMMDD format >> 20110627

#if you really want to use awk
echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash

#note | bash just redirects awk's output to the shell to be executed
#FS is field separator, in this case you can use $0 to print the line
#But this is useful if you have more than one date on a line

More on Dates

note this only works on GNU date

I have read that:

Solaris version of date, which is unable
to support -d can be resolve with
replacing sunfreeware.com version of
date



Related Topics



Leave a reply



Submit