add 'd' conversion for format strings
to convert a timestamp to a formattable 'datetime' object.
For example '{created_at!d:%Y-%m-%d}'
transforms the timestamp in 'created_at' into a 'datetime' object
and then formats its content using '%Y-%m-%d' as template.
1262304000 -> datetime(2010, 1, 1) -> "2010-01-01"
This commit is contained in:
@@ -506,6 +506,7 @@ class Formatter():
|
|||||||
- "c": calls str.capitalize
|
- "c": calls str.capitalize
|
||||||
- "C": calls string.capwords
|
- "C": calls string.capwords
|
||||||
- "t": calls str.strip
|
- "t": calls str.strip
|
||||||
|
- "d": calls text.parse_timestamp
|
||||||
- "U": calls urllib.parse.unquote
|
- "U": calls urllib.parse.unquote
|
||||||
- "S": calls util.to_string()
|
- "S": calls util.to_string()
|
||||||
- Example: {f!l} -> "example"; {f!u} -> "EXAMPLE"
|
- Example: {f!l} -> "example"; {f!u} -> "EXAMPLE"
|
||||||
@@ -537,6 +538,7 @@ class Formatter():
|
|||||||
"c": str.capitalize,
|
"c": str.capitalize,
|
||||||
"C": string.capwords,
|
"C": string.capwords,
|
||||||
"t": str.strip,
|
"t": str.strip,
|
||||||
|
"d": text.parse_timestamp,
|
||||||
"U": urllib.parse.unquote,
|
"U": urllib.parse.unquote,
|
||||||
"S": to_string,
|
"S": to_string,
|
||||||
"s": str,
|
"s": str,
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import unittest
|
|||||||
import io
|
import io
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import datetime
|
||||||
import http.cookiejar
|
import http.cookiejar
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
@@ -267,6 +268,7 @@ class TestFormatter(unittest.TestCase):
|
|||||||
"n": None,
|
"n": None,
|
||||||
"s": " \n\r\tSPACE ",
|
"s": " \n\r\tSPACE ",
|
||||||
"u": "%27%3C%20/%20%3E%27",
|
"u": "%27%3C%20/%20%3E%27",
|
||||||
|
"t": 1262304000,
|
||||||
"name": "Name",
|
"name": "Name",
|
||||||
"title1": "Title",
|
"title1": "Title",
|
||||||
"title2": "",
|
"title2": "",
|
||||||
@@ -289,6 +291,9 @@ class TestFormatter(unittest.TestCase):
|
|||||||
self._run_test("{a!S}", self.kwdict["a"])
|
self._run_test("{a!S}", self.kwdict["a"])
|
||||||
self._run_test("{l!S}", "a, b, c")
|
self._run_test("{l!S}", "a, b, c")
|
||||||
self._run_test("{n!S}", "")
|
self._run_test("{n!S}", "")
|
||||||
|
self._run_test("{t!d}", datetime.datetime(2010, 1, 1))
|
||||||
|
self._run_test("{t!d:%Y-%m-%d}", "2010-01-01")
|
||||||
|
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
self._run_test("{a!q}", "hello world")
|
self._run_test("{a!q}", "hello world")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user