Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add Millisecond to date element #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions hidateinfer/date_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class Minute(DateElement):

@staticmethod
def is_match(token):
if len(token) > 3:
return False
try:
minute = int(token)
return 0 <= minute <= 59
Expand Down Expand Up @@ -207,6 +209,8 @@ class Second(DateElement):

@staticmethod
def is_match(token):
if len(token) > 3:
return False
try:
second = int(token)
return 0 <= second <= 60
Expand All @@ -218,6 +222,25 @@ def is_numerical():
return True


class Millisecond(DateElement):
"""000000 .. 999999

Normally, millisecond range from 0 to 999999.
"""

directive = "%f"

@staticmethod
def is_match(token):
if len(token) != 6:
return False
try:
millisecond = int(token)
return 0 <= millisecond <= 999999
except ValueError:
return False


class Timezone(DateElement):
"""IANA common timezones (e.g. UTC, EST, US/Eastern, ...)"""

Expand Down
2 changes: 2 additions & 0 deletions hidateinfer/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MonthTextLong,
MonthTextShort,
Second,
Millisecond,
Timezone,
UTCOffset,
WeekdayLong,
Expand Down Expand Up @@ -51,6 +52,7 @@
DayOfMonth(),
Minute(),
Second(),
Millisecond(),
Year2(),
Year4(),
UTCOffset(),
Expand Down
7 changes: 7 additions & 0 deletions hidateinfer/tests/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,10 @@ format: "%A %d %B %Y %I:%M %p"
examples:
- Tuesday 5 March 2019 05:09 PM
...
---
name: UTC with millisecond
format: "%Y-%m-%dT%H:%M:%S.%f"
examples:
- 2014-01-22T23:21:05.000055
- 2015-02-23T16:05:31.020023
...