Skip to content

Use local timezone offset by default in datetime.timezone #58291

Closed

Description

@ncoghlan
BPO 14083
Nosy @malemburg, @ncoghlan, @abalkin, @pitrou
Superseder
  • bpo-9527: Add aware local time support to datetime module
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-02-22.04:24:18.376>
    created_at = <Date 2012-02-22.02:27:38.245>
    labels = ['type-feature', 'library']
    title = 'Use local timezone offset by default in datetime.timezone'
    updated_at = <Date 2012-02-22.04:24:18.375>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2012-02-22.04:24:18.375>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-02-22.04:24:18.376>
    closer = 'ncoghlan'
    components = ['Library (Lib)']
    creation = <Date 2012-02-22.02:27:38.245>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 14083
    keywords = []
    message_count = 3.0
    messages = ['153923', '153925', '153927']
    nosy_count = 4.0
    nosy_names = ['lemburg', 'ncoghlan', 'belopolsky', 'pitrou']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'needs patch'
    status = 'closed'
    superseder = '9527'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14083'
    versions = ['Python 3.3']

    Activity

    1. ncoghlan commented on Feb 22, 2012

      @ncoghlan
      ContributorAuthor

      Currently, datetime.timezone requires that the offset be specified explicitly.

      It would be more convenient if the local offset was used by default.

      Since the time module already exposes the offset details for the local timezone, this would just make:

        local_tz = datetime.timezone()

      equivalent to the current complex incantation:

        local_tz = datetime.timezone(datetime.timedelta(hours=(time.timezone / -3600 + tm.tm_isdst)))

      Then getting a timezone aware version of the local time would just be:

        now = datetime.datetime.now(datetime.timezone())

      Similar to the existing spelling for UTC:

        now = datetime.datetime.now(datetime.timezone.utc)

      (Inspired by http://hyperpolyglot.org/scripting#timezone-offset-notes)

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Feb 22, 2012
    3. abalkin commented on Feb 22, 2012

      @abalkin
      Member

      The problem with this idea is that while evaluating datetime.datetime.now(datetime.timezone()), python will have to query the real time clock twice (first in timezone() and then in now()). At a particularly unfortunate time this may result in an error. Unlikely? Yes, but I would not board a python powered airplane if this problem was in the standard library.

      I gave a lot of thought to the problem of supporting timezone aware local time and bpo-9527 was the best solution I was able to come up with.

    4. ncoghlan commented on Feb 22, 2012

      @ncoghlan
      ContributorAuthor

      Marking as a duplicate of bpo-9527.

      If that API is added, the local fixed offset timezone will be accessible as datetime.datetime.localtime().tzinfo which would be simple enough.

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions