Skip to content

Commit

Permalink
utils: Add new datetime_format module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Espen A. Kristiansen committed Aug 30, 2018
1 parent 54ec2d1 commit c380e32
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ievv_opensource/utils/datetime_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import arrow
from django.conf import settings
from django.template import defaultfilters


def format_datetime_in_timezone(datetime_object, datetime_format='DATETIME_FORMAT', timezone=settings.TIME_ZONE):
"""
Format a datetime object in a timezone.
Args:
datetime_object (datetime.datetime): Datetime object to format.
datetime_format (str): A Django datetime formatting string name, such as ``"DATETIME_FORMAT"``,
``"SHORT_DATETIME_FORMAT``", ``"DATE_FORMAT"``, ...
timezone (str): Defaults to ``settings.TIME_ZONE``. The datetime is converted to
this timezone. So if you use UTC in the database, and want to present another
timezone, this will convert it correctly.
Returns:
str: The formatted datetime.
"""
return defaultfilters.date(arrow.get(datetime_object).to(timezone).datetime, datetime_format)
1 change: 1 addition & 0 deletions not_for_deploy/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Utilities
utils.validate_redirect_url
utils.testhelpers
utils.validation_error_util
utils.datetime_format


************
Expand Down
8 changes: 8 additions & 0 deletions not_for_deploy/docs/releasenotes/releasenotes-5.10.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##################################
ievv_opensource 5.10.0 releasenotes
##################################

************
New features
************
- New ``utils.datetime_format`` module - see :doc:`/utils.datetime_format`.
6 changes: 6 additions & 0 deletions not_for_deploy/docs/utils.datetime_format.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
##############################################################
`utils.datetime_format` --- Utilities for formatting datetimes
##############################################################

.. currentmodule:: ievv_opensource.utils.datetime_format
.. automodule:: ievv_opensource.utils.datetime_format
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'termcolor',
'psutil',
'future',
'arrow',
],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit c380e32

Please sign in to comment.