|
1 | 1 | # Utility Guide: Date Time Utility |
2 | 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. |
| 3 | +The Date Time Utility provides a set of helper functions for manipulating dates and times in your tests. |
| 4 | +You can use it to assert timestamp values, change the format of a date, calculate date differences, add or subtract time, and more. |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | + |
| 8 | +- [Utility Guide: Date Time Utility](#utility-guide-date-time-utility) |
| 9 | + - [Table of Contents](#table-of-contents) |
| 10 | + - [Using the Date Time Utility](#using-the-date-time-utility) |
| 11 | + - [Main Features](#main-features) |
| 12 | + - [Example Usage](#example-usage) |
| 13 | + - [Method Reference](#method-reference) |
| 14 | + - [`current_datetime(format_date: str = "%d/%m/%Y %H:%M") -> str`](#current_datetimeformat_date-str--dmy-hm---str) |
| 15 | + - [`format_date(date: datetime, format_date: str = "%d/%m/%Y") -> str`](#format_datedate-datetime-format_date-str--dmy---str) |
| 16 | + - [`add_days(date: datetime, days: float) -> datetime`](#add_daysdate-datetime-days-float---datetime) |
| 17 | + - [`get_day_of_week_for_today(date: datetime) -> str`](#get_day_of_week_for_todaydate-datetime---str) |
| 18 | + - [`get_a_day_of_week(date: datetime) -> str`](#get_a_day_of_weekdate-datetime---str) |
| 19 | + - [`report_timestamp_date_format() -> str`](#report_timestamp_date_format---str) |
| 20 | + - [`fobt_kits_logged_but_not_read_report_timestamp_date_format() -> str`](#fobt_kits_logged_but_not_read_report_timestamp_date_format---str) |
| 21 | + - [`screening_practitioner_appointments_report_timestamp_date_format() -> str`](#screening_practitioner_appointments_report_timestamp_date_format---str) |
| 22 | + - [`month_string_to_number(string: str) -> int`](#month_string_to_numberstring-str---int) |
5 | 23 |
|
6 | 24 | ## Using the Date Time Utility |
7 | 25 |
|
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. |
| 26 | +To use the Date Time Utility, import the `DateTimeUtils` class from `utils.date_time_utils` into your test file and call its methods as needed. |
| 27 | + |
| 28 | +```python |
| 29 | +from utils.date_time_utils import DateTimeUtils |
| 30 | +``` |
| 31 | + |
| 32 | +## Main Features |
| 33 | + |
| 34 | +- Get the current date/time in various formats |
| 35 | +- Format dates to different string representations |
| 36 | +- Add or subtract days from a date |
| 37 | +- Get the day of the week for a given date |
| 38 | +- Get formatted timestamps for different report types |
| 39 | +- Convert a month name string to its corresponding number |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Example Usage |
| 44 | + |
| 45 | +```python |
| 46 | +from utils.date_time_utils import DateTimeUtils |
| 47 | +from datetime import datetime |
| 48 | + |
| 49 | +# Get the current date and time as a string |
| 50 | +now_str = DateTimeUtils.current_datetime() |
| 51 | +print(now_str) # e.g. '28/05/2025 14:30' |
| 52 | + |
| 53 | +# Format a datetime object as 'dd/mm/yyyy' |
| 54 | +formatted = DateTimeUtils.format_date(datetime(2025, 1, 16)) |
| 55 | +print(formatted) # '16/01/2025' |
| 56 | + |
| 57 | +# Add 5 days to a date |
| 58 | +future_date = DateTimeUtils.add_days(datetime(2025, 1, 16), 5) |
| 59 | +print(future_date) # datetime object for 21/01/2025 |
| 60 | + |
| 61 | +# Get the day of the week for a date |
| 62 | +weekday = DateTimeUtils.get_day_of_week_for_today(datetime(2025, 1, 16)) |
| 63 | +print(weekday) # 'Thursday' |
| 64 | + |
| 65 | +# Get the day of the week (alias method) |
| 66 | +weekday2 = DateTimeUtils.get_a_day_of_week(datetime(2025, 1, 16)) |
| 67 | +print(weekday2) # 'Thursday' |
| 68 | + |
| 69 | +# Get the current timestamp in report format |
| 70 | +report_timestamp = DateTimeUtils.report_timestamp_date_format() |
| 71 | +print(report_timestamp) # e.g. '28/05/2025 at 14:30:00' |
| 72 | + |
| 73 | +# Get the current timestamp in FOBT kits report format |
| 74 | +fobt_timestamp = DateTimeUtils.fobt_kits_logged_but_not_read_report_timestamp_date_format() |
| 75 | +print(fobt_timestamp) # e.g. '28 May 2025 14:30:00' |
| 76 | + |
| 77 | +# Get the current timestamp in screening practitioner appointments report format |
| 78 | +spa_timestamp = DateTimeUtils.screening_practitioner_appointments_report_timestamp_date_format() |
| 79 | +print(spa_timestamp) # e.g. '28.05.2025 at 14:30:00' |
| 80 | + |
| 81 | +# Convert a month name to its number |
| 82 | +month_num = DateTimeUtils().month_string_to_number("February") |
| 83 | +print(month_num) # 2 |
| 84 | + |
| 85 | +month_num_short = DateTimeUtils().month_string_to_number("Sep") |
| 86 | +print(month_num_short) # 9 |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Method Reference |
| 92 | + |
| 93 | +### `current_datetime(format_date: str = "%d/%m/%Y %H:%M") -> str` |
| 94 | + |
| 95 | +Returns the current datetime as a string in the specified format. |
| 96 | + |
| 97 | +### `format_date(date: datetime, format_date: str = "%d/%m/%Y") -> str` |
| 98 | + |
| 99 | +Formats a given `datetime` object as a string. |
| 100 | + |
| 101 | +### `add_days(date: datetime, days: float) -> datetime` |
| 102 | + |
| 103 | +Adds a specified number of days to a given date. |
| 104 | + |
| 105 | +### `get_day_of_week_for_today(date: datetime) -> str` |
| 106 | + |
| 107 | +Returns the day of the week for the given date. |
| 108 | + |
| 109 | +### `get_a_day_of_week(date: datetime) -> str` |
| 110 | + |
| 111 | +Alias for `get_day_of_week_for_today`. |
| 112 | + |
| 113 | +### `report_timestamp_date_format() -> str` |
| 114 | + |
| 115 | +Returns the current date and time in the format `'dd/mm/yyyy at hh:mm:ss'`. |
| 116 | + |
| 117 | +### `fobt_kits_logged_but_not_read_report_timestamp_date_format() -> str` |
| 118 | + |
| 119 | +Returns the current date and time in the format `'dd Mon yyyy hh:mm:ss'`. |
| 120 | + |
| 121 | +### `screening_practitioner_appointments_report_timestamp_date_format() -> str` |
10 | 122 |
|
11 | | -## Required arguments |
| 123 | +Returns the current date and time in the format `'dd.mm.yyyy at hh:mm:ss'`. |
12 | 124 |
|
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. |
| 125 | +### `month_string_to_number(string: str) -> int` |
16 | 126 |
|
17 | | -## Example usage |
| 127 | +Converts a month name (full or short, case-insensitive) to its corresponding number (1-12). |
18 | 128 |
|
19 | | - from tests_utils.date_time_utils import DateTimeUtils |
| 129 | +--- |
20 | 130 |
|
21 | | - def test_date_format(page: Page) -> None: |
22 | | - expect(page.locator("#date")).to_contain_text(DateTimeUtils.current_datetime()) |
| 131 | +Refer to the source code and function docstrings in `utils/date_time_utils.py` for more details and any additional helper methods. |
0 commit comments