DateStringToISOFormat

This is a thin wrapper around the `dateutil.parser.parse` function. It will parse a string containing a date/time stamp and return it in ISO 8601 format.

python · Filters And Transformers

Source

from datetime import UTC

import demistomock as demisto
from dateutil.parser import ParserError, parse  # type: ignore[attr-defined]


def parse_datestring_to_iso(
    date_value: str, day_first: bool, year_first: bool, fuzzy: bool, add_utc_timezone: bool = False
) -> str:
    try:
        datetime_obj = parse(date_value, dayfirst=day_first, yearfirst=year_first, fuzzy=fuzzy)
        if add_utc_timezone and not datetime_obj.tzinfo:
            datetime_obj = datetime_obj.replace(tzinfo=UTC)
        return datetime_obj.isoformat()
    except ParserError as e:
        demisto.error(f"ParserError occurred: {e}\n Returning the original date string.")
        date_string = date_value
    return date_string


def main():
    args = demisto.args()
    date_value = args.get("value")
    day_first = args.get("dayfirst", "True").lower() == "true"
    year_first = args.get("yearfirst", "False").lower() == "true"
    fuzzy = args.get("fuzzy", "True").lower() == "true"
    add_utc_timezone = args.get("add_utc_timezone", "true").lower() == "true"
    iso_string = parse_datestring_to_iso(date_value, day_first, year_first, fuzzy, add_utc_timezone)
    demisto.results(iso_string)


# python2 uses __builtin__ python3 uses builtins
if __name__ in ("__builtin__", "builtins", "__main__"):
    main()

README

This is a thin wrapper around the dateutil.parser.parse function. It will parse a string containing a date/time stamp and return it in ISO 8601 format.

Script Data


Name Description
Script Type python3
Tags transformer, date
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
value Date value to convert.
dayfirst Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (``True``) or month (``False``). If ``yearfirst`` is set to ``True``, this distinguishes between YDM and YMD.
yearfirst Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the year. If ``True``, the first number is taken to be the year, otherwise the last number is taken to be the year.
fuzzy Whether to allow fuzzy parsing, allowing for string like “Today is January 1, 2047 at 8:21:00AM”.
add_utc_timezone Whether to add UTC timezone to the date string returned in case offset-naive date was provided as input.

Outputs


There are no outputs for this script.

Script Examples

Example command

!DateStringToISOFormat value="'05-11-2929'" dayfirst="true" yearfirst="true" fuzzy="true" add_utc_timezone="false"

Context Example

{}

Human Readable Output

2929-11-05T00:00:00