Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2870)

Side by Side Diff: Lib/datetime.py

Issue 9527: Add aware local time support to datetime module
Patch Set: Created 1 year, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Concrete date/time and related types -- prototype implemented in Python. 1 """Concrete date/time and related types -- prototype implemented in Python.
2 2
3 See http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage 3 See http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
4 4
5 See also http://dir.yahoo.com/Reference/calendars/ 5 See also http://dir.yahoo.com/Reference/calendars/
6 6
7 For a primer on DST, including many current DST rules, see 7 For a primer on DST, including many current DST rules, see
8 http://webexhibits.org/daylightsaving/ 8 http://webexhibits.org/daylightsaving/
9 9
10 For more about DST than you ever wanted to know, see 10 For more about DST than you ever wanted to know, see
(...skipping 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 @classmethod 1408 @classmethod
1409 def combine(cls, date, time): 1409 def combine(cls, date, time):
1410 "Construct a datetime from a given date and a given time." 1410 "Construct a datetime from a given date and a given time."
1411 if not isinstance(date, _date_class): 1411 if not isinstance(date, _date_class):
1412 raise TypeError("date argument must be a date instance") 1412 raise TypeError("date argument must be a date instance")
1413 if not isinstance(time, _time_class): 1413 if not isinstance(time, _time_class):
1414 raise TypeError("time argument must be a time instance") 1414 raise TypeError("time argument must be a time instance")
1415 return cls(date.year, date.month, date.day, 1415 return cls(date.year, date.month, date.day,
1416 time.hour, time.minute, time.second, time.microsecond, 1416 time.hour, time.minute, time.second, time.microsecond,
1417 time.tzinfo) 1417 time.tzinfo)
1418
1419 @classmethod
1420 def localtime(cls, t=None, isdst=-1):
techtonik 2011/01/14 01:14:26 The t param should probably be called 'time'.
sasha 2011/01/14 04:30:11 Yes, a better name should be found, although it wo
techtonik 2011/01/14 18:28:50 dt is a consistent name for datetime parameters in
1421 """Return local time as an aware datetime object.
techtonik 2011/01/14 01:14:26 ISTM that we need an object hierarchy like DateTim
sasha 2011/01/14 04:30:11 Early versions of datetime has a separate datetime
techtonik 2011/01/14 18:28:50 It must be recorded for history if we want to get
1422
1423 If called without arguments, return current time. Otherwise
1424 *t* is converted to local time zone according to the system
1425 time zone database. If *t* is naive (i.e. t.tzinfo is None),
1426 it is assumed to be in local time. In this case, a positive or
1427 zero value for *isdst* causes localtime to presume initially
1428 that summer time (for example, Daylight Saving Time) is or is
1429 not (respectively) in effect for the specified time. A
1430 negative value for *isdst* causes the localtime() function to
1431 attempt to divine whether summer time is in effect for the
1432 specified time.
1433 """
1434 if t is None:
1435 t = _time.time()
techtonik 2011/01/14 01:14:26 Here we should always receive aware object, but I
sasha 2011/01/14 04:30:11 No. _time.time() returns seconds since epoch. Nai
techtonik 2011/01/14 18:28:50 On 2011/01/14 04:30:11, sasha wrote:
1436 else:
1437 if t.tzinfo is None:
techtonik 2011/01/14 01:14:26 I'd replace this with elif and place comment that
sasha 2011/01/14 04:30:11 Yes, elif would probably be cleaner and I'll add c
1438 tm = t.timetuple()[:-1] + (isdst,)
techtonik 2011/01/14 01:14:26 IIUC return result of time() value is time.struct_
sasha 2011/01/14 04:30:11 No, t here has not been converted to seconds yet,
1439 t = _time.mktime(tm)
1440 else:
1441 delta = t - datetime(1970, 1, 1, tzinfo=timezone.utc)
1442 t = delta.total_seconds()
1443 tm = _time.localtime(t)
1444 if _time.daylight:
1445 if tm.tm_isdst:
1446 offset = _time.altzone
1447 tzname = _time.tzname[1]
1448 else:
1449 offset = _time.timezone
1450 tzname = _time.tzname[0]
1451 tz = timezone(timedelta(seconds=-offset), tzname)
1452 return cls.fromtimestamp(t, tz)
1418 1453
1419 def timetuple(self): 1454 def timetuple(self):
1420 "Return local time tuple compatible with time.localtime()." 1455 "Return local time tuple compatible with time.localtime()."
1421 dst = self.dst() 1456 dst = self.dst()
1422 if dst is None: 1457 if dst is None:
1423 dst = -1 1458 dst = -1
1424 elif dst: 1459 elif dst:
1425 dst = 1 1460 dst = 1
1426 else: 1461 else:
1427 dst = 0 1462 dst = 0
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
2087 _check_time_fields, _check_tzinfo_arg, _check_tzname, 2122 _check_time_fields, _check_tzinfo_arg, _check_tzname,
2088 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, 2123 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month,
2089 _days_before_year, _days_in_month, _format_time, _is_leap, 2124 _days_before_year, _days_in_month, _format_time, _is_leap,
2090 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, 2125 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class,
2091 _wrap_strftime, _ymd2ord) 2126 _wrap_strftime, _ymd2ord)
2092 # XXX Since import * above excludes names that start with _, 2127 # XXX Since import * above excludes names that start with _,
2093 # docstring does not get overwritten. In the future, it may be 2128 # docstring does not get overwritten. In the future, it may be
2094 # appropriate to maintain a single module level docstring and 2129 # appropriate to maintain a single module level docstring and
2095 # remove the following line. 2130 # remove the following line.
2096 from _datetime import __doc__ 2131 from _datetime import __doc__
OLDNEW
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld