Skip to content

Instantly share code, notes, and snippets.

@Teraflopst
Last active March 27, 2019 13:26
Show Gist options
  • Save Teraflopst/b99c1c578458863f7b99296a6b29ed14 to your computer and use it in GitHub Desktop.
Save Teraflopst/b99c1c578458863f7b99296a6b29ed14 to your computer and use it in GitHub Desktop.
Year Progress 一年时间进度
# 判断闰年平年
cal_days_in_year() {
year=$1
if (( !(year % 4) && ( year % 100 || !(year % 400) ) ))
then
echo 366
else
echo 365
fi
}
# 计算月份的总天数
cal_days_in_month() {
c_year=$1
c_month=$2
c_day=0;
case $c_month in
1|3|5|7|8|10|12)
c_day=31;;
4|6|9|11)
c_day=30;;
2)
if (( !(c_year % 4) && ( c_year % 100 || !(c_year % 400) ) ))
then
c_day=29
else
c_day=28
fi
;;
esac
echo ${c_day}
}
# 四舍五入
round() {
printf "%.0f" "$1"
}
current_year=$(date +%-Y)
current_month=$(date '+%-m')
current_day=$(date '+%-d')
current_hour=$(date '+%-H')
current_min=$(date '+%-M')
mins_past_of_current_day=`expr $current_hour \* 60 + $current_min`
days_past_of_current_year=$(date '+%-j')
days_of_current_month=$(cal_days_in_month $current_year $current_month)
days_of_current_year=$(cal_days_in_year $current_year)
echo "年:"$current_year
echo "月:"$current_month
echo "日:"$current_day
echo "时:"$current_hour
echo "分:"$current_min
echo ""
echo "今天已过分钟:"$mins_past_of_current_day
echo "今月总共天数:"$days_of_current_month
echo "今年已过天数:"$days_past_of_current_year
echo "今年总共天数:"$days_of_current_year
day_progress=$(echo "scale=4; $mins_past_of_current_day * 100 / (60*24)" | bc)
month_progress=$(echo "scale=4; $current_day * 100 / $days_of_current_month" | bc)
year_progress=$(echo "scale=4; $(date +%j) * 100 / $days_of_current_year" | bc)
echo ""
echo "---4位小数---"
echo "今天进度(%):"$day_progress
echo "今月进度(%):"$month_progress
echo "今年进度(%):"$year_progress
echo ""
echo "---四舍五入---"
echo "今天进度(%):"$(round $day_progress)
echo "今月进度(%):"$(round $month_progress)
echo "今年进度(%):"$(round $year_progress)
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment