shell实现给定一个日期得到具体天数
Question
Given a specific date, write a function to return the day of the year for that date. The signature of the function is
int dayOfYear(int year, int month, int day)
For example, given input of year=2016, month=1, day=3. The function should return 3 as the date 2016-01-03 is the 3rd day of the year 2016.
Another example, given input of year=2016, month=2, day=1. The function should return 32 as the date 2016-02-01 is the 32nd day of the year 2016.
Notes:
Please write your own algorithm. Do NOT use any class like Calendar in Java.
Please give a full implementation. Do NOT write pseudo code.
Please include your own logic of determining a leap year.
You are free to choose any programming language, not limited to Java, C# or C++. Please tell us the programming language you used in your answer.
shell 代码
#!/bin/bash
# Harlen
# 2022/03/22
# 此脚本功能为用户输入年月日,计算出这个日期是当年的多少天
# 获取用户输入date
while :
do
read -p "请输入年份:" Y
# 判断号数输入是否合法,号数输入必须是非空整数且>0!!
if [ -n $Y ] && [[ $Y =~ ^[0-9]+$ ]] && [ $Y -gt 0 ];then
break
else
echo -e "\033[31m输入错误请重新输入 \033[0m"