-1

I am trying the below command in Solaris. I am trying all this in bash

START_DT="$(date --date='2013-04-01' +'%Y-%m-%d')"

and its giving me the error

-bash-3.2$ START_DT="$(date --date='2013-04-01' +'%Y-%m-%d')"
date: illegal option -- date=2013-04-01
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

-bash-3.2$ START_DT=$(date -d '2013-04-01' +%Y-%m-%d)
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

When I try the same in RedHat it works.

$ START_DT="$(date --date='2013-04-01' +'%Y-%m-%d')"
$ echo $START_DT
'2013-04-01'

In the very same way certain arithmetic operation doesn't work. By Arithmetic operations I mean adding 10 days to a give date etc.

I have tried multiple references - 1, 2, 3 but nothing seems to work

But all this works on Redhat. May be I have a very old version of Solaris and I have to use only this server for this problem statement

Cyrus
  • 69,405
  • 13
  • 65
  • 117
Rahul
  • 179
  • 2
  • 4
  • 15

1 Answers1

0

I don't ahve a Solaris 10 box to test with, but it should come with perl:

perl -MTime::Piece -sle '
    ($fmt = shift @ARGV) =~ s/^\+//;
    print Time::Piece->strptime($date, $datefmt)->strftime($fmt)
' -- -date='2017-03-15' -datefmt='%Y-%m-%d' '+%d/%m/%y'
15/03/17

With the Time::Piece::strptime function, you need to specify the datetime format of the datetime string.

And you can wrap that up in a bash function like:

mydate () {
    local OPTIND OPTARG date datefmt fmt opt
    while getopts ":d:f:" opt; do
        case $opt in
            d) date=$OPTARG ;;
            f) datefmt=$OPTARG ;;
            :) echo "error: missing argument for -$OPTARG; return 1 ;;
            *) echo "error: unknown argument: -$OPTARG; return 1 ;;
        esac
    done
    shift $((OPTIND-1))
    if [[ -z "$date" ]]; then
        command date "$@"
    elif [[ -z "$datefmt" ]]; then
        echo "error: missing -f date format for input date $date"
        return 1
    else
        if [[ -z "$1" ]]; then
            fmt="%c"
        else
            fmt=${1#+}
        fi
        perl -MTime::Piece -sle 'print Time::Piece->strptime($d, $df)->strftime($f)' -- \
            -d="$date" \
            -df="$datefmt" \
            -f="$fmt"
    fi
}
glenn jackman
  • 207,528
  • 33
  • 187
  • 305