Odd behavior and offsets when using now() rather than utcnow()

Asked by Dustin Oprea on 2013-09-28

1) Why do the given offsets not reflect DST (see below)? Mountain, Central, and Eastern
   should currently be at -6, -5, -4, respectively.
2) Why does Detroit have the odd offset '-5.53..'? It should be -5, when not DST. EST and
   US/Eastern are -5, as expected (though they still don't reflect DST).

    >>> from pytz import timezone
    >>> from datetime import datetime

    >>> la = timezone('America/Los_Angeles')
    >>> denver = timezone('America/Denver')
    >>> chicago = timezone('America/Chicago')
    >>> detroit = timezone('America/Detroit')

    >>> datetime.now().replace(tzinfo=la).utcoffset().total_seconds() / 3600
    -8.0

    >>> datetime.now().replace(tzinfo=denver).utcoffset().total_seconds() / 3600
    -7.0

    >>> datetime.now().replace(tzinfo=chicago).utcoffset().total_seconds() / 3600
    -6.0

    >>> datetime.now().replace(tzinfo=detroit).utcoffset().total_seconds() / 3600
    -5.533333333333333

3) Why don't I get different times when converting Eastern to Central? It works fine
   when I convert from Eastern to Mountain:

    >>> eastern = timezone('US/Eastern')
    >>> central = timezone('US/Central')
    >>> mountain = timezone('US/Mountain')
    >>> now = datetime.now()

    >>> now.replace(tzinfo=eastern).utcoffset().total_seconds() / 3600
    -5.0
    >>> now.replace(tzinfo=central).utcoffset().total_seconds() / 3600
    -6.0
    >>> now.replace(tzinfo=mountain).utcoffset().total_seconds() / 3600
    -7.0

    >>> now.replace(tzinfo=eastern).strftime('%H:%M:%S %z')
    '16:15:53 -0500'

    >>> now.replace(tzinfo=eastern).astimezone(central).strftime('%H:%M:%S %z')
    '16:15:53 -0500'

    >>> now.replace(tzinfo=eastern).astimezone(mountain).strftime('%H:%M:%S %z')
    '15:15:53 -0600'

Yet, everything works fine when I start with utcnow() instead of now():

    >>> utc = timezone('UTC')
    >>> nowutc = datetime.utcnow().replace(tzinfo=utc)

    >>> nowutc.astimezone(eastern).strftime('%H:%M:%S %z')
    '16:24:14 -0400'
    >>> nowutc.astimezone(central).strftime('%H:%M:%S %z')
    '15:24:14 -0500'
    >>> nowutc.astimezone(mountain).strftime('%H:%M:%S %z')
    '14:24:14 -0600'

Question information

Language:
English Edit question
Status:
Answered
For:
pytz Edit question
Assignee:
No assignee Edit question
Last query:
2013-09-28
Last reply:
2013-10-01
Dustin Oprea (myselfasunder) said : #1

1) Why do the given offsets not reflect DST (see below)? Mountain, Central, and Eastern
   should currently be at -6, -5, -4, respectively.
2) Why does Detroit have the odd offset '-5.53..'? It should be -5, when not DST. EST and
   US/Eastern are -5, as expected (though they still don't reflect DST).

    >>> from pytz import timezone
    >>> from datetime import datetime

    >>> la = timezone('America/Los_Angeles')
    >>> denver = timezone('America/Denver')
    >>> chicago = timezone('America/Chicago')
    >>> detroit = timezone('America/Detroit')

    >>> datetime.now().replace(tzinfo=la).utcoffset().total_seconds() / 3600
    -8.0

    >>> datetime.now().replace(tzinfo=denver).utcoffset().total_seconds() / 3600
    -7.0

    >>> datetime.now().replace(tzinfo=chicago).utcoffset().total_seconds() / 3600
    -6.0

    >>> datetime.now().replace(tzinfo=detroit).utcoffset().total_seconds() / 3600
    -5.533333333333333

3) Why don't I get different times when converting Eastern to Central? It works fine
   when I convert from Eastern to Mountain:

    >>> eastern = timezone('US/Eastern')
    >>> central = timezone('US/Central')
    >>> mountain = timezone('US/Mountain')
    >>> now = datetime.now()

    >>> now.replace(tzinfo=eastern).utcoffset().total_seconds() / 3600
    -5.0
    >>> now.replace(tzinfo=central).utcoffset().total_seconds() / 3600
    -6.0
    >>> now.replace(tzinfo=mountain).utcoffset().total_seconds() / 3600
    -7.0

    >>> now.replace(tzinfo=eastern).strftime('%H:%M:%S %z')
    '16:15:53 -0500'

    >>> now.replace(tzinfo=eastern).astimezone(central).strftime('%H:%M:%S %z')
    '16:15:53 -0500'

    >>> now.replace(tzinfo=eastern).astimezone(mountain).strftime('%H:%M:%S %z')
    '15:15:53 -0600'

Yet, everything works fine when I start with utcnow() instead of now():

    >>> utc = timezone('UTC')
    >>> nowutc = datetime.utcnow().replace(tzinfo=utc)

    >>> nowutc.astimezone(eastern).strftime('%H:%M:%S %z')
    '16:24:14 -0400'
    >>> nowutc.astimezone(central).strftime('%H:%M:%S %z')
    '15:24:14 -0500'
    >>> nowutc.astimezone(mountain).strftime('%H:%M:%S %z')
    '14:24:14 -0600'

Stuart Bishop (stub) said : #2

Stuart Bishop suggests this article as an answer to your question:
FAQ #1: “pytz is giving me incorrect timezone information”.

Can you help with this problem?

Provide an answer of your own, or ask Dustin Oprea for more information if necessary.

To post a message you must log in.