2

I am creating a application about the history. But I am getting stuck when I need to create a field with django DateTimeField to represent the date time for B.C.(aka Before Christ).

eg. Roman Empire is from 27 B.C. to 395 AD. How can I represent the from and to date with python datetime or django DateTimeField?

I need some further date calculation on these fields, so an integer is not quite well for this, any idea?

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
Enix
  • 4,415
  • 1
  • 24
  • 37

2 Answers2

3

You can also use datautil. Datautil

Install it as :

pip install datautil

Then use it as:

from datautil.date import parse

fd = parse(u'Feb 1996')
print fd

<class 'datautil.date.FlexiDate'> 1996-02

fd.as_datetime()
datetime.datetime(1956, 1, 1, 0, 0)

bc = parse(u'1700BC')
print bc 
<class 'datautil.date.FlexiDate'> -1700

Here is also a useful link.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
  • interesting utility... but does `dateutil` support date calculation, e.g., date compare? – Enix Dec 29 '16 at 07:43
  • @Enix Date calculations will be same as all DateTimeField calculations.You have to handle it yourself. Whenever you will calculate the duration or gap from present date and time, The results should be adjusted as per the minus (-) sign appeared in the datautil result. – Prakhar Trivedi Dec 29 '16 at 07:46
  • thanks for your message... but i got this error when i run with your code: `AttributeError: 'tuple' object has no attribute 'year'`. i am using python2.7.. it seems not much document included, and not quite user friendly. I am wondering how to start with it... – Enix Dec 29 '16 at 07:56
0

Also, see the discussion on this same question from 2013 on SO:

BC dates in Python

Community
  • 1
  • 1
Chris Larson
  • 1,684
  • 1
  • 11
  • 19
  • chris in these cases the question should not be marked as duplicate (Getting a question marked as duplicate does not harm the question's poster) – e4c5 Dec 29 '16 at 08:34
  • @e4c5 That was my first impulse, however, I couldn't figure out how to do so, so I posted my answer both as a gentle suggestion to the asker and in hopes someone else would mark it. Is there a way to do it myself? Reading through the 'Help Center,' the only thing I could find was: "Moderators and anyone with 3000 reputation may vote to close a question as a duplicate by clicking the "close" link and entering in the URL or title of the question they believe it to be a duplicate of." I'm just building my own reputation. It's good to know, however, that such marking doesn't harm the asker. – Chris Larson Dec 29 '16 at 17:58
  • I think you have to flag it till you hit 3000 – e4c5 Dec 29 '16 at 23:50
  • I considered that, but the only flags available are: spam, ,rude or abusive, and in need of moderator intervention. I suppose that last would work, but I wasn't sure it _wouldn't_ damage the asker's reputation and it somehow seemed heavy handed for the task at hand. Also, with only 10 flags available at my reputation level, I was reluctant to use one. Seems like 'Already asked' should be a flag as well. ::sigh:: Anyhow, thank you. – Chris Larson Dec 30 '16 at 00:12
  • no worries. I've forgotten how things use to be before my reps went up. Thanks for the timely reminder. Always good to get one's feet back on the ground – e4c5 Dec 30 '16 at 01:00