2012-03-07 06:04:18 -06:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
2012-03-20 09:11:01 -05:00
|
|
|
# edbob -- Pythonic Software Framework
|
|
|
|
# Copyright © 2010-2012 Lance Edgar
|
2012-03-07 06:04:18 -06:00
|
|
|
#
|
|
|
|
# This file is part of edbob.
|
|
|
|
#
|
|
|
|
# edbob is free software: you can redistribute it and/or modify it under the
|
2012-03-20 09:11:01 -05:00
|
|
|
# terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
2012-03-07 06:04:18 -06:00
|
|
|
#
|
|
|
|
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2012-03-20 09:11:01 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
|
|
|
# more details.
|
2012-03-07 06:04:18 -06:00
|
|
|
#
|
2012-03-20 09:11:01 -05:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with edbob. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-07 06:04:18 -06:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
"""
|
2012-03-24 12:15:11 -05:00
|
|
|
``edbob.time`` -- Date & Time Utilities
|
2012-03-07 06:04:18 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import pytz
|
2012-03-24 12:15:11 -05:00
|
|
|
import logging
|
2012-03-07 06:04:18 -06:00
|
|
|
|
|
|
|
|
2012-03-20 09:11:01 -05:00
|
|
|
__all__ = ['local_time', 'set_timezone', 'utc_time']
|
2012-03-07 06:04:18 -06:00
|
|
|
|
2012-03-24 12:15:11 -05:00
|
|
|
log = logging.getLogger(__name__)
|
2012-03-07 06:04:18 -06:00
|
|
|
|
2012-03-24 12:15:11 -05:00
|
|
|
timezone = None
|
|
|
|
|
|
|
|
|
|
|
|
def init(config):
|
|
|
|
"""
|
|
|
|
Initializes the time framework. Currently this only sets the local
|
|
|
|
timezone according to config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
tz = config.get('edbob.time', 'timezone')
|
|
|
|
if tz:
|
|
|
|
set_timezone(tz)
|
2012-03-25 13:48:13 -05:00
|
|
|
log.debug("Timezone set to '%s'" % tz)
|
2012-03-24 12:15:11 -05:00
|
|
|
else:
|
|
|
|
log.warning("No timezone configured; falling back to US/Central")
|
|
|
|
set_timezone('US/Central')
|
|
|
|
|
2012-03-07 06:04:18 -06:00
|
|
|
|
|
|
|
def local_time(timestamp=None):
|
|
|
|
"""
|
|
|
|
Tries to return a "localized" version of ``timestamp``, which should be a
|
|
|
|
UTC-based ``datetime.datetime`` instance.
|
|
|
|
|
|
|
|
If a local timezone has been configured, then
|
|
|
|
``datetime.datetime.utcnow()`` will be called to obtain a value for
|
|
|
|
``timestamp`` if one is not specified. Then ``timestamp`` will be modified
|
|
|
|
in such a way that its ``tzinfo`` member contains the local timezone, but
|
|
|
|
the effective UTC value for the timestamp remains accurate.
|
|
|
|
|
|
|
|
If a local timezone has *not* been configured, then
|
|
|
|
``datetime.datetime.now()`` will be called instead to obtain the value
|
|
|
|
should none be specified. ``timestamp`` will be returned unchanged.
|
|
|
|
"""
|
|
|
|
|
2012-03-24 12:15:11 -05:00
|
|
|
if timezone:
|
2012-03-07 06:04:18 -06:00
|
|
|
if timestamp is None:
|
|
|
|
timestamp = datetime.datetime.utcnow()
|
|
|
|
timestamp = pytz.utc.localize(timestamp)
|
2012-03-24 12:15:11 -05:00
|
|
|
return timestamp.astimezone(timezone)
|
2012-03-07 06:04:18 -06:00
|
|
|
|
|
|
|
if timestamp is None:
|
|
|
|
timestamp = datetime.datetime.now()
|
|
|
|
return timestamp
|
|
|
|
|
|
|
|
|
|
|
|
def set_timezone(tz):
|
|
|
|
"""
|
|
|
|
Sets edbob's notion of the "local" timezone. ``tz`` should be an Olson
|
|
|
|
name.
|
|
|
|
|
2012-03-24 20:53:24 -05:00
|
|
|
.. highlight:: ini
|
|
|
|
|
2012-03-07 06:04:18 -06:00
|
|
|
You usually don't need to call this yourself, since it's called by
|
2012-03-24 12:15:11 -05:00
|
|
|
:func:`edbob.init()` whenever the config file includes a timezone (but
|
|
|
|
only as long as ``edbob.time`` is configured to be initialized)::
|
|
|
|
|
2012-03-07 06:04:18 -06:00
|
|
|
[edbob]
|
2012-03-24 20:53:24 -05:00
|
|
|
init = edbob.time
|
2012-03-24 12:15:11 -05:00
|
|
|
|
|
|
|
[edbob.time]
|
2012-03-07 06:04:18 -06:00
|
|
|
timezone = US/Central
|
|
|
|
"""
|
|
|
|
|
2012-03-24 12:15:11 -05:00
|
|
|
global timezone
|
2012-03-07 06:04:18 -06:00
|
|
|
|
|
|
|
if tz is None:
|
2012-03-24 12:15:11 -05:00
|
|
|
timezone = None
|
2012-03-07 06:04:18 -06:00
|
|
|
else:
|
2012-03-24 12:15:11 -05:00
|
|
|
timezone = pytz.timezone(tz)
|
2012-03-20 09:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
def utc_time(timestamp=None):
|
|
|
|
"""
|
|
|
|
Returns a timestamp whose ``tzinfo`` member is set to the UTC timezone.
|
|
|
|
|
|
|
|
If ``timestamp`` is not provided, then ``datetime.datetime.utcnow()`` will
|
|
|
|
be called to obtain the value.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if timestamp is None:
|
|
|
|
timestamp = datetime.datetime.utcnow()
|
|
|
|
return pytz.utc.localize(timestamp)
|