FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pendulum/pendulum/parser.py at develop · python-pendulum/pendulum · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python-pendulum
/
pendulum
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
445
Star
6.7k
Code
Issues
206
Pull requests
60
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pendulum
/
pendulum
/
parser.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (57 loc) · 1.89 KB
Breadcrumbs
pendulum
/
pendulum
/
parser.py
Copy path
File metadata and controls
75 lines (57 loc) · 1.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
from
__future__
import
division
from
.
parsing
import
Parser
as
BaseParser
from
.
tz
import
UTC
from
.
pendulum
import
Pendulum
from
.
date
import
Date
from
.
time
import
Time
from
.
_global
import
Global
class
Parser
(
BaseParser
):
"""
Parser that returns known types (Pendulum, Date, Time)
"""
def
parse
(
self
,
text
):
"""
Parses a string with the given options.
:param text: The string to parse.
:type text: str
:rtype: mixed
"""
# Handling special cases
if
text
==
'now'
:
return
Pendulum
.
now
()
parsed
=
super
(
Parser
,
self
).
parse
(
text
)
if
not
self
.
is_exact
():
return
self
.
_create_pendulum_object
(
parsed
)
# Checking for date
if
'year'
in
parsed
:
# Checking for time
if
'hour'
in
parsed
:
return
self
.
_create_pendulum_object
(
parsed
)
else
:
return
self
.
_create_date_object
(
parsed
)
return
self
.
_create_time_object
(
parsed
)
def
_create_pendulum_object
(
self
,
parsed
):
if
parsed
[
'offset'
]
is
None
:
tz
=
self
.
_options
.
get
(
'tz'
,
UTC
)
else
:
tz
=
parsed
[
'offset'
]
/
3600
return
Pendulum
(
parsed
[
'year'
],
parsed
[
'month'
],
parsed
[
'day'
],
parsed
[
'hour'
],
parsed
[
'minute'
],
parsed
[
'second'
],
parsed
[
'subsecond'
],
tzinfo
=
tz
)
def
_create_date_object
(
self
,
parsed
):
return
Date
(
parsed
[
'year'
],
parsed
[
'month'
],
parsed
[
'day'
]
)
def
_create_time_object
(
self
,
parsed
):
return
Time
(
parsed
[
'hour'
],
parsed
[
'minute'
],
parsed
[
'second'
],
parsed
[
'subsecond'
]
)
def
parse
(
text
,
**
options
):
# Use the mock now value if it exists
options
[
'now'
]
=
options
.
get
(
'now'
,
Global
.
get_test_now
())
return
Parser
(
**
options
).
parse
(
text
)
Back
|
FazBrowse Home
|
New Git URL