Convert string (YYYYMMDD) format to date in Sun OS


 
Thread Tools Search this Thread
Operating Systems Solaris Convert string (YYYYMMDD) format to date in Sun OS
# 8  
Old 03-27-2018
Try a Python approach through bash.
Code:
#!/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.
Code:
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...)

Last edited by wisecracker; 03-27-2018 at 06:37 PM.. Reason: See above.
# 9  
Old 03-28-2018
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.
Code:
#!/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
# 10  
Old 03-29-2018
Quote:
Originally Posted by apmcd47
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.
Code:
#!/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:
Code:
$ zeller.sh 2017 3 25
6
$ zeller.sh 2017 3 1
3

# 11  
Old 03-29-2018
Quote:
Originally Posted by vgersh99
good idea, but...somehow the zeller function doesn't work correctly:
Code:
$ 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?
# 12  
Old 03-29-2018
Quote:
Originally Posted by Don Cragun
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: usage: ... (1 Reply)
Discussion started by: SK123
1 Replies

2. Shell Programming and Scripting

Convert string into date format

Hi, I am getting the below string as a input for date. 12/03/2013 11:02 AM I want to change this date as 03-DEC-2013 11:02 AM. Could you please help on this. Thanks Chelladurai (4 Replies)
Discussion started by: ckchelladurai
4 Replies

3. Shell Programming and Scripting

Date after 5 days from current date in YYYYMMDD format

Hello Experts, How do i get date after 5 days from current date in YYYYMMDD format? How do you compare date in YYYYMMDD format? Thanks (8 Replies)
Discussion started by: needyourhelp10
8 Replies

4. Solaris

Date after 5 dates in YYYYMMDD format

Hi Experts, How to get date 5 days after current date in YYYYMMDD format? How do we compare date in YYYYMMDD format? Thanks (1 Reply)
Discussion started by: needyourhelp10
1 Replies

5. Shell Programming and Scripting

PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All, I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal. The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy. Any ideas to do this? Thanks a lot for the... (9 Replies)
Discussion started by: guruparan18
9 Replies

6. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

7. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

8. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

9. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies

10. UNIX for Dummies Questions & Answers

get yesterday date in yyyymmdd format

I would like to know how I could get a yesterday date in yyyymmdd e.g. today is 20011109, and I would like to get 20011108. Thank you!:confused: (2 Replies)
Discussion started by: hk_newbie
2 Replies
Login or Register to Ask a Question