Skip to content

Commit

Permalink
Improve regex for ignoring locales when looking for datetime formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themanwithoutaplan committed Jun 17, 2015
1 parent 0f567f0 commit 25963e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions openpyxl/styles/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ def is_date_format(self):


DATE_INDICATORS = 'dmyhs'
BAD_DATE_RE = re.compile(r'(\[|").*[dmhys].*(\]|")')
BAD_DATE_RE = re.compile(r'((?<=\[\$)|").*[dmhys]+.*(\]|")', re.UNICODE)

def is_date_format(fmt):
if fmt is None:
return False
if any([x in fmt.lower() for x in DATE_INDICATORS]):
fmt = fmt.lower()
if any([x in fmt for x in DATE_INDICATORS]):
return not BAD_DATE_RE.search(fmt)
return False

Expand Down
18 changes: 17 additions & 1 deletion openpyxl/styles/tests/test_number_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,25 @@ def __init__(self, value=None):
@pytest.mark.parametrize("format, result",
[
("DD/MM/YY", True),
("H:MM:SS;@", True)
("H:MM:SS;@", True),
("H:MM:SS;@", True),
(u'#,##0\\ [$\u20bd-46D]', False)
]
)
def test_is_date_format(format, result):
from ..numbers import is_date_format
assert is_date_format(format) is result


@pytest.mark.parametrize("fmt, result",
[
("[h]:mm:ss", False),
("[hh]:mm:ss", False),
(u'#,##0\\ [$\u20bd-46D]', True),
('"$"#,##0_);[Red]("$"#,##0)', True)
]
)
def test_datetime_regex(fmt, result):
from ..numbers import BAD_DATE_RE
match = BAD_DATE_RE.search(fmt.lower()) is not None
assert match is result

0 comments on commit 25963e3

Please # to comment.