FormattedDateToEpoch

Converts a custom-formatted timestamp to UNIX epoch time. Use it to convert custom time stamps to a XSOAR date field. If you pass formatter argument, we will use it to transform. If not, we will use dateparser.parse for transforming. For more info, see: https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior

python · Filters And Transformers

Source

from datetime import UTC, datetime

import dateparser
import demistomock as demisto


def date_to_epoch(date: str, formatter: str | None = None) -> int:
    epoch = datetime(1970, 1, 1, tzinfo=UTC)
    date_obj = (
        datetime.strptime(date, formatter)
        if formatter
        else dateparser.parse(date, settings={"RELATIVE_BASE": datetime(1900, 1, 1)})
    )
    assert date_obj is not None, f"could not parse {date}"
    return int(date_obj.strftime("%s") if date_obj.tzinfo is None else (date_obj - epoch).total_seconds())


def main():
    args = demisto.args()
    date_value = args["value"]
    formatter = args.get("formatter")
    demisto.results(date_to_epoch(date_value, formatter))


# python2 uses __builtin__ python3 uses builtins
if __name__ == "__builtin__" or __name__ == "builtins":
    main()

README

Converts a custom-formatted timestamp to UNIX epoch time. Use it to convert custom time stamps to a XSOAR date field. If you pass formatter argument, we will use it to transform. If not, we will use dateparser.parse for transforming. For more info, see: https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior

Script Data


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

Inputs


Argument Name Description
value Time stamp to convert
formatter Python ‘strptime’ formatter string

Outputs


There are no outputs for this script.