Search code examples
pythondatetimeodoodayofweek

Find day from given date in python


Hello friends I have written two methods in python which returns the day according to the date passed to them but however the output I am getting is different in some cases. Pasting my code below please have a look at it and suggest me corrections....

e.g. for date "10/06/2014" (m/d/yy) one function returns output as Monday which is correct and another returns it as Sunday which is not. I am using Odoo8 (Openerp) server's method in one case and Python's method in other.

def a():
    month, day, year = (int(x) for x in "10/06/2014".split('/'))
    ans = datetime.date(year, month, day)
    day_name = ans.strftime("%A")    # the day_name is Monday here


def b():
    dt = datetime.strptime("10/06/2014", "%m/%d/%y")
    dt = fields.Datetime.context_timestamp(self, dt)   # openerp's ORM method 
    t = dt.strftime("%A")        # the day_name is Sunday here

Solution

  • I guess that's because you are to the west of Greenwich, datetime.date is timezone-unaware, and openerp.fields.Datetime.context_timestamp is timezone-aware.

    If your timezone is PST, for example, the timestamp 10/06/2014 0:00 UTC becomes 10/05/2014 16:00 PST, which is obviously Sunday.