User:GeneaBot/Code/util date
< User:GeneaBot | Code
# -*- coding: utf-8 -*-
import pywikibot
Month = [ "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
lMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
def bissextile(year):
bis = False
if year < 1582: # julian calendar
if year % 4 == 0:
bis = True
else: # gregorian calendar
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
bis = True
else:
bis = True
return bis
def testBissextile(year):
print( "bissextile(" + str(year) + ") = " + str(bissextile(year)) )
def calendar(year, month, day):
if int(year) < 1582:
return 'http://www.wikidata.org/entity/Q1985786' # calendrier julien proleptique
elif int(year) == 1582 and int(month) > 0 and int(month) < 10:
return 'http://www.wikidata.org/entity/Q1985786' # calendrier julien proleptique
elif int(year) == 1582 and int(month) == 10 and int(jour) > 0 and int(jour) < 5:
return 'http://www.wikidata.org/entity/Q1985786' # calendrier julien proleptique
else:
return 'http://www.wikidata.org/entity/Q1985727' # calendrier grégorien proleptique
#======================================================
# Fonction DateToWiki
#
# assume that value is a string as 'AAAA', 'Mmm AAAA' or 'JJ Mmm AAAA'
def DateToWiki(Value):
wValue = ""
elt = Value.split(" ")
while 1:
if '' in elt:
elt.remove('')
else:
break
#print str(len(elt)) + ":" + str(elt)
if len(elt) == 3:
day = int(elt[0])
i = 1
month = 0
while i <= 12:
mon = Month[i].lower()
if len(elt[1]) == 3:
mon = mon[:3]
if elt[1].lower() == mon:
month = i
break
i += 1
year = int(elt[2])
#print str(day) + ' ' + str(month) + ' ' + str(year)
if not month == 0:
if month == 2 and bissextile(year):
if day <= 29:
wValue = pywikibot.WbTime(year=year, month=month, day=day, precision='day', calendarmodel=calendar(year, month, day) )
else:
if day <= lMonth[month]:
wValue = pywikibot.WbTime(year=year, month=month, day=day, precision='day', calendarmodel=calendar(year, month, day) )
elif len(elt) == 2:
day = 0
month = 0
i = 1
while i <= 12:
mon = Month[i].lower()
if len(elt[0]) == 3:
mon = mon[:3]
if elt[0].lower() == mon:
month = i
break
i += 1
year = elt[1]
if not month == 0:
wValue = pywikibot.WbTime(year=year, month=month, day=0, precision='month', calendarmodel=calendar(year, month, day) )
elif len(elt) == 1:
day = 0
year = elt[0]
month = 0
wValue = pywikibot.WbTime(year=year, month=0, day=0, precision='year', calendarmodel=calendar(year, month, day) )
return wValue
def testDateToWiki(Value):
wDate = DateToWiki(Value)
if str(type(wDate)) == "<class 'pywikibot.WbTime'>":
print( "DateToWiki(" + Value + ") = " + str(wDate.toWikibase()) )
else:
print( "DateToWiki(" + Value + ") = " + str(wDate) )
#======================================================
# Fonction DayToWiki
#
# assume that value is a string as JJ Mmm AAAA (ex. : "5 nov 2015")
def DayToWiki(Value):
wValue = ""
elt = Value.split(" ")
if len(elt) == 3:
day = int(elt[0])
i = 1
month = 0
while i <= 12:
mon = Month[i].lower()
if len(elt[1]) == 3:
mon = mon[:3]
if elt[1].lower() == mon:
month = i
break
i += 1
year = elt[2]
if not month == 0:
if month == 2 and bissextile(year):
if day <= 29:
wValue = pywikibot.WbTime(year=year, month=month, day=day, precision='day')
else:
if day <= lMonth[month]:
wValue = pywikibot.WbTime(year=year, month=month, day=day, precision='day')
return wValue
def testDayToWiki(Value):
wDate = DateToWiki(Value)
if str(type(wDate)) == "<class 'pywikibot.WbTime'>":
print( "DayToWiki(" + Value + ") = " + str(wDate.toWikibase()) )
else:
print( "DayToWiki(" + Value + ") = " + str(wDate) )
#======================================================
# Fonction ExtractDate
#
def ExtractDate(Value):
global Month
dic = Value.toWikibase()
Text = Value.toTimestr()
if dic[u'precision'] < 9:
tDate = 'None'
if dic[u'precision'] >= 9:
tDate = str(int(Text[8:12]))
if dic[u'precision'] >= 10:
tMonth = Month[int(Text[13:15])]
tDate = tMonth[:3] + " " + tDate
if dic[u'precision'] >= 11:
tDate = str(int(Text[16:18])) + " " + tDate
return tDate
def mainTestDate():
testBissextile(1500)
testBissextile(1552)
testBissextile(1600)
testBissextile(1900)
testBissextile(1971)
testBissextile(1980)
testBissextile(2000)
testDateToWiki("1601")
testDateToWiki("Jan 1920")
testDateToWiki("Feb 1920")
testDateToWiki("Mar 1920")
testDateToWiki("Apr 1920")
testDateToWiki("May 1920")
testDateToWiki("Jun 1920")
testDateToWiki("Jul 1920")
testDateToWiki("Aug 1920")
testDateToWiki("Sep 1920")
testDateToWiki("Oct 1920")
testDateToWiki("Nov 1920")
testDateToWiki("Dec 1920")
testDateToWiki("4 Jan 1477")
testDateToWiki("15 Feb 1968")
testDateToWiki("3 Mar 1990")
testDateToWiki("10 Apr 1920")
testDateToWiki("9 May 1920")
testDateToWiki("25 Jun 1920")
testDateToWiki("2 Jul 1965")
testDateToWiki("7 Aug 1920")
testDateToWiki("20 Sep 1920")
testDateToWiki("31 Oct 1920")
testDateToWiki("11 Nov 1920")
testDateToWiki("25 Dec 1920")
testDateToWiki("Mai 1920")
testDateToWiki("May 1920")
testDateToWiki("33 Mar 1990")
testDayToWiki("4 Jan 1477")
testDayToWiki("15 Feb 1968")
testDayToWiki("3 Mar 1990")
testDayToWiki("10 Apr 1920")
testDayToWiki("9 May 1920")
testDayToWiki("25 Jun 1920")
testDayToWiki("2 Jul 1965")
testDayToWiki("7 Aug 1920")
testDayToWiki("20 Sep 1920")
testDayToWiki("31 Oct 1920")
testDayToWiki("11 Nov 1920")
testDayToWiki("25 Dec 1920")
testDayToWiki("33 Mar 1990")
if __name__ == "__main__":
mainTestDate()