Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Docstrings and doctests.

  • Loading branch information...
commit 11f684aa882a6407bf31b5e6998214667110e841 1 parent 3520951
@sbuss authored
Showing with 30 additions and 6 deletions.
  1. +5 −3 wtftz/converter.py
  2. +25 −3 wtftz/parser.py
View
8 wtftz/converter.py
@@ -22,8 +22,9 @@ def convert(timestamp, to_tz="utc", from_tz="utc", naive=True):
"timestamp" string, the `from_tz` parameter will be ignored. For example
if you do the following:
- >>> wtftz.convert(
+ >>> convert(
... '2012-12-23T14:23:03.826437-05:00', to_tz='utc', from_tz='pst')
+ datetime.datetime(2012, 12, 23, 19, 23, 3, 826437)
Then wtftz will use US/Eastern standard time and ignore the 'pst' value
for `from_tz`.
@@ -49,10 +50,11 @@ def convert_free(query):
Args:
query - A string with a time, and a source and destination timezone.
+ Returns a timestamp in the requested timezone.
EG:
- >>> wtftz.convert_free("2012-12-23T14:23:03.826437-05:00 to pst")
- datetime(2012, 12, 23, 11, 23, 03, 826437)
+ >>> convert_free("2012-12-23T14:23:03.826437-05:00 to pst")
+ datetime.datetime(2012, 12, 23, 11, 23, 3, 826437)
"""
ts, fromz, toz = free_text(query)
return convert(ts, toz, fromz)
View
28 wtftz/parser.py
@@ -4,12 +4,14 @@
def free_text(query):
"""Parse a string into the parameters for `convert`.
+ Args:
+ query - A string with a time, and a source and destination timezone.
Returns a triplet (timestamp, from_timezone, to_timezone) parsed from
the query.
Ex:
- >>> convert_free("2012-12-23T14:23:03.826437-05:00 to pst")
- ("2012-12-23T14:23:03.826437-05:00", None, "pst")
+ >>> free_text("2012-12-23T14:23:03.826437-05:00 to pst")
+ ('2012-12-23T14:23:03.826437-05:00', None, 'pst')
"""
try:
return _from_to(query)
@@ -21,10 +23,20 @@ def free_text(query):
except Exception:
pass
- raise MismatchException("%s does not match" % query)
+ raise MismatchException("%s does not match a known pattern" % query)
def _from_to(query):
+ """Try to parse a `from` and `to` timezone from the query.
+
+ Args:
+ query - A string with a time, and a source and destination timezone.
+ Returns a triplet (timestamp, from_timezone, to_timezone) parsed from
+ the query.
+
+ >>> _from_to("2012-12-23T14:23:03.826437 from est to pst")
+ ('2012-12-23T14:23:03.826437', 'est', 'pst')
+ """
template = re.compile(r"(.*)from(.*)to(.*)")
matches = template.findall(query)
if len(matches) == 1:
@@ -37,6 +49,16 @@ def _from_to(query):
def _to(query):
+ """Try to parse a `to` timezone from the query.
+
+ Args:
+ query - A string with a time, and a source and destination timezone.
+ Returns a triplet (timestamp, from_timezone, to_timezone) parsed from
+ the query.
+
+ >>> _to("2012-12-23T14:23:03.826437 to pst")
+ ('2012-12-23T14:23:03.826437', None, 'pst')
+ """
template = re.compile(r"(.*)to(.*)")
fromz = None
matches = template.findall(query)
Please sign in to comment.
Something went wrong with that request. Please try again.