Skip to content

Util

Utility functions for Pact.

This module defines a number of utility functions that are used in specific contexts within the Pact library. These functions are not intended to be used directly by consumers of the library, but are still made available for reference.

Functions

strftime_to_simple_date_format(python_format: str) -> str

Convert a Python datetime format string to Java SimpleDateFormat format.

Python uses strftime codes which are ultimately based on the C strftime function. Java uses SimpleDateFormat codes which generally have corresponding codes, but with some differences.

Note that this function strictly supports codes explicitly defined in the Python documentation. Locale-dependent codes are not supported, and codes supported by the underlying C library but not Python are not supported. For examples, %c, %x, and %X are not supported as they are locale dependent, and %D is not supported as it is not part of the Python documentation (even though it may be supported by the underlying C and therefore work in some Python implementations).

PARAMETER DESCRIPTION
python_format

The Python datetime format string to convert.

TYPE: str

RETURNS DESCRIPTION
str

The equivalent Java SimpleDateFormat format string.

Source code in src/pact/v3/util.py
def strftime_to_simple_date_format(python_format: str) -> str:
    """
    Convert a Python datetime format string to Java SimpleDateFormat format.

    Python uses [`strftime`
    codes](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes)
    which are ultimately based on the C `strftime` function. Java uses
    [`SimpleDateFormat`
    codes](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
    which generally have corresponding codes, but with some differences.

    Note that this function strictly supports codes explicitly defined in the
    Python documentation. Locale-dependent codes are not supported, and codes
    supported by the underlying C library but not Python are not supported. For
    examples, `%c`, `%x`, and `%X` are not supported as they are locale
    dependent, and `%D` is not supported as it is not part of the Python
    documentation (even though it may be supported by the underlying C and
    therefore work in some Python implementations).

    Args:
        python_format:
            The Python datetime format string to convert.

    Returns:
        The equivalent Java SimpleDateFormat format string.
    """
    # Each Python format code is exactly two characters long, so we can
    # safely iterate through the string.
    idx = 0
    result: str = ""
    escaped = False

    while idx < len(python_format):
        c = python_format[idx]
        idx += 1

        if c == "%":
            c = python_format[idx]
            if escaped:
                result += "'"
                escaped = False
            result += _format_code_to_java_format(c)
            # Increment another time to skip the second character of the
            # Python format code.
            idx += 1
            continue

        if c == "'":
            # In Java, single quotes are used to escape characters.
            # To insert a single quote, we need to insert two single quotes.
            # It doesn't matter if we're in an escape sequence or not, as
            # Java treats them the same.
            result += "''"
            continue

        if not escaped and c.isalpha():
            result += "'"
            escaped = True
        result += c

    if escaped:
        result += "'"
    return result