This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: Added date helper functions. (#57)
- now: Returns the proper datetime.datetime instance - formatted_dt: Formats a datetime.datetime, or uses now if none is provided
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |