xref: /openbmc/openbmc-test-automation/ffdc/plugins/date_time_utils.py (revision a64fa5fa6ba06a50b5ee9f193279a7bc8e5b20e6)
1#!/usr/bin/env python3
2
3r"""
4This module contains functions having to do with date time filter.
5"""
6
7from datetime import datetime
8
9
10def convert_string_dateime(date_str, date_format, desired_format):
11    r"""
12    Convert a date time string to the desired format.
13
14    This function converts a date time string to the desired format.
15    The function takes the date_str argument, which can be a single date time
16    string or a list of date time strings.
17
18    The function also accepts date_format and desired_format arguments, which
19    specify the input date time pattern and the desired output format,
20    respectively.
21
22    The function returns a list of date time strings in the desired format.
23
24    Parameters:
25        date_str (str or list): A date time string or a list of date time
26                                strings.
27        date_format (str):      The date time pattern of the input string(s).
28        desired_format (str):   The desired output format for the date time
29                                strings.
30
31    Returns:
32        list: A list of date time strings in the desired format.
33    """
34    if isinstance(date_str, list):
35        tmp_date = []
36        for date in date_str:
37            tmp_date.append(
38                datetime.strptime(date, date_format).strftime(desired_format)
39            )
40        return tmp_date
41    else:
42        return datetime.strptime(date_str, date_format).strftime(
43            desired_format
44        )
45