0

I would like to iterate over days (or better construct an array) of BC dates as I do with typical dates:

from calendar import monthrange
import datetime

for y in range(2010, 2020):
  for m in range(1,13):
    for d in range(1, monthrange(y, m)[1]+1):
      print(datetime.date(year=y,month=m,day=d))

I am aware that this is not possible in pure python datetime library (because of datetime definition) thus I went through this question asked earlier and realized there might be two main options to achieve my task:

  • datautil parsed object (from datautil.date import parse) (looks deprecated)
  • Astropy object (from astropy.time import Time)

Fiddling with an datautil object (python3's package name is python-dateutil (instead of datautil) and the import differs a bit) confuses me even more as it seems to me the BC date ability is no longer perserved / or it is completely different library:

from dateutil.parser import parse

parse(-200-01-01) # is an AD date object: datetime.datetime(200, 1, 1, 0, 0)
parse(u'200BC') # reports: ValueError: ('Unknown string format:', '200BC')

Speaking about astropy - the only mention about negative dates is in FITS section, but I have absolutely no clue how to use the object. Docs is very sparse, without examples and I did not manage to get it working.

My limited knowledge does not allow me to find anywhere in the documentation, if my approach of creating BC date object in python 3+ is possible and if I am somehow able to iterate over days here.

The only "workaround" (monthrange replacement) I can think of is a function last_day_month submitted by JK elsewhere. But for now, can you suggest any way, how can I build a range of days and iterate over them?

Kube Kubow
  • 398
  • 1
  • 6
  • 18

1 Answers1

0

I've might found a way how to proceed.

There is a python library called skyfield, that can hold BC dates (stored as TT - Terrestrial Time), may seem an overkill to use astronomy library, but it suits my needs. Properly formatted can be shown in utc/gmt or other formats... (as written in documentation)

This is an approach how to store BC date:

from skyfield.api import load
ts = load.timescale()
t1 = ts.utc(-200,1,1)
print(t1.utc_strftime('%Y-%m-%d %H:%M'))

Outputs:

-200-01-01 00:00

Combined with JK's answer to get last day from month, I am able to proceed very similar way as I do with AD dates:

from calendar import monthrange
import datetime
from skyfield.api import load

ts = load.timescale()
for y in range(-200, -190):
  for m in range(1,13):
    for d in range(1, last_day_month(y, m)+1):  # this is JK function to find days in month
      print(ts.utc(y,m,d).utc_strftime('%Y-%m-%d %H:%M'))

If there is no-one who can submit any other (and more efficent way) how to iterate over days in BC years, I will consider this as solved.

Kube Kubow
  • 398
  • 1
  • 6
  • 18