Skip to content

Commit f4e8022

Browse files
committed
debug wip
1 parent 500b9f6 commit f4e8022

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

src/wtforms/fields/datetime.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def __init__(
3434
def _value(self):
3535
if self.raw_data:
3636
return " ".join(self.raw_data)
37-
return self.data and self.data.strftime(self.format[0]) or ""
37+
format = self.format[0]
38+
return self.data and self.data.strftime(format) or ""
3839

3940
def process_formdata(self, valuelist):
4041
if not valuelist:

src/wtforms/utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
12
import re
23

4+
_LEADING_SYMBOL = "#" if os.name == "nt" else "-"
5+
36
# https://docs.python.org/3/library/datetime.html#technical-detail (see NOTE #9)
47
_DATETIME_STRIP_ZERO_PADDING_FORMATS_RE = re.compile(
5-
"%-["
8+
f"%{_LEADING_SYMBOL}["
69
"d" # day of month
710
"m" # month
811
"H" # hour (24-hour)
@@ -25,7 +28,7 @@ def clean_datetime_format_for_strptime(formats):
2528
return [
2629
re.sub(
2730
_DATETIME_STRIP_ZERO_PADDING_FORMATS_RE,
28-
lambda m: m[0].replace("-", ""),
31+
lambda m: m[0].replace(_LEADING_SYMBOL, ""),
2932
format,
3033
)
3134
for format in formats

tests/fields/test_datetime.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from datetime import datetime
23

34
from tests.common import DummyPostData
@@ -12,27 +13,29 @@ def make_form(name="F", **fields):
1213
class F(Form):
1314
a = DateTimeField()
1415
b = DateTimeField(format="%Y-%m-%d %H:%M")
15-
c = DateTimeField(format="%-m/%-d/%Y %-I:%M")
16+
c = DateTimeField(
17+
format="%#m/%#d/%Y %#I:%M" if os.name == "nt" else "%-m/%-d/%Y %-I:%M"
18+
)
1619

1720

1821
def test_basic():
1922
d = datetime(2008, 5, 5, 4, 30, 0, 0)
2023
# Basic test with both inputs
21-
form = F(
22-
DummyPostData(
23-
a=["2008-05-05", "04:30:00"], b=["2008-05-05 04:30"], c=["5/5/2008 4:30"]
24-
)
25-
)
24+
form = F(DummyPostData(a=["2008-05-05", "04:30:00"]))
2625
assert form.a.data == d
2726
assert (
2827
form.a()
2928
== """<input id="a" name="a" type="datetime" value="2008-05-05 04:30:00">"""
3029
)
30+
31+
form = F(DummyPostData(b=["2008-05-05 04:30"]))
3132
assert form.b.data == d
3233
assert (
3334
form.b()
3435
== """<input id="b" name="b" type="datetime" value="2008-05-05 04:30">"""
3536
)
37+
38+
form = F(DummyPostData(c=["5/5/2008 4:30"]))
3639
assert form.c.data == d
3740
assert (
3841
form.c() == """<input id="c" name="c" type="datetime" value="5/5/2008 4:30">"""

tests/fields/test_datetimelocal.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from datetime import datetime
23

34
from tests.common import DummyPostData
@@ -12,7 +13,9 @@ def make_form(name="F", **fields):
1213
class F(Form):
1314
a = DateTimeLocalField()
1415
b = DateTimeLocalField(format="%Y-%m-%d %H:%M")
15-
c = DateTimeLocalField(format="%-m/%-d/%Y %-I:%M")
16+
c = DateTimeLocalField(
17+
format="%#m/%#d/%Y %#I:%M" if os.name == "nt" else "%-m/%-d/%Y %-I:%M"
18+
)
1619

1720

1821
def test_basic():

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ deps =
1111
babel
1212
email_validator
1313
commands =
14-
pytest --tb=short --basetemp={envtmpdir} {posargs}
14+
pytest --tb=short -l --basetemp={envtmpdir} {posargs}
1515

1616
[testenv:coverage]
1717
deps =

0 commit comments

Comments
 (0)