最近比较忙,也有可能是我自己比较懒…
来更新一篇吧,之前说要更新scrapy的文章,但是没有来得及准备,但是,
恰好碰到一个比较有意思的东西,波斯日历与公历的转换,有人咋眼一看可能一看到波斯日历就蒙蔽了,什么鬼东西。简单的科普下吧。
最近,遇到这样一个网站,一看瞬间懵逼了,来给大家欣赏下…(没错红框里面的就是日期!!!)要抓取里面的内容,抓取的话就不介绍了,很简单,随便get下就能得到数据,但是这个时间!!!
百度一下,还是有人搞这些东西的,不然我真的不知道该怎么办了。
这里面有个calcPersian() 的方法,我们就来用python 改写然后实现它就OK了。
就是这个方法,里面还涉及到其他很多function,有兴趣的朋友可以去体验下。
网站:https://cn.calcuworld.com/波斯日历
怎么写的就不详细说明啦,还是比较耗时,直接上代码吧,写的可能不是很干净利索,勿喷…
# -*- 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,
结果:
没错就是今天(2018-07-24)。
#总结: