Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
util: Added date helper functions. (#57)
Browse files Browse the repository at this point in the history
- now: Returns the proper datetime.datetime instance
- formatted_dt: Formats a datetime.datetime, or uses now if none is
  provided
  • Loading branch information
ashcrow authored and mbarnes committed Nov 16, 2016
1 parent 8e3cf4f commit c60a70d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/apidoc/commissaire.util.date.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commissaire.util.date module
============================

.. automodule:: commissaire.util.date
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions doc/apidoc/commissaire.util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Submodules
.. toctree::

commissaire.util.config
commissaire.util.date
commissaire.util.logging
commissaire.util.ssh

Expand Down
45 changes: 45 additions & 0 deletions src/commissaire/util/date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Date/Time related utilities.
"""

import datetime

from commissaire import constants as C


def now():
"""
Returns the date and time right now.
:returns: The date and time right now.
:rtype: datetime.datetime
"""
return datetime.datetime.utcnow()


def formatted_dt(dt=None):
"""
Returns the date and time in the format expected by all components.
:param dt: Optional datetime to format.
:type dt: datetime.datetime
:returns: The date and time in the expected format.
:rtype: str
"""
if dt is None:
dt = now()
return datetime.datetime.strftime(dt, C.DATE_FORMAT)
67 changes: 67 additions & 0 deletions test/test_util_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test cases for the commissaire.util.date module.
"""

from unittest import mock

from . import TestCase

from commissaire.util import date


#: datetime instance to use in tests
DT = date.datetime.datetime.utcnow()
#: isoformat of DT used in tests
ISOFORMAT = DT.isoformat()


class Test_now(TestCase):
"""
Tests the now function.
"""

def test_now(self):
"""
Test the now function.
"""
with mock.patch('datetime.datetime') as _dt:
_dt.utcnow.return_value = DT
self.assertEquals(DT, date.now())


class Test_formatted_dt(TestCase):
"""
Tests the formatted_dt function.
"""

def test_formatted_dt(self):
"""
Test the formatted_dt function with no input.
"""
with mock.patch('datetime.datetime') as _dt:
_dt.utcnow.return_value = DT
_dt.strftime.return_value = ISOFORMAT
self.assertEquals(ISOFORMAT, date.formatted_dt())

def test_formatted_dt_with_datetime(self):
"""
Test the formatted_dt function with a datetime.
"""
with mock.patch('datetime.datetime') as _dt:
_dt.utcnow.return_value = DT
_dt.strftime.return_value = ISOFORMAT
self.assertEquals(ISOFORMAT, date.formatted_dt(DT))

0 comments on commit c60a70d

Please sign in to comment.