Hi ,
we have a string with yyyymmdd
format .
how to know which date it is ?
example:
20120712-->sunday
20150228-->saturday
20140431-->invalid
please suggest commands which work on below
os : SunOS 5.10
shell: bash shell
Thanks,
Srinath.
Hi ,
we have a string with yyyymmdd
format .
how to know which date it is ?
example:
20120712-->sunday
20150228-->saturday
20140431-->invalid
please suggest commands which work on below
os : SunOS 5.10
shell: bash shell
Thanks,
Srinath.
A simple reading of the "date" manpage tells you this:
Today is 20160901, so
$ date +%A -d 20160901
Thursday
Invalid?
$ date +%A -d 20160431
date: invalid date `20160431'
-d is not working in sun OS
what version of Solaris?
os : SunOS 5.10
shell: bash shell
Hi.
$ gdate +%A -d 20160901
Thursday
$ gdate +%A -d 20160431
gdate: invalid date '20160431'
On a system like:
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution : Solaris 11.3 X86
gdate date (GNU coreutils) 8.16
And gdate
residing in:
$ which gdate
/usr/bin/gdate
Best wishes ... cheers, drl
Many older versions of Solaris do not have GNU date available.
For systems without it, I've written this Perl script which implements parts of GNU date's -d feature.
If you don't have GNU date, and don't have Perl, and don't have a very new KSH, you may be low on options.
But most versions of Solaris 10 should, unless they were specifically removed.
I believe that installing some GNU utilities with leading g
on the utility names is an installation option on Solaris 10 systems. When I was using Solaris 10 systems, most of the systems at my location were installed without that option.
Except for the month in which the cal
utility thinks the calendar switched from Julian dates to Gregorian dates (September 1752 in Great Britain and her colonies at that time), I think the following will do what you want on Solaris 10 systems when using bash
, ksh
, or /usr/xpg4/bin/sh
as your shell for calendar years 1 through 9999:
#!/bin/bash
#weekday=("sunday" "monday" "tuesday" "wednesday" "thursday" "friday" "saturday")
for ymd in "$@"
do year=${ymd%[0-9][0-9][0-9][0-9]}
month=${ymd#$year}
month=${month%[0-9][0-9]}
day=${ymd#$year$month}
sday=${day#0}
# printf 'Day:%s, Month:%s, Year:%s\n' "$day" "$month" "$year"
printf '%s-->' "$ymd"
if [ "${day#[0-9][0-9]}" != "" ] || [ $day = "00" ] || [ $sday -gt 31 ]
then echo 'invalid day'
continue
fi
cal "$month" "$year" 2>/dev/null | {
read
read
read line
set -- $line
if [ $# -eq 0 ]
then echo invalid
continue
fi
dow=$(( (sday + 6 - $#) % 7 ))
if [ $sday -gt 28 ]
then weeks=$(( (sday - $#) / 7 ))
while [ $weeks -ge 0 ]
do read line
weeks=$((weeks - 1))
done
set -- $line
if [ $dow -ge $# ]
then echo 'invalid -- too many days for that month'
continue
fi
fi
case $dow in
(0) echo sunday;;
(1) echo monday;;
(2) echo tuesday;;
(3) echo wednesday;;
(4) echo thursday;;
(5) echo friday;;
(6) echo saturday;;
esac
#echo ${weekday[$dow]}
}
done
If you invoke this script with the command:
./scriptname 20120712 20150228 20150229 20160228 20160229 20140431 19501206 \
19660501 20160901 x10101 17760704 99991231 10101 20160500 20160832
where scriptname is the name of your script, it produces the output:
20120712-->thursday
20150228-->saturday
20150229-->invalid -- too many days for that month
20160228-->sunday
20160229-->monday
20140431-->invalid -- too many days for that month
19501206-->wednesday
19660501-->sunday
20160901-->thursday
x10101-->invalid
17760704-->thursday
99991231-->friday
10101-->saturday
20160500-->invalid day
20160832-->invalid day
If your shell supports indexed arrays (which I think bash
and ksh
on Solaris 10 do, but I don't have access to them for testing), you could uncomment the lines in red and comment out the case
statement shown in bold text and get the same results.
If you need to process dates in the month when the calendar changed from Julian dates to Gregorian dates, I will leave it as an exercise for the reader to fix the code for this corner case. Note that the output of cal 9 1752
on my system in the C Locale is:
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Hi,
bash-3.2$ gdate +%A -d 20150616
bash: gdate: command not found
bash-3.2$ which gdate
no gdate in /usr/bin /bin
bash-3.2$ uname -a
SunOS pfdbfd01 5.10 Generic_150400-34 sun4v sparc sun4v
bash-3.2$ date -d "20150616" +"%Y%m%d"
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
I am getting above error ...
Thanks,
Srinadh
Hi Srinadh,
Try:
/usr/[gG][nN][uU]/bin/date -d 20150616 "+%A"
If that doesn't work either, I still think the code I suggested in post #9 in this thread should work for you. Have you tried the code I suggested there?