-1

I want to validate dates in some RFC defined formats. The problem is that the timezone could differ eg

24 Dec 1991 23:59:23 UTC
24 Dec 1991 23:59:23 EST
24 Dec 1991 23:59:23 CST

And I expanded upon this code which works nicely How to validate a specific Date and Time format using Python

def validate_date(d):
    try:
        datetime.strptime(d, '%d %b %Y %H:%M:%S %Z')
        return True
    except ValueError:
        return False

As long as the timezone is UTC (my local).

I could change my local timezone I suppose Setting timezone in Python

But then I would need to extract the last part of the string and test it but it feels messy. Then I rather go with a complete regex solution.

I'm looking into http://labix.org/python-dateutil, not sure how to use it for this problem though.

Community
  • 1
  • 1
user1267259
  • 761
  • 2
  • 10
  • 22
  • Wow, ok, I suppose this question is bad (-2 after 8mins)? But could someone please explain why? Obviously I'm missing something obvious? – user1267259 Feb 22 '16 at 22:10
  • 1
    Not one of the downvotes but... I wouldn't do this by hand. Dates are hard to do right (there are lots edge cases). People write whole books on handling dates correctly (and to make it more interesting, "correctly" varies on usage). Use a library and save yourself the time, effort and headaches. See [here](https://julien.danjou.info/blog/2015/python-and-timezones) for an explanation of Python's failings with dates. I've heard [Arrow](http://crsmithdev.com/arrow/) ([github](https://github.com/crsmithdev/arrow)) is good but haven't used it myself as I tend to use django's internal date library. – Basic Feb 22 '16 at 22:32
  • 1
    Likely you need a module or modules to handle timezone definitions. I used a combination of [pytz](https://pypi.python.org/pypi/pytz) and [tzlocal](https://pypi.python.org/pypi/tzlocal) to solve a similar task – user3159253 Feb 22 '16 at 22:39
  • @user3159253 Thank you! Yes I found pytz but I've already installed dateutil so I was hoping I could use it (it seems to also have a timezone library), the problem is I don't know how to combine it with the datetime module or if it is even possible. – user1267259 Feb 22 '16 at 22:51
  • @Basic Thank you! I suppose I was too generic in my question, I'm not looking for validating all formats, just some specific few RFC defined formats (I hope I still don't sound like someone who deserves to be drive-by downvoted....). I've googled but I couldn't find anything. The closes I found was the so answer (and some others so answers like it) I linked to. I'll have a look into the Arrow module. – user1267259 Feb 22 '16 at 22:58

1 Answers1

0

I really don't understand what all the fuzz was about.

Using dateutil

https://stackoverflow.com/a/4766400/1267259

And the limitations of that solution can be found at https://stackoverflow.com/a/18407231/1267259 (What are the "standard" timezone abbreviations?)

Community
  • 1
  • 1
user1267259
  • 761
  • 2
  • 10
  • 22