summaryrefslogtreecommitdiffstats
path: root/Lib/_strptime.py
diff options
context:
space:
mode:
authorNice Zombies <nineteendo19d0@gmail.com>2024-09-25 21:43:58 (GMT)
committerGitHub <noreply@github.com>2024-09-25 21:43:58 (GMT)
commit9968caa0cc8a4d07595f0e0abe5f28ba9b6f7a96 (patch)
tree34514828a85ef128027d75c737858a5eebc6fbbf /Lib/_strptime.py
parentb0c6cf5f17f0be13aa927cf141a289f7b76ae6b1 (diff)
downloadcpython-9968caa0cc8a4d07595f0e0abe5f28ba9b6f7a96.zip
cpython-9968caa0cc8a4d07595f0e0abe5f28ba9b6f7a96.tar.gz
cpython-9968caa0cc8a4d07595f0e0abe5f28ba9b6f7a96.tar.bz2
gh-41431: Add `datetime.time.strptime()` and `datetime.date.strptime()` (#120752)
* Python implementation * C implementation * Test `date.strptime` * Test `time.strptime` * 📜🤖 Added by blurb_it. * Update whatsnew * Update documentation * Add leap year note * Update 2024-06-19-19-53-42.gh-issue-41431.gnkUc5.rst * Apply suggestions from code review Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> * Remove parentheses * Use helper function * Remove bad return * Link to github issue * Fix directive * Apply suggestions from code review Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com> * Fix test cases --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
Diffstat (limited to 'Lib/_strptime.py')
-rw-r--r--Lib/_strptime.py44
1 files changed, 33 insertions, 11 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index 3f868bc..a3f8bb5 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -567,18 +567,40 @@ def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
tt = _strptime(data_string, format)[0]
return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
-def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
- """Return a class cls instance based on the input string and the
+def _strptime_datetime_date(cls, data_string, format="%a %b %d %Y"):
+ """Return a date instance based on the input string and the
+ format string."""
+ tt, _, _ = _strptime(data_string, format)
+ args = tt[:3]
+ return cls(*args)
+
+def _parse_tz(tzname, gmtoff, gmtoff_fraction):
+ tzdelta = datetime_timedelta(seconds=gmtoff, microseconds=gmtoff_fraction)
+ if tzname:
+ return datetime_timezone(tzdelta, tzname)
+ else:
+ return datetime_timezone(tzdelta)
+
+def _strptime_datetime_time(cls, data_string, format="%H:%M:%S"):
+ """Return a time instance based on the input string and the
format string."""
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
tzname, gmtoff = tt[-2:]
- args = tt[:6] + (fraction,)
- if gmtoff is not None:
- tzdelta = datetime_timedelta(seconds=gmtoff, microseconds=gmtoff_fraction)
- if tzname:
- tz = datetime_timezone(tzdelta, tzname)
- else:
- tz = datetime_timezone(tzdelta)
- args += (tz,)
+ args = tt[3:6] + (fraction,)
+ if gmtoff is None:
+ return cls(*args)
+ else:
+ tz = _parse_tz(tzname, gmtoff, gmtoff_fraction)
+ return cls(*args, tz)
- return cls(*args)
+def _strptime_datetime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
+ """Return a datetime instance based on the input string and the
+ format string."""
+ tt, fraction, gmtoff_fraction = _strptime(data_string, format)
+ tzname, gmtoff = tt[-2:]
+ args = tt[:6] + (fraction,)
+ if gmtoff is None:
+ return cls(*args)
+ else:
+ tz = _parse_tz(tzname, gmtoff, gmtoff_fraction)
+ return cls(*args, tz)