Linux shell实现阳历转农历


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

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

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

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

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

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

如2013年1月1日:

完整数据下载地址

http://files.cnblogs.com/snowsolf/lunar_20130711.zip

包中文件

lunar.sh 主脚本,具体实现

datebases 农历元数据

change.log 更改日志

readme 脚本说明及注意事项

主要脚本lunar.sh代码如下

#!/bin/= =  ] && DATE=$( +%Y%m%=$( $DATE | =$( $DATE | =$( $DATE | =$( -d $DATE +%=$( /$date_year/!d databases | =$( /$date_year/!d databases | =$( |bc | -e :a -e =$( $lunar_year_data_bin | -e =$( |bc | -e :a -e =$( $lunar_year_data_bin | -e =$( |bc | -e :a -e =$( -d $date_year$new_year_month$new_year_day +%=$( $date_days - $new_year_days + =
 
 [  -le  ]; ==$(($date_year-=$( /$date_year/!d databases | =$( /$date_year/!d databases | =$( |bc | -e :a -e =$( $lunar_year_data_bin | -e =$( |=$( $lunar_year_data_bin | -e  =  ] && lunar_month_all_bin=$( $lunar_year_data_bin | -e =$( $lunar_month_all_bin | -e  | -e  [  =  ];== i 
       [  -gt  ]; =$(($lunar_day -=$(($lunar_month + 
   
==$((-=$( $lunar_month_all_bin |=$( $lunar_month_all_bin | -e  | -e  i 
       [  -gt  ]; =$(($lunar_day -=$(($lunar_month - =$(($i -
   

 
 [  =  ]; 
     $lunar_year-$lunar_month-
    [  -ge  ]; 
       $lunar_year-$lunar_month- [  =  ]; 
       [  =  ];=$(($lunar_month -  $lunar_year-*$lunar_month-=$(($lunar_month -  $lunar_year-$lunar_month-
   
       $lunar_year-$lunar_month-
 

相关内容