Linux shell实现阳历转农历

闲来无事,想在Linux下用shell写一个阳历转农历的脚本,断断续续大概一个星期终于搞定。现在拿出来与大家分享。

本脚本实现原理是查表法(因为公式有误差);基于农历新年为基准,对农历新年前后两个不同的农历进行计算。

写这个脚本之前是想在Linux 终端命令提示符中加入阳历及农历日期。在Ubuntu中有Lunar软件可以获取农历日期,但在Fedora或CentOS中并没有类似软件,所以就想自己来实现一个,但网上用其他语言写的一大把,香港服务器,如果再写没什么必要。所以就想用shell来写一个。

功能:将具体的阳历日期转换为农历日期。

时间范围:1901~2099,对应农历年时间为4598~4796

参数格式(无参数默认为当前系统日期):yyyymmdd

如2013年1月1日:

$./lunar.sh 20130101

4709-11-20

完整数据下载地址:

包中文件:

lunar.sh 主脚本,具体实现

datebases 农历元数据

change.log 更改日志

readme 脚本说明及注意事项

主要脚本lunar.sh代码如下:

#!/bin/shDATE=$@[ = “” ] && DATE=$(date +%Y%m%d)date_year=$()date_month=$()date_day=$()date_days=$(date -d $DATE +%j)lunar_year=$()lunar_year_data=$()lunar_year_data_bin=$(|bc |)new_year_month_bin=$()new_year_month=$(|bc |)new_year_day_bin=$()new_year_day=$(|bc |)new_year_days=$(date -d $date_year$new_year_month$new_year_day +%j)lunar_days=$(expr $date_days – $new_year_days + 1)befor_or_after=-le ]; thenbefor_or_after=1date_year=$(($date_year-1))lunar_year=$()lunar_year_data=$()lunar_year_data_bin=$(|bc |)filunar_leap_month_bin=$()lunar_leap_month=$(|bc)lunar_month_all_bin=$()[ = ] && lunar_month_all_bin=$()lunar_month_all=$(|)= ];thenlunar_month=1lunar_day=$lunar_daysfor i in $lunar_month_all-gt ]; thenlunar_day=$(($lunar_day – $i))lunar_month=$(($lunar_month + 1))lunar_month=12lunar_day=$((-$lunar_days))lunar_month_all_bin=$(echo $lunar_month_all_bin |rev)lunar_month_all=$(|)for i in $lunar_month_all-gt ]; thenlunar_day=$(($lunar_day – $i))lunar_month=$(($lunar_month – 1))elselunar_day=$(($i – $lunar_day))break[ = ]; thenecho $lunar_year-$lunar_month-$lunar_day-ge ]; thenecho $lunar_year-$lunar_month-$lunar_day= ]; = ];thenlunar_month=$(($lunar_month – 1))echo $lunar_year-*$lunar_month-$lunar_dayelselunar_month=$(($lunar_month – 1))echo $lunar_year-$lunar_month-$lunar_day$lunar_year-$lunar_month-$lunar_dayfifi,网站空间,美国服务器钱财何足贵,仁义值千金。

Linux shell实现阳历转农历

相关文章:

你感兴趣的文章:

标签云: