Patch-ID: bash41-003
[bash.git] / examples / functions / jdate.bash
blob9488ed902893d5ddef9a67f91b21df3ac2a68c34
1 #From: damatex@CAM.ORG (Mario Boudreault)
2 #Newsgroups: comp.unix.shell
3 #Subject: JULIAN DATE CONVERSION SUB
4 #Date: 4 Aug 1995 10:23:28 -0400
5 #Message-ID: <3vtah0$jb3@ocean.CAM.ORG>
7 #For those using shells and who want to convert dates to a julian number
8 #here is a shell script (wihtout validation) that can be used as a base
9 #program for your shell scripts.
11 #Special thanks to Ed Ferguson@ti.com who sent me the algorithm to compute
12 #that date.
15 # MODIFIED BY CHET RAMEY TO CONVERT TO bash v2 SYNTAX
18 # cnvdate - Conversion de dates en julienne et vice et versa...
20 # Par : Mario Boudreault Damatex Inc Montreal, Canada
21 # Date: 2 Aout 1995
22 # Rev.: 2 Aout 1995
24 # Usage:
25 # cvdate [-j] YYYMMDD pour convertir en nbre de jours
26 # cvdate -d {julian number} pour convertir en AAAAMMJJ
29 jul_date()
32 # Separe ANNEE, MOIS et JOUR...
34 YEAR=`echo $DATE | awk ' { print substr($0,1,4) } '`
35 MONTH=`echo $DATE | awk ' { print substr($0,5,2) } '`
36 DAY=`echo $DATE | awk ' { print substr($0,7,2) } '`
38 # Execute la formule magique...
40 A=$(( $DAY - 32075 + 1461 * ( $YEAR + 4800 - ( 14 - $MONTH ) / 12 ) \
41 / 4 + 367 * ( $MONTH - 2 + ( 14 - $MONTH ) / 12 * 12 ) / 12 - \
42 3 * ( ( $YEAR + 4900 - ( 14 - $MONTH ) / 12 ) / 100 ) / 4 ))
43 echo $A
46 day_date()
48 TEMP1=$(( $DATE + 68569 ))
49 TEMP2=$(( 4 * $TEMP1 / 146097 ))
50 TEMP1=$(( $TEMP1 - ( 146097 * $TEMP2 + 3 ) / 4 ))
51 Y=$(( 4000 * ( $TEMP1 + 1 ) / 1461001 ))
52 TEMP1=$(( $TEMP1 - 1461 * $Y / 4 + 31 ))
53 M=$(( 80 * $TEMP1 / 2447 ))
54 D=$(( $TEMP1 - 2447 * $M / 80 ))
55 TEMP1=$(( $M / 11 ))
56 M=$(( $M + 2 - 12 * $TEMP1 ))
57 Y=$(( 100 * ( $TEMP2 - 49 ) + $Y + $TEMP1 ))
58 M=`echo $M | awk ' { M=$0 ; if ( length($0) == 1 ) M="0"$0 } END { print M } '`
59 D=`echo $D | awk ' { D=$0 ; if ( length($0) == 1 ) D="0"$0 } END { print D } '`
60 echo $Y$M$D
63 # main()
65 if [ $# -eq 1 ]; then
66 DATE=$1
67 jul_date
68 elif [ "$1" = '-j' ]; then
69 DATE=$2
70 jul_date
71 elif [ "$1" = '-d' ]; then
72 DATE=$2
73 day_date
76 # Termine
78 exit 0