Linuxshell to convert the solar calendar to the lunar calendar

Source: Internet
Author: User
Tags ibase
Linuxshell is nothing to do if you want to use shell in Linux to write a script for converting the Gregorian calendar to the lunar calendar, which is finally completed in about a week. Now let's share it with you. The implementation principle of this script is the look-up table method (because there is an error in the formula). based on the lunar new year as the benchmark... in Linux shell, there is nothing to do with the conversion of the Gregorian calendar to the lunar calendar. I want to use shell to write a script for the conversion of the Gregorian calendar to the lunar calendar in Linux, and it will take about a week to complete it. Now let's share it with you. The principle of this script is the look-up table method (because there is an error in the formula). based on the lunar new year as the benchmark, two different Chinese calendars before and after the Lunar New Year are calculated. Before writing this script, you want to add the Gregorian calendar and lunar date to the Linux Terminal command prompt. There is Lunar Software in Ubuntu to get the Lunar date, but there is no similar software in Fedora or CentOS, so I want to implement one by myself, but I write it in other languages on the Internet, it is unnecessary to write more data. So I want to use shell to write one. Function: converts a calendar date to a calendar date. Time range: 1901 ~ 2099, the time corresponding to the calendar year is 4598 ~ 4796 parameter format (the default value of none parameter is the current system date): yyyymmdd for example, January 1, January 1, 2013: $./lunar. sh 20130101 4709-11-20 complete data: http://up.2cto.com/2013/0716/20130716104840753.rar File in the package: main script of lunar. sh, which is used to implement the description and precautions of the datebases lunar metadata change. log change log readme script. the main script lunar. sh code is as follows :#! /Bin/sh DATE = $ @ ["$ DATE" = ""] & DATE = $ (date + % Y % m % d) date_year = $ (echo $ DATE | sed's/^ \(. \ {4 \}\). */\ 1/') date_month = $ (echo $ DATE | sed's /. *\(.. \).. $/\ 1/') date_day = $ (echo $ DATE | sed's /. *\(.. \) $/\ 1/') date_days = $ (date-d $ DATE + % j) lunar_year = $ (sed/$ date_year /! D databases | sed's/^ \ (... \). */\ 1/') lunar_year_data = $ (sed/$ date_year /! D databases | sed's /. *\\(. * \)/\ 1/') lunar_year_data_bin = $ (echo "ibase = 16; obase = 2; $ lunar_year_data" | bc | sed-e: a-e's/^. \ {1, 23 \} $/0 &/; ta ') new_year_month_bin = $ (echo $ lunar_year_data_bin | sed-e's/^. \ {17 \}\(. \ {2 \}\). */\ 1/') new_year_month = $ (echo "ibase = 2; $ new_year_month_bin" | bc | sed-e: a-e's/^. \ {1, 1 \} $/0 &/; ta ') new_year_day_bin = $ (echo $ lunar_year_data_bin | sed-e's /. *\(. \ {5 \} \) $/\ 1/') new_year_d Ay = $ (echo "ibase = 2; $ new_year_day_bin" | bc | sed-e: a-e's/^. \ {1, 1 \} $/0 &/; ta ') 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 = 0 if ["$ lunar_days"-le "0"]; then befor_or_after = 1 date_year = $ ($ date_year-1) lunar_year = $ (sed/$ date_year /! D databases | sed's/^ \ (... \). */\ 1/') lunar_year_data = $ (sed/$ date_year /! D databases | sed's /. *\\(. * \)/\ 1/') lunar_year_data_bin = $ (echo "ibase = 16; obase = 2; $ lunar_year_data" | bc | sed-e: a-e's/^. \ {1, 23 \} $/0 &/; ta ') fi lunar_leap_month_bin = $ (echo $ lunar_year_data_bin | sed-e's/^ \(. \ {4 \}\). */\ 1/') lunar_leap_month = $ (echo "ibase = 2; $ response" | bc) lunar_month_all_bin = $ (echo $ lunar_year_data_bin | sed-e's/^. \ {4 \}\(. \ {13 \}\). */\ 1/') ["$ lunar_leap_month" = "0"] & lunar_month_all_bin = $ (echo $ lunar_year_data_bin | sed-e's/^. \ {4 \}\(. \ {12 \}\). */\ 1/') lunar_month_all = $ (echo $ lunar_month_all_bin | sed-e's/0/29 \/g' | sed-e's/1/30 \/g ') if ["$ befor_or_after" = "0"]; then lunar_month = 1 lunar_day = $ lunar_days for I in $ lunar_month_all do if ["$ lunar_day"-gt "$ I"]; then lunar_day = $ ($ lunar_day-$ I) lunar_month = $ ($ lunar_month + 1) fi doneelse lunar_month = 12 lunar_day = $ (-$ lunar_days )) lunar_month_all_bin = $ (echo $ lunar_month_all_bin | rev) lunar_month_all = $ (echo $ lunar_month_all_bin | sed-e's/0/29 \/g' | sed-e's/1/30 \/g ') for I in $ lunar_month_all do if ["$ lunar_day"-gt "$ I"]; then lunar_day =$ ($ lunar_day-$ I )) lunar_month = $ ($ lunar_month-1) else lunar_day = $ ($ I-$ lunar_day) break fi donefi if ["$ lunar_leap_month" = "0"]; then echo $ lunar_year-$ lunar_month-$ lunar_dayelse if ["$ lunar_leap_month"-ge "$ lunar_month"]; then echo $ lunar_year-$ lunar_month-$ lunar_day elif ["$ befor_or_after" = "0"]; then if ["$ ($ lunar_leap_month + 1 )) "=" $ lunar_month "]; then lunar_month = $ ($ lunar_month-1 )) echo $ lunar_year-* $ lunar_month-$ lunar_day else lunar_month = $ ($ lunar_month-1 )) echo $ lunar_year-$ lunar_month-$ lunar_day fi else echo $ lunar_year-$ lunar_month-$ lunar_day fifi
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.