Main Page
Namespaces
Classes
Files
File List
scripts
mycroft
util
time.py
Go to the documentation of this file.
1
# -*- coding: utf-8 -*-
2
#
3
# Copyright 2018 Mycroft AI Inc.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
from
datetime
import
datetime
18
from
dateutil.tz
import
gettz, tzlocal
19
20
21
def
default_timezone
():
22
""" Get the default timezone
23
24
Based on user location settings location.timezone.code or
25
the default system value if no setting exists.
26
27
Returns:
28
(datetime.tzinfo): Definition of the default timezone
29
"""
30
try
:
31
# Obtain from user's configurated settings
32
# location.timezone.code (e.g. "America/Chicago")
33
# location.timezone.name (e.g. "Central Standard Time")
34
# location.timezone.offset (e.g. -21600000)
35
from
mycroft.configuration
import
Configuration
36
config = Configuration.get()
37
code = config[
"location"
][
"timezone"
][
"code"
]
38
39
return
gettz(code)
40
except
Exception:
41
# Just go with system default timezone
42
return
tzlocal()
43
44
45
def
now_utc
():
46
""" Retrieve the current time in UTC
47
48
Returns:
49
(datetime): The current time in Universal Time, aka GMT
50
"""
51
return
to_utc
(datetime.utcnow())
52
53
54
def
now_local
(tz=None):
55
""" Retrieve the current time
56
57
Args:
58
tz (datetime.tzinfo, optional): Timezone, default to user's settings
59
60
Returns:
61
(datetime): The current time
62
"""
63
if
not
tz:
64
tz =
default_timezone
()
65
return
datetime.now(tz)
66
67
68
def
to_utc
(dt):
69
""" Convert a datetime with timezone info to a UTC datetime
70
71
Args:
72
dt (datetime): A datetime (presumably in some local zone)
73
Returns:
74
(datetime): time converted to UTC
75
"""
76
tzUTC = gettz(
"UTC"
)
77
if
dt.tzinfo:
78
return
dt.astimezone(tzUTC)
79
else
:
80
return
dt.replace(tzinfo=gettz(
"UTC"
)).astimezone(tzUTC)
81
82
83
def
to_local
(dt):
84
""" Convert a datetime to the user's local timezone
85
86
Args:
87
dt (datetime): A datetime (if no timezone, defaults to UTC)
88
Returns:
89
(datetime): time converted to the local timezone
90
"""
91
tz =
default_timezone
()
92
if
dt.tzinfo:
93
return
dt.astimezone(tz)
94
else
:
95
return
dt.replace(tzinfo=gettz(
"UTC"
)).astimezone(tz)
96
97
98
def
to_system
(dt):
99
""" Convert a datetime to the system's local timezone
100
101
Args:
102
dt (datetime): A datetime (if no timezone, assumed to be UTC)
103
Returns:
104
(datetime): time converted to the operation system's timezone
105
"""
106
tz = tzlocal()
107
if
dt.tzinfo:
108
return
dt.astimezone(tz)
109
else
:
110
return
dt.replace(tzinfo=gettz(
"UTC"
)).astimezone(tz)
mycroft.util.time.now_local
def now_local(tz=None)
Definition:
time.py:54
mycroft.util.time.to_system
def to_system(dt)
Definition:
time.py:98
mycroft.util.time.now_utc
def now_utc()
Definition:
time.py:45
mycroft.util.time.to_local
def to_local(dt)
Definition:
time.py:83
mycroft.util.time.default_timezone
def default_timezone()
Definition:
time.py:21
mycroft.util.time.to_utc
def to_utc(dt)
Definition:
time.py:68
mycroft.configuration
Definition:
scripts/mycroft/configuration/__init__.py:1
mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40