1. 程式人生 > >Python-波斯日曆轉換(日期轉換)

Python-波斯日曆轉換(日期轉換)

最近比較忙,也有可能是我自己比較懶… 來更新一篇吧,之前說要更新scrapy的文章,但是沒有來得及準備,但是, 恰好碰到一個比較有意思的東西,波斯日曆與公曆的轉換,有人咋眼一看可能一看到波斯日曆就矇蔽了,什麼鬼東西。簡單的科普下吧。

  • 波斯日曆:波斯歷又被稱為伊朗歷是根據太陽的週期運轉而劃分月份的,每年的第一天始於春分。它是阿富汗和伊朗的官方日曆。波斯日曆有6個月31天和5個月30天之分,最後一個月是29天還是30天取決於這一年是否是閏年。

最近,遇到這樣一個網站,一看瞬間懵逼了,來給大家欣賞下…(沒錯紅框裡面的就是日期!!!)要抓取裡面的內容,抓取的話就不介紹了,很簡單,隨便get下就能得到資料,但是這個時間!!! image.png

百度一下,還是有人搞這些東西的,不然我真的不知道該怎麼辦了。 image.png

這裡面有個calcPersian() 的方法,我們就來用python 改寫然後實現它就OK了。 image.png

怎麼寫的就不詳細說明啦,還是比較耗時,直接上程式碼吧,寫的可能不是很乾淨利索,勿噴…

# -*- coding: utf-8 -*-
import math

PERSIAN_EPOCH = 1948320.5
GREGORIAN_EPOCH = 1721425.5

class caculate_Julian:

    def mod(self, a, b):

        return (a - b * math.floor(a / b))

    def persian_to_jd(self, year, month, day):
        epbase = year - 474
        epyear = 474 + self.mod(epbase, 2820)
        if month > 7:
            month = (month - 1) * 30 + 6
        else:
            month = (month - 1) * 31
        return day + month + math.floor(((epyear * 682) - 110) / 2816) +(epyear - 1) * 365 + math.floor(epbase / 2820) * 1029983 +(PERSIAN_EPOCH - 1)

    def leap_gregorian(self, year):
        if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
            return True
        else:
            return False


    def gregorian_to_jd(self, year, month, day):
        if month <= 2:
            v = 0
        elif self.leap_gregorian(year):
            v = -1
        else:
            v = -2
        return (GREGORIAN_EPOCH - 1) +(365 * (year - 1)) +math.floor((year - 1) / 4) +(-math.floor((year - 1) / 100)) + math.floor((year - 1) / 400) + math.floor((((367 * month) - 362) / 12) +v +day)

    def jd_to_gregorian(self, jd):
        wjd = math.floor(jd - 0.5) + 0.5
        depoch = int(wjd - GREGORIAN_EPOCH)
        quadricent = math.floor(depoch / 146097)
        dqc = self.mod(depoch, 146097)
        cent = math.floor(dqc / 36524)
        dcent = self.mod(dqc, 36524)
        quad = math.floor(dcent / 1461)
        dquad = self.mod(dcent, 1461)
        yindex = math.floor(dquad / 365)
        year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex
        if not (cent==4 or yindex==4):
            year+=1
        yearday = int(wjd - self.gregorian_to_jd(year, 1, 1))
        if wjd < self.gregorian_to_jd(year, 3, 1):
            leapadj = 0
        elif self.leap_gregorian(year):
            leapadj = 1
        else:
            leapadj = 2
        month = math.floor((((yearday + leapadj) * 12) + 373) / 367)
        day = int((wjd - self.gregorian_to_jd(year, month, 1)) + 1)
        return (year, month, day)

    def jhms(self, j):
        j += 0.5
        ij = ((j - math.floor(j)) * 86400.0) + 0.5
        return (math.floor(ij/3600), math.floor((ij/60)%60), math.floor(ij%60))

    def calcJulian(self, date):
        date_list = date.split('-')
        year = int(date_list[0])
        month = int(date_list[1])
        day = int(date_list[2])
        j = float(self.persian_to_jd(year, month, day))
        date = self.jd_to_gregorian(j)
        year_julian = date[0]
        month_julian = str(date[1]).zfill(2)
        day_julian = str(date[2]).zfill(2)
        return '%s-%s-%s'%(year_julian, month_julian, day_julian)


if __name__ == '__main__':
    caculate_date = caculate_Julian()

    print(caculate_date.calcJulian('1397-05-02'))

簡單來計算個 1397-05-02, 結果: image.png 沒錯就是今天(2018-07-24)。

#總結:

  • 不知道有麼有可以直接拿來用的包,肯定有,但是不知道是哪個,有知道的大神,方便留言告知下。