[New-bugs-announce] [issue9527] Add aware local time support to datetime module

Alexander Belopolsky report at bugs.python.org
Fri Aug 6 05:48:20 CEST 2010

New submission from Alexander Belopolsky <belopolsky at users.sourceforge.net>:

See python-dev post for motivation.


I am attaching a patch implementing the proposed method in datetime.py.  I will also paste the code below.  Note that this is only prototype.  Real implementation will use tm_zone and tm_gmtoff components of tm structure on platforms that supply them.

    def localtime(cls, t=None, isdst=-1):
        """Return local time as an aware datetime object.                                                                                                          
        If called without arguments, return current time.  Otherwise                                                                                               
        *t* is converted to local time zone according to the system                                                                                                
        time zone database.  If *t* is naive (i.e. t.tzinfo is None),                                                                                              
        it is assumed to be in local time.  In this case, a positive or                                                                                            
        zero value for *isdst* causes localtime to presume initially                                                                                               
        that summer time (for example, Daylight Saving Time) is or is                                                                                              
        not (respectively) in effect for the specified time.  A                                                                                                    
        negative value for *isdst* causes the localtime() function to                                                                                              
        attempt to divine whether summer time is in effect for the                                                                                                 
        specified time.                                                                                                                                            
        if t is None:
            t = _time.time()
            if t.tzinfo is None:
                tm = t.timetuple()[:-1] + (isdst,)
                t = _time.mktime(tm)
                delta = t - datetime(1970, 1, 1, tzinfo=timezone.utc)
                t = delta.total_seconds()
        tm = _time.localtime(t)
        if _time.daylight:
            if tm.tm_isdst:
                offset = _time.altzone
                tzname = _time.tzname[1]
                offset = _time.timezone
                tzname = _time.tzname[0]
        tz = timezone(timedelta(seconds=-offset), tzname)
        return cls.fromtimestamp(t, tz)

assignee: belopolsky
components: Extension Modules, Library (Lib)
files: datetime-localtime-proto.diff
keywords: patch
messages: 113067
nosy: belopolsky
priority: normal
severity: normal
status: open
title: Add aware local time support to datetime module
type: feature request
versions: Python 3.2
Added file: http://bugs.python.org/file18410/datetime-localtime-proto.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list