Skip to content

Commit 7de6f28

Browse files
committed
Fix missing x token for string formatting
1 parent aa121e1 commit 7de6f28

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fixed `from_format()` not recognizing input strings when the specified pattern had escaped elements.
8+
- Fixed missing `x` token for string formatting.
89

910

1011
## [2.0.3] - 2018-07-30

pendulum/formatting/formatter.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,23 @@
3737

3838
class Formatter:
3939

40-
_TOKENS = "\[([^\[]*)\]|\\\(.)|" "(" "Mo|MM?M?M?" "|Do|DDDo|DD?D?D?|ddd?d?|do?" "|E{1,4}" "|w[o|w]?|W[o|W]?|Qo?" "|YYYY|YY|Y" "|gg(ggg?)?|GG(GGG?)?" "|a|A" "|hh?|HH?|kk?" "|mm?|ss?|S{1,9}" "|x|X" "|zz?|ZZ?" "|LTS|LT|LL?L?L?" ")"
40+
_TOKENS = (
41+
"\[([^\[]*)\]|\\\(.)|"
42+
"("
43+
"Mo|MM?M?M?"
44+
"|Do|DDDo|DD?D?D?|ddd?d?|do?"
45+
"|E{1,4}"
46+
"|w[o|w]?|W[o|W]?|Qo?"
47+
"|YYYY|YY|Y"
48+
"|gg(ggg?)?|GG(GGG?)?"
49+
"|a|A"
50+
"|hh?|HH?|kk?"
51+
"|mm?|ss?|S{1,9}"
52+
"|x|X"
53+
"|zz?|ZZ?"
54+
"|LTS|LT|LL?L?L?"
55+
")"
56+
)
4157

4258
_FORMAT_RE = re.compile(_TOKENS)
4359

@@ -106,6 +122,7 @@ class Formatter:
106122
"SSSSSS": lambda dt: "{:06d}".format(dt.microsecond),
107123
# Timestamp
108124
"X": lambda dt: "{:d}".format(dt.int_timestamp),
125+
"x": lambda dt: "{:d}".format(dt.int_timestamp * 1000 + dt.microsecond // 1000),
109126
# Timezone
110127
"zz": lambda dt: "{}".format(dt.tzname() if dt.tzinfo is not None else ""),
111128
"z": lambda dt: "{}".format(dt.timezone_name or ""),

tests/formatting/test_formatter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ def test_timestamp():
209209
assert f.format(d.add(days=1), "X") == "86400"
210210

211211

212+
def test_timestamp_milliseconds():
213+
f = Formatter()
214+
d = pendulum.datetime(1970, 1, 1)
215+
assert f.format(d, "x") == "0"
216+
assert f.format(d.add(days=1), "x") == "86400000"
217+
assert f.format(d.add(days=1, microseconds=129123), "x") == "86400129"
218+
219+
212220
def test_date_formats():
213221
f = Formatter()
214222
d = pendulum.datetime(2016, 8, 28, 7, 3, 6, 123456)

0 commit comments

Comments
 (0)