Skip to content

Commit

Permalink
added options
Browse files Browse the repository at this point in the history
  • Loading branch information
AshKsh committed Aug 1, 2019
1 parent cb35a51 commit caa80f6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Command line tool to convert Gregorian calendar dates to Persian calendar dates.
# Getting Started
Pipe Gregorian calendar dates in `YYYY-MM-DD` format to `to_persian_date` to get that date in Persian calendar.

## example
## examples
If you have put `to_persian_date` somewhere in your path:
```shell
echo "2019-07-26" | to_persian_date #1398-05-04
Expand All @@ -13,6 +13,14 @@ Or if it is not in your path:
```shell
echo "2019-07-26" | /path/to/to_persian_date #1398-05-04
```
To use a different output delimiter:
```shell
echo "2019-07-26" | to_persian_date -D / #1398/05/04
```
To use a different input delimiter:
```shell
echo "2019/07/26" | to_persian_date -d / #1398-05-04
```

# License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
Expand Down
87 changes: 71 additions & 16 deletions to_persian_date
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ d2j() {
if [[ $k -le 185 ]]; then
local jm=$((1 + k / 31))
local jd=$((k % 31 + 1))
local jmd=$(zero_pad_md $jm $jd)
echo "$jy-$jmd"

echo "$(format_output $jy $jm $jd)"
return 0
else
k=$((k - 186))
Expand All @@ -115,26 +115,81 @@ d2j() {

local jm=$((7 + k / 30))
local jd=$((k % 30 + 1))
local jmd=$(zero_pad_md $jm $jd)

echo "$jy-$jmd"
echo "$(format_output $jy $jm $jd)"
}

zero_pad_md() {
local jm=$1
local jd=$2
zero_pad() {
local num=$1

[[ $jm -lt 10 ]] && jm="0$jm"
[[ $jd -lt 10 ]] && jd="0$jd"
[[ $num -lt 10 ]] && num="0$num"

echo "$jm-$jd"
echo "$num"
}

read input_date
format_output() {
local jy=$1
local jm=$(zero_pad $2)
local jd=$(zero_pad $3)
local odel=${OPTION_ODEL:--}

echo "$jy$odel$jm$odel$jd"
}

parse_input() {
local input_date=$1
local idel=${OPTION_IDEL:--}
local y=${input_date%%$idel*}
local m=${input_date%$idel*}
m=${m#*$idel}
local d=${input_date##*$idel}

echo "$((10#$y)) $((10#$m)) $((10#$d))"
}

print_help() {
echo "Usage: to_persian_date [d idelim] [D odelim]"
echo " Convert Gregorian calendar date to Persian calendar date."
echo ""
echo " Reads a Gregorian calendar date from standard input and prints "
echo " its corresponding Persian calendar date to standard output."
echo ""
echo " Options:"
echo " -h display this help and exit"
echo " -d idelim use IDELIM instead of - for date delimiter"
echo " -D Odelim use ODELIM instead of - for date delimiter"
}

main() {
local input_data

while getopts ":hd:D:" opt; do
case ${opt} in
h)
print_help
exit 0
;;
d)
OPTION_IDEL=$OPTARG
;;
D)
OPTION_ODEL=$OPTARG
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))

read input_data
to_jalaali $(parse_input $input_data)
}

y=${input_date%%-*}
m=${input_date%-*}
m=${m#*-}
d=${input_date##*-}
main "$@"

to_jalaali $y $m $d

0 comments on commit caa80f6

Please sign in to comment.