Skip to content

Commit 484b95f

Browse files
committed
Added new, untested, dates module
1 parent aa27165 commit 484b95f

File tree

6 files changed

+220
-106
lines changed

6 files changed

+220
-106
lines changed

__pkginfo__.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright © 2019 Dominic Davis-Foster <[email protected]>
1+
# Copyright © 2020 Dominic Davis-Foster <[email protected]>
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -15,48 +15,43 @@
1515

1616
# This script based on https://github.com/rocky/python-uncompyle6/blob/master/__pkginfo__.py
1717

18-
copyright = """
18+
import pathlib
19+
20+
copyright = """
1921
2019 Dominic Davis-Foster <[email protected]>
2022
"""
2123

2224
VERSION = "0.1.21"
2325

24-
modname = "domdf_python_tools"
25-
py_modules = None
26-
entry_points = None
27-
28-
license = 'LGPL3'
29-
30-
short_desc = 'Helpful functions for Python'
31-
32-
classifiers = ['Development Status :: 4 - Beta',
33-
'Intended Audience :: Developers',
34-
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
35-
'Operating System :: OS Independent',
36-
'Programming Language :: Python',
37-
'Programming Language :: Python :: 3.6',
38-
'Programming Language :: Python :: 3.7',
39-
'Programming Language :: Python :: 3.8',
40-
'Topic :: Software Development :: Libraries :: Python Modules',
41-
]
42-
43-
author = "Dominic Davis-Foster"
44-
author_email = "[email protected]"
45-
github_username = "domdfcoding"
46-
web = github_url = f"https://github.com/{github_username}/{modname}"
26+
modname = "domdf_python_tools"
27+
py_modules = None
28+
entry_points = None
4729

48-
install_requires = []
30+
license = 'LGPLv3+'
4931

32+
short_desc = 'Helpful functions for Python'
5033

51-
import os.path
52-
def get_srcdir():
53-
filename = os.path.normcase(os.path.dirname(os.path.abspath(__file__)))
54-
return os.path.realpath(filename)
34+
classifiers = [
35+
'Development Status :: 4 - Beta',
36+
'Intended Audience :: Developers',
37+
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
38+
'Operating System :: OS Independent',
39+
'Programming Language :: Python',
40+
'Programming Language :: Python :: 3.6',
41+
'Programming Language :: Python :: 3.7',
42+
'Programming Language :: Python :: 3.8',
43+
'Topic :: Software Development :: Libraries :: Python Modules',
44+
]
5545

56-
srcdir = get_srcdir()
46+
author = "Dominic Davis-Foster"
47+
author_email = "[email protected]"
48+
github_username = "domdfcoding"
49+
web = github_url = f"https://github.com/{github_username}/{modname}"
5750

58-
def read(*rnames):
59-
return open(os.path.join(srcdir, *rnames)).read()
51+
install_requires = []
6052

6153
# Get info from files; set: long_description
62-
long_description = ( read("README.rst") + '\n' )
54+
if pathlib.Path.cwd().name == "doc-source":
55+
long_description = (pathlib.Path.cwd().parent / "README.rst").read_text() + '\n'
56+
else:
57+
long_description = pathlib.Path("README.rst").read_text() + '\n'

doc-source/conf.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
language = 'en'
3333

3434
extensions = [
35-
'sphinx.ext.intersphinx',
36-
'sphinx.ext.autodoc',
37-
'sphinx.ext.mathjax',
38-
'sphinx.ext.viewcode',
39-
'sphinxcontrib.httpdomain',
40-
]
35+
'sphinx.ext.intersphinx',
36+
'sphinx.ext.autodoc',
37+
'sphinx.ext.mathjax',
38+
'sphinx.ext.viewcode',
39+
'sphinxcontrib.httpdomain',
40+
]
4141

4242
templates_path = ['_templates']
4343
source_suffix = '.rst'
@@ -47,66 +47,67 @@
4747
suppress_warnings = ['image.nonlocal_uri']
4848
pygments_style = 'default'
4949

50-
intersphinx_mapping = { # Is this where those mystery links are specified?
51-
'rtd': ('https://docs.readthedocs.io/en/latest/', None),
52-
'sphinx': ('http://www.sphinx-doc.org/en/stable/', None),
53-
}
50+
intersphinx_mapping = {
51+
'rtd': ('https://docs.readthedocs.io/en/latest/', None),
52+
'sphinx': ('http://www.sphinx-doc.org/en/stable/', None),
53+
'python': ('https://docs.python.org/3/', None),
54+
}
5455

5556
html_theme = 'sphinx_rtd_theme'
5657
html_theme_options = {
57-
'logo_only': False, # True will show just the logo
58-
}
58+
'logo_only': False, # True will show just the logo
59+
}
5960
html_theme_path = ["../.."]
60-
#html_logo = "logo/pyms.png"
61-
html_show_sourcelink = False # True will show link to source
61+
# html_logo = "logo/pyms.png"
62+
html_show_sourcelink = False # True will show link to source
6263

6364
html_context = {
64-
# Github Settings
65-
"display_github": True, # Integrate GitHub
66-
"github_user": "domdfcoding", # Username
67-
"github_repo": "domdf_python_tools", # Repo name
68-
"github_version": "master", # Version
69-
"conf_py_path": "/", # Path in the checkout to the docs root
70-
}
65+
# Github Settings
66+
"display_github": True, # Integrate GitHub
67+
"github_user": "domdfcoding", # Username
68+
"github_repo": "domdf_python_tools", # Repo name
69+
"github_version": "master", # Version
70+
"conf_py_path": "/", # Path in the checkout to the docs root
71+
}
7172

7273
htmlhelp_basename = slug
7374

7475
latex_documents = [
75-
('index', '{0}.tex'.format(slug), project, author, 'manual'),
76-
]
76+
('index', '{0}.tex'.format(slug), project, author, 'manual'),
77+
]
7778

7879
man_pages = [
79-
('index', slug, project, [author], 1)
80-
]
80+
('index', slug, project, [author], 1)
81+
]
8182

8283
texinfo_documents = [
83-
('index', slug, project, author, slug, project, 'Miscellaneous'),
84-
]
84+
('index', slug, project, author, slug, project, 'Miscellaneous'),
85+
]
8586

8687

8788
# Extensions to theme docs
8889
def setup(app):
89-
from sphinx.domains.python import PyField
90-
from sphinx.util.docfields import Field
91-
92-
app.add_object_type(
93-
'confval',
94-
'confval',
95-
objname='configuration value',
96-
indextemplate='pair: %s; configuration value',
97-
doc_field_types=[
98-
PyField(
99-
'type',
100-
label=_('Type'),
101-
has_arg=False,
102-
names=('type',),
103-
bodyrolename='class'
104-
),
105-
Field(
106-
'default',
107-
label=_('Default'),
108-
has_arg=False,
109-
names=('default',),
110-
),
111-
]
112-
)
90+
from sphinx.domains.python import PyField
91+
from sphinx.util.docfields import Field
92+
93+
app.add_object_type(
94+
'confval',
95+
'confval',
96+
objname='configuration value',
97+
indextemplate='pair: %s; configuration value',
98+
doc_field_types=[
99+
PyField(
100+
'type',
101+
label=_('Type'),
102+
has_arg=False,
103+
names=('type',),
104+
bodyrolename='class'
105+
),
106+
Field(
107+
'default',
108+
label=_('Default'),
109+
has_arg=False,
110+
names=('default',),
111+
),
112+
]
113+
)

doc-source/docs.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
:private-members:
1010
:special-members:
1111

12+
==================================
13+
:mod:`domdf_python_tools.dates`
14+
==================================
15+
16+
.. warning:: This module has not been fully tested. Use with caution.
17+
18+
.. note:: This module requires the `pytz <https://pypi.org/project/pytz/>`_ package to be installed.
19+
20+
.. automodule:: domdf_python_tools.dates
21+
:members:
22+
:private-members:
23+
:special-members:
24+
1225
==================================
1326
:mod:`domdf_python_tools.doctools`
1427
==================================

domdf_python_tools/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
#
1717

1818

19-
import sys
20-
2119
from domdf_python_tools.utils import *
2220
from domdf_python_tools import paths, terminal, utils, doctools
2321

24-
__all__ = ["paths", "terminal", "utils"]
22+
__all__ = ["paths", "terminal", "utils", "dates"]
2523

2624
__author__ = "Dominic Davis-Foster"
2725
__copyright__ = "2014-2019 Dominic Davis-Foster"
2826

29-
__license__ = "LGPL"
27+
__license__ = "LGPLv3+"
3028
__version__ = "0.1.21"
3129
__email__ = "[email protected]"

domdf_python_tools/dates.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# dates.py
5+
"""
6+
Utilities for working with dates and times
7+
"""
8+
#
9+
# Copyright © 2020 Dominic Davis-Foster <[email protected]>
10+
#
11+
# Parts of the docstrings based on the Python 3.8.2 Documentation
12+
# Licensed under the Python Software Foundation License Version 2.
13+
# Copyright © 2001-2020 Python Software Foundation. All rights reserved.
14+
# Copyright © 2000 BeOpen.com . All rights reserved.
15+
# Copyright © 1995-2000 Corporation for National Research Initiatives . All rights reserved.
16+
# Copyright © 1991-1995 Stichting Mathematisch Centrum . All rights reserved.
17+
#
18+
# This program is free software; you can redistribute it and/or modify
19+
# it under the terms of the GNU Lesser General Public License as published by
20+
# the Free Software Foundation; either version 3 of the License, or
21+
# (at your option) any later version.
22+
#
23+
# This program is distributed in the hope that it will be useful,
24+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
25+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+
# GNU Lesser General Public License for more details.
27+
#
28+
# You should have received a copy of the GNU Lesser General Public License
29+
# along with this program; if not, write to the Free Software
30+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31+
# MA 02110-1301, USA.
32+
#
33+
34+
# stdlib
35+
import datetime
36+
37+
# 3rd party
38+
try:
39+
import pytz
40+
41+
def current_tzinfo():
42+
"""
43+
Returns a tzinfo object for the current timezone
44+
45+
:rtype: :class:`~python:datetime.datetime.tzinfo`
46+
"""
47+
48+
return datetime.datetime.now().astimezone().tzinfo
49+
50+
51+
def datetime_to_utc_timestamp(datetime, current_tzinfo=None):
52+
"""
53+
Convert a :class:`datetime.datetime` object to seconds since UNIX epoch, in UTC time
54+
55+
:param datetime:
56+
:type datetime: :class:`datetime.datetime`
57+
:param current_tzinfo: A tzinfo object representing the current timezone.
58+
If None it will be inferred.
59+
:type current_tzinfo: :class:`~python:datetime.datetime.tzinfo`
60+
61+
:return: Timestamp in UTC timezone
62+
:rtype: float
63+
"""
64+
65+
return datetime.astimezone(current_tzinfo).replace(tzinfo=pytz.UTC).timestamp()
66+
67+
68+
def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
69+
"""
70+
Convert UTC timestamp (seconds from UNIX epoch) to a :class:`datetime.datetime` object
71+
72+
If ``output_tz`` is None the timestamp is converted to the platform’s local date and time,
73+
and the local timezone is inferred and set for the object.
74+
75+
If ``output_tz`` is not None, it must be an instance of a :class:`~python:datetime.datetime.tzinfo` subclass,
76+
and the timestamp is converted to tz’s time zone.
77+
78+
79+
:param utc_timestamp: The timestamp to convert to a datetime object
80+
:type utc_timestamp: float, int
81+
:param output_tz: The timezone to output the datetime object for.
82+
If None it will be inferred.
83+
:type output_tz: :class:`~python:datetime.datetime.tzinfo`
84+
85+
:return: The timestamp as a datetime object.
86+
:rtype: :class:`datetime.datetime`
87+
88+
:raises: :class:`~python:OverflowError` if the timestamp is out of the range
89+
of values supported by the platform C localtime() or gmtime() functions,
90+
and OSError on localtime() or gmtime() failure. It’s common for this to
91+
be restricted to years in 1970 through 2038.
92+
"""
93+
94+
new_datetime = datetime.datetime.fromtimestamp(utc_timestamp, output_tz)
95+
96+
if output_tz is None:
97+
return new_datetime.astimezone()
98+
else:
99+
return new_datetime
100+
101+
except ImportError:
102+
import warnings
103+
warnings.warn("'domdf_python_tools.dates' requires pytz (https://pypi.org/project/pytz/), but it is not installed.")

0 commit comments

Comments
 (0)