Skip to content

Commit 6c7ae0d

Browse files
authored
Added date_time utility, unit tests, and utility guide. (#33)
<!-- markdownlint-disable-next-line first-line-heading --> ## Description <!-- Added date_time utility to enable easy manipulation/formatting of datetime elements. Included unit tests, and a utility guide giving an overview of how to use the function. --> ## Context <!-- The change is required so that teams utilising the framework will have a function to assist them in writing tests as quickly as possible --> ## Type of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply. --> - [ ] Refactoring (non-breaking change) - [ x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I am familiar with the [contributing guidelines](../docs/CONTRIBUTING.md) - [x ] I have followed the code style of the project - [x] I have added tests to cover my changes - [x] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent a39d358 commit 6c7ae0d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Utility Guide: Date Time Utility
2+
3+
The Date Time Utility can be used to manipulate dates and times for various purposes,
4+
such as asserting timestamp values, changing the format of a date, or returning the day of the week for a given date.
5+
6+
## Using the Date Time Utility
7+
8+
To use the Date Time Utility, import the 'DateTimeUtils' class into your test file and then call the DateTimeUtils
9+
functions from within your tests, as required.
10+
11+
## Required arguments
12+
13+
The functions in this class require different arguments, including `datetime`, `str`, and `float`.
14+
Look at the docstrings for each function to see what arguments are required.
15+
The docstrings also specify when arguments are optional, and what the default values are when no argument is provided.
16+
17+
## Example usage
18+
19+
from tests_utils.date_time_utils import DateTimeUtils
20+
21+
def test_date_format(page: Page) -> None:
22+
expect(page.locator("#date")).to_contain_text(DateTimeUtils.current_datetime()))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from datetime import datetime, timedelta
2+
import utils.date_time_utils
3+
4+
5+
def test_current_datetime():
6+
dtu = utils.date_time_utils.DateTimeUtils()
7+
current_date = datetime.now()
8+
current_date1 = current_date.strftime("%d/%m/%Y %H:%M")
9+
current_date2 = current_date.strftime("%Y-%m-%d %H:%M")
10+
current_date3 = current_date.strftime("%d %B %Y %H:%M")
11+
assert dtu.current_datetime() == current_date.strftime("%d/%m/%Y %H:%M")
12+
assert dtu.current_datetime("%Y-%m-%d %H:%M") == current_date.strftime("%Y-%m-%d %H:%M")
13+
assert dtu.current_datetime("%d %B %Y %H:%M") == current_date.strftime("%d %B %Y %H:%M")
14+
15+
16+
def test_format_date():
17+
dtu = utils.date_time_utils.DateTimeUtils()
18+
date = datetime(2022, 12, 31)
19+
formatted_date1 = dtu.format_date(date, "%d/%m/%Y")
20+
formatted_date2 = dtu.format_date(date, "%Y/%m/%d")
21+
formatted_date3 = dtu.format_date(date, "%d %B %Y")
22+
assert dtu.format_date(date, "%d/%m/%Y") == "31/12/2022"
23+
assert dtu.format_date(date, "%Y/%m/%d") == "2022/12/31"
24+
assert dtu.format_date(date, "%d %B %Y") == "31 December 2022"
25+
26+
27+
def test_add_days():
28+
dtu = utils.date_time_utils.DateTimeUtils()
29+
date = datetime.now()
30+
new_date = dtu.add_days(date, 5)
31+
assert new_date == date + timedelta(days=5)
32+
33+
34+
def test_get_day_of_week_for_today():
35+
dtu = utils.date_time_utils.DateTimeUtils()
36+
date = datetime.now()
37+
day_of_week = dtu.get_a_day_of_week(date)
38+
assert day_of_week in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
39+
40+
41+
def test_get_a_day_of_week():
42+
dtu = utils.date_time_utils.DateTimeUtils()
43+
date = datetime(2023, 11, 8)
44+
day_of_week = dtu.get_a_day_of_week(date)
45+
assert day_of_week in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

utils/date_time_utils.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from datetime import datetime, timedelta
2+
3+
4+
class DateTimeUtils:
5+
def __init__(self):
6+
pass
7+
8+
@staticmethod
9+
def current_datetime(format_date: str = "%d/%m/%Y %H:%M") -> str:
10+
"""Gets the current datetime in the specified format.
11+
12+
Args:
13+
format_date (str): [Optional] The format to return the current datetime in. Defaults to dd/mm/yyyy hh:mm if not provided.
14+
15+
Returns:
16+
current_date (str): The current datetime in the specified format.
17+
18+
"""
19+
return datetime.now().strftime(format_date)
20+
21+
@staticmethod
22+
def format_date(date: datetime, format_date: str = "%d/%m/%Y") -> str:
23+
"""Formats a specified datetime object.
24+
25+
Args:
26+
date (datetime): The date to be formatted.
27+
format_date (str): [Optional] The format to return the datetime in. Defaults to dd/mm/yyyy if not provided.
28+
29+
Returns:
30+
format_date (str): The formatted date in the specified format.
31+
32+
"""
33+
return date.strftime(format_date)
34+
35+
@staticmethod
36+
def add_days(date: datetime, days: float) -> datetime:
37+
"""Adds a specified number of days to a specified date.
38+
39+
Args:
40+
date (datetime): The date to which the days will be added.
41+
days (float): The number of days to add to the specified date.
42+
43+
Returns:
44+
new_date (datetime): The specified date plus the number of specified days (year, month, day, hour, minute, second, microsecond).
45+
46+
"""
47+
return date + timedelta(days=days)
48+
49+
@staticmethod
50+
def get_day_of_week_for_today(date: datetime) -> str:
51+
"""Gets the day of the week (e.g., Monday, Tuesday) from the specified date.
52+
53+
Args:
54+
date (datetime): The current date using the now function
55+
56+
Returns:
57+
day_of_week (str): The day of the week relating to the specified date.
58+
59+
"""
60+
return date.strftime("%A")
61+
62+
@staticmethod
63+
def get_a_day_of_week(date: datetime) -> str:
64+
"""Gets the day of the week (e.g., Monday, Tuesday) from the specified date.
65+
66+
Args:
67+
date (datetime): The date for which the day of the week will be returned.
68+
69+
Returns:
70+
day_of_week (str): The day of the week relating to the specified date.
71+
72+
"""
73+
return date.strftime("%A")

0 commit comments

Comments
 (0)