Convert string (YYYYMMDD) format to date in Sun OS

Hi All

I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated.

The date command usage from the operating system we use here is as follows:

Thanks,
SK

what is the 'date' format?
Please be more specific.

1 Like

Any format is OK, because I need to find out the day after that. I am only worried about getting the day (For example: Wednesday) from the string (YYYYMMDD) I am passing.

This might be of interest or help:

Courtesy of Corona688.
EDIT:
A little bit of googling...
SunOS man pages : date (1)

1 Like

a bit verbose, but you could start here - isWed.sh :

#!/bin/bash

if [ ${#} -ne 1 ];then
   echo "invalid number of arguments : ${0} YYYYMMDD"
   exit 1
fi

d="${1}"
# wed from cal
dayOFweek=4

YEAR=${d%????}
DAY=${d#??????}
MON=${d#$YEAR}
MON=${MON%$DAY}
echo $YEAR/$MON/$DAY

dow=$(cal "${MON}" "${YEAR}" | awk -v day="${DAY}" '
  FNR==2 {split($0,daysA)}
  FNR > 2 {
    for(i=1; i <= NF; i++)
      if ( $i == day) {
        #printf("day->[%d] row->[%d]\n", $i, FNR-2)
        #printf("%d\n", (NF == 7 || FNR!=3) ? i-1 : i+(6-NF))
        printf("%d\n", (NF == 7 || FNR!=3) ? i : i+(6-NF)+1)
        exit
      }
  }
')

if [ ${dow} -eq ${dayOFweek} ]; then
   echo yes
else
   echo no
fi

isWed.sh 20180329
isWed.sh 20180328

1 Like

there was a failure in this script:

bash-3.2$ ./isWed.sh 20180328
2018/03/28
awk: syntax error near line 1
awk: bailing out near line 1
./isWed.sh: line 31: [: -eq: unary operator expected
no

---------- Post updated at 04:26 PM ---------- Previous update was at 04:09 PM ----------

Found answer from the following thread:

Thanks a lot for your help.

You could do this with perl eg:

#!/bin/perl
use POSIX;
use Time::Local;

my ($yyyy, $mm, $dd) = (
   $ARGV[0] =~ /([0-9]{4}) ([0-9]{2}) ([0-9]{2})/x
);

eval {
   print strftime("%a\n", localtime(timelocal(0,0,0,$dd,$mm - 1,$yyyy)))
};
print "ERROR: Format is YYYYMMDD\n" if $@;

Or, for a bit if fun, you could use cal and nawk like this:

Y=${1%????}
M=${1#????}
D=${M#??}
M=${M%??}
cal $M $Y | nawk '/^[A-Z]/ { split($0, dayname, " ") } dayname[1] && match($0, wd) { print dayname[int((RSTART+2)/3)] ; exit }' wd=$D

Assumption here is that each day takes three vertical text columns like this so when we find a day in the cal output we can calculate the heading it is under:

     March 2018     
Su Mo Tu We Th Fr Sa
             1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 31 

Try a Python approach through bash.

#!/bin/bash
# epoch.sh
# Usage: ./epoch.sh YYYYMMDD
EPOCH=$1
if [ "$EPOCH" == "" ]
then
	exit 1
fi
echo '#epoch.py
import time
mydate='\"$EPOCH\"'
epoch=int(time.mktime(time.strptime(mydate, "%Y%m%d")))
print(time.strftime("%A", time.localtime(epoch)))
exit()' > "$HOME"/epoch.py
DAY=$( python "$HOME"/epoch.py )
echo "$DAY"

Results OSX 10.13.3, default bash terminal calling Python 2.7.x.

Last login: Tue Mar 27 22:13:36 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./epoch.sh 20180327
Tuesday
AMIGA:amiga~/Desktop/Code/Shell> ./epoch.sh 20180328
Wednesday
AMIGA:amiga~/Desktop/Code/Shell> _

Note: There is very linited error checking so beware!
EDIT:
This is Python 2.7.10 but I think it works on Python 3.5.2 also.
(I have bent the rules on the Python print statement in 2.7.x AND print() function in 3.5.x...)

This bash-only script works with bash 3 on Solaris and bash 4 on Ubuntu.

To use it call it with yyyymmdd date strings as arguments.

#!/bin/bash

zeller() {
   year=$1 month=$2 day=$3
   (( 10#$month < 3 )) && year=$(( year - 1 ))
   F=$(( (10#$day + ((13 * ((10#$month+9) % 12) + 12)/5) + \
                (5*(year%100))/4 + (year/400) - 2*(year/100))%7 ))
   (( F<0 )) && (( F = F + 7 ))
   echo $F
}

for date in $@
do
   y=${date:0:4} m=${date:4:2} d=${date:6:2}
   dow=$(zeller $y $m $d)
   if (( dow == 3 ))
   then 
      printf "%02d/%02d/%4d (%s) is a Wednesday\n" $d $m $y $date
      # US date format: (comment above and uncomment below if in the USA)
      #printf "%20d/%02d/%4d (%s) is a Wednesday\n" $m $d $y $date
   else
      printf "%02d/%02d/%4d (%s) is NOT a Wednesday\n" $d $m $y $date
      # US date format:
      #printf "%02d/%02d/%4d (%s) is NOT a Wednesday\n" $m $d $y $date
   fi
done

The function zeller uses the Zeller algorithm to find the day of week from the given year/month/day. Result: 0 for a Sunday through to 6 for a Saturday.

Andrew

good idea, but...somehow the zeller function doesn't work correctly:

$ zeller.sh 2017 3 25
6
$ zeller.sh 2017 3 1
3

The results you are showing seem to indicate that it is working perfectly? What days of the week were you expecting for the 1st and 25th of March last year?

My bad - was validating against 2018.
Thanks Don!