Git Product home page Git Product logo

isee15 / lunar-solar-calendar-converter Goto Github PK

View Code? Open in Web Editor NEW
630.0 31.0 240.0 4.93 MB

公历(阳历) 农历(阴历)转换,支持时间段从1900-2100 如果需要更长的时间段,利用generate.htm生成的数据即可。 支持各种编程语言 C#,java,Objective-C,php,Python,javascript(nodejs),C/C++,ruby,swift,golang等 支持Mac,Windows,Android,WP多种平台

License: MIT License

C# 0.41% C 0.24% HTML 88.93% JavaScript 7.73% Objective-C 0.31% PHP 0.26% Python 0.35% Ruby 0.22% Swift 0.56% Java 0.34% Batchfile 0.01% Go 0.27% Dart 0.37%
lunar solar javascript swift java php python ruby

lunar-solar-calendar-converter's Introduction

Lunar Solar Calendar Converter

公历(阳历) 农历(阴历)转换,支持时间段从1900-2100
如果需要更长的时间段,利用generate.htm生成的数据即可。
支持各种编程语言 C#,java,Objective-C,php,Python,javascript(nodejs),C/C++,ruby,swift,golang等
支持Mac,Windows,Android,WP多种平台

数据验证

1.用io.js(nodejs)写了一个httpserver,各种语言可以通过下面的http接口验证不同实现的数据一致性。
在javascript目录下node check.js启动

2.http://localhost:1337/?src=2015,1,15 (公历转农历,返回2014,11,25,0) 或者 
  http://localhost:1337/?src=2014,11,25,0 (农历转公历,返回2015,1,15)

3.比如在C#版本中,Check.cs 实现了C#与nodejs的数据比对

基本原理

  • 查表。有2个数据表,对于每一年,一张表存着X年正月初一对应的公历年月日,另一张表存着X年农历每个月的天数以及闰月的月份。 然后根据这两张表进行日期的偏移。
  • 原始数据通过了微软ChineseLunisolarCalendar类的比对。比对程序在C#版本中。
  • Swift由于苹果官方有NSCalendarIdentifierChinese,因此有跟Swift官方匹配的generate.swift。实际比对的结果,微软与苹果的数据有部分不一致。比如Apple 2057/9/28->2057年九月〇明显有问题。

TODO

  • 干支
  • 星座

API For CSharp or Java

/**
*农历转公历
*/
public static Solar LunarToSolar(Lunar lunar)

/**
*公历转农历
*/
public static Lunar SolarToLunar(Solar solar)

API For Objective-C

/**
*农历转公历
*/
+ (Solar *)lunarToSolar:(Lunar *)lunar;

/**
*公历转农历
*/
+ (Lunar *)solarToLunar:(Solar *)solar;

API For php

/**
*农历转公历
*/
public static function LunarToSolar($lunar)

/**
*公历转农历
*/
public static function SolarToLunar($solar)

API For python

//support pypi "pip install LunarSolarConverter"
//refer pypiDemo
/**
*农历转公历
*/
def LunarToSolar(self, lunar):

/**
*公历转农历
*/
def SolarToLunar(self, solar):

API For javascript

/**
*农历转公历
*/
this.LunarToSolar = function (lunar)

/**
*公历转农历
*/
this.SolarToLunar = function (solar)

API For C/C++

/**
*农历转公历
*/
Solar LunarToSolar(Lunar lunar);

/**
*公历转农历
*/
Lunar SolarToLunar(Solar solar);

API For ruby

/**
*农历转公历
*/
def lunar_to_solar(lunar)

/**
*公历转农历
*/
def solar_to_lunar(solar)

API For swift

/**
*农历转公历
*/
class func LunarToSolar( lunar:Lunar)->Solar

/**
*公历转农历
*/
class func SolarToLunar( solar:Solar)->Lunar

API For Go

/**
*农历转公历
*/
func  LunarToSolar(lunar Lunar) *Solar

/**
*公历转农历
*/
func SolarToLunar(solar Solar) *Lunar

API For Dart

/**
*农历转公历
*/
static lunarToSolar(Lunar lunar) 

/**
*公历转农历
*/
static solarToLunar(Solar solar)

lunar-solar-calendar-converter's People

Contributors

bryant1410 avatar huuang avatar isee15 avatar meizhitu avatar nlsun avatar renyijiu avatar ridgestd avatar sjm1992st avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lunar-solar-calendar-converter's Issues

java 测试转化后 出现很多转化错误

java 测试出现通过,测试代码如下 (Solar类 加了equals,hashCode和toString)

public static void main(String[] args) {
    int dayCount = 365 * 1;
    Calendar calendar = Calendar.getInstance();
    calendar.set(2001, Calendar.JANUARY, 1);
    for (int i = 0; i < dayCount; i++) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);


        Solar solar = new Solar();
        solar.solarYear = year;
        solar.solarMonth = month+1;
        solar.solarDay = day;

        Lunar lunar = LunarSolarConverter.SolarToLunar(solar);
        Solar solar2 = LunarSolarConverter.LunarToSolar(lunar);

        if (solar.equals(solar2)) {
            System.out.println("ok solar:" + solar + " lunar:" + lunar + " solar2:" + solar2);
        } else {
            System.out.println("---error solar:" + solar + " lunar:" + lunar + " solar2:" + solar2);
        }
    }
}

打印结果

能加到PyPi吗?

你好,我看到了你写的阴阳历转换库。
但是python版本貌似没有加到PyPI?
请问是否可以加进去呢?
谢谢。

Maybe too many exposed functions/variables?

I've read and tried to understand this repo(java version) for about a week, and I've come to that, since there are only to API for java client, why are there so many public method exposed? Maybe private static should be used?
I'd like to discuss about it in this issue, and there will be a new merge request from my fork in a few days. Stay in touch.

我花了大约一周时间阅读了java部分的部分代码,我发现,既然java的API只有两个方法暴露出来,为何有如此多的public方法?(几乎全部)。这可能有违设计模式。我在考虑应该修改部分为私有静态方法。
希望在这个issue里讨论这个问题,几天后我会推一个解决方案到我的fork上我们来看看能不能解决这个问题。

阳历2018年三月份数据转换有问题

from LunarSolarConverter.LunarSolarConverter import LunarSolarConverter, Lunar, Solar

converter = LunarSolarConverter()

solar = Solar(2018, 2, 28)
print(vars(solar))
lunar = converter.SolarToLunar(solar)
print(vars(lunar))

solar = Solar(2018, 3, 1)
print(vars(solar))
lunar = converter.SolarToLunar(solar)
print(vars(lunar))

Output:
{'solarDay': 28, 'solarMonth': 2, 'solarYear': 2018}
{'isleap': False, 'lunarDay': 13, 'lunarMonth': 1, 'lunarYear': 2018}
{'solarDay': 1, 'solarMonth': 3, 'solarYear': 2018}
{'isleap': False, 'lunarDay': 22, 'lunarMonth': 2, 'lunarYear': 2018}

Python 版本的阴历转阳历 显示结果怎么看?

converter = LunarSolarConverter()
lolar = Lunar(1998, 2, 3, isleap=True)
solar = converter.LunarToSolar(lolar)
pprint('%s-%s-%s' % (solar.solarYear, solar.solarMonth, solar.solarDay))
print("Done")

输出
' 1998.4838136092799-2.9047222222161952--0.01999999999998181'
这是阳历的几月几号呢?

Maybe too many exposed functions/variables?

I've read and tried to understand this repo(java version) for about a week, and I've come to that, since there are only to API for java client, why are there so many public method exposed?
I'd like to discuss about it in this issue, and there will be a new merge request from my fork in a few days. Stay in touch.

我花了大约一周时间阅读了java部分的部分代码,我发现,既然java的API只有两个方法暴露出来,为何有如此多的public方法?(几乎全部)。这可能有违设计模式。我在考虑应该修改部分为私有方法。
希望在这个issue里讨论这个问题,几天后我会推一个解决方案到我的fork上我们来看看能不能解决这个问题。

C版本的SolarFromInt函数有溢出,导致计算结果错误

Solar SolarFromInt(long g) {
    long y = (10000 * g + 14780) / 3652425;
    long ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
    if (ddd < 0) {
        y--;
        ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
    }
    long mi = (100 * ddd + 52) / 3060;
    long mm = (mi + 2) % 12 + 1;
    y = y + (mi + 2) / 12;
    long dd = ddd - (mi * 306 + 5) / 10 + 1;
    Solar solar = {
            .solarYear = (int) y,
            .solarMonth = (int) mm,
            .solarDay = (int) dd,
    };
    return solar;
}

函数内的第一句
long y = (10000 * g + 14780) / 3652425; 这句有溢出,
改为long y = (10000LL * g + 14780) / 3652425 正常,
测试值为农历 2014-12-6

LunarSolarConverter.class.php 计算异常

如:由公历1997-11-15 应为农历:1997-10-16

而计算为:
object(Solar)[1]
public 'solarDay' => int 15
public 'solarMonth' => int 11
public 'solarYear' => int 1997
object(Lunar)[2]
public 'isleap' => boolean false
public 'lunarDay' => int 17
public 'lunarMonth' => int 10
public 'lunarYear' => int 1997
object(Solar)[3]
public 'solarDay' => int 15
public 'solarMonth' => int 11
public 'solarYear' => int 1996
object(Lunar)[2]
public 'isleap' => boolean false
public 'lunarDay' => int 17
public 'lunarMonth' => int 10
public 'lunarYear' => int 1997

修复版本:
https://github.com/120991021/Lunar-Solar-Calendar-Converter/commit/7dc445e7ec5a0197f558869e52cb92206ffec8fd

@@ -75,7 +75,7 @@ public static function GetBitInt($data, $length, $shift)
public static function SolarToInt($y, $m, $d)
{
$m = ($m + 9) % 12;

  •    $y = intval($y - $m / 10);
    
  •    $y = intval($y) - intval($m / 10);
     return intval(365 \* $y + intval($y / 4) - intval($y / 100) + intval($y / 400) + intval(($m \* 306 + 5) / 10) + ($d - 1));
    
    }

Tried on 2017-01-04, the lunarMonth is 14.

Calendar c = Calendar.getInstance();
Solar solar = new Solar();
solar.solarYear = 2017;
solar.solarMonth = Calendar.JANUARY;
solar.solarDay = 4;
Lunar lunar = LunarSolarConverter.SolarToLunar(solar);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.