Date conversion


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Date conversion
# 8  
Old 09-01-2016
But most versions of Solaris 10 should, unless they were specifically removed.
# 9  
Old 09-02-2016
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:
Code:
#!/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:
Code:
./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:
Code:
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:
Code:
   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

# 10  
Old 09-02-2016
date conversion

Hi,

Code:
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


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 09-02-2016 at 03:31 AM..
# 11  
Old 09-02-2016
Quote:
Originally Posted by srinadhreddy27
Hi,

Code:
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

Moderator's Comments:
Mod Comment Use code tags, thanks.
Hi Srinadh,
Try:
Code:
/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?

Last edited by Don Cragun; 09-02-2016 at 03:40 AM.. Reason: Fix typo: s/"+%B"/"+%A"/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date conversion

Trying to convert dates using a Perl Script but it has to accept formats like 3 letter month, day and year like Nov 02 2010 or 1/4/11 or 21 Feb 2011 and have it convert to something like October 20, 2011. Any ideas? (2 Replies)
Discussion started by: reduxeffect81
2 Replies

2. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

3. UNIX for Dummies Questions & Answers

Date conversion in ab i

(string(8)) ((date("YYYYMMDD")) ((date("YYYY/MM/DD")) in.date_field_name)) (1 Reply)
Discussion started by: dr46014
1 Replies

4. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

5. Shell Programming and Scripting

Date conversion

Hi, I have the string YYYYMMDDHHMMSS like 20090801204150 and I need to convert it using the unix command date in the format: date "Saturday, 1 August 2009 20:40:59" All in one single Unix line if this is possible. What's the correct syntax? Steve Hagi (6 Replies)
Discussion started by: hagimeno
6 Replies

6. Shell Programming and Scripting

Date conversion

Hi I want to convert MAY 05 2005 01:15:00PM date format to 2005/05/05 01:15:00PM . CAn somebody suggest me a code ,I am new to unix shell programming. Thanks Arif (21 Replies)
Discussion started by: mab_arif16
21 Replies

7. Shell Programming and Scripting

Date Conversion

Hi, Does anyone know (in KSH, CSH, SED or AWK), how to convert date text in a file from: EX: May232008 to: 05232008 Thanks, (3 Replies)
Discussion started by: jgrant746
3 Replies

8. Shell Programming and Scripting

date conversion

file1 E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769 E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538 E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5 E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077 E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077... (14 Replies)
Discussion started by: charandevu
14 Replies

9. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

10. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies
Login or Register to Ask a Question