ModifyDateTime

Takes a date or time input and adds or subtracts a determined amount of time. Returns a string in date or time in ISO Format.

python · Filters And Transformers

Source

import dateparser
import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401

from CommonServerUserPython import *  # noqa: E402 lgtm [py/polluting-import]


def apply_variation(original_datetime: datetime, variation: str) -> datetime | None:
    try:
        new_time = dateparser.parse(variation, settings={"RELATIVE_BASE": original_datetime})
        assert new_time is not None, f"could not parse {variation}"
        new_time = new_time.replace(tzinfo=original_datetime.tzinfo)
    except Exception as err:
        return_error(f"Error adding variation to the date / time - {err}")

    return new_time


def main():
    args = demisto.args()
    value = args.get("value")
    try:
        original_datetime = dateparser.parse(value)
    except Exception as err:
        return_error(f"Error with input date / time - {err}")

    variation = args.get("variation")
    new_time = apply_variation(original_datetime, variation)  # type: ignore
    if isinstance(new_time, datetime):
        return_results(new_time.isoformat())
    else:
        return_error("Invalid variation specified")


if __name__ in ("__main__", "builtin", "builtins"):
    main()

README

Takes a date or time input and adds or subtracts a determined amount of time. Returns a string in date or time in ISO Format.

Script Data


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

Inputs


Argument Name Description
value Input date or time in a format that is supported by the dateparser.parse() function as outlined here- https://dateparser.readthedocs.io/en/latest/#popular-formats. For example: ‘2020-01-01’ or ‘1999/02/03 12:01:59’
variation Variation of time (for example: ‘in 1 day’, or ‘3 months ago’). Must be supported by the dateparser.parse() function here - https://dateparser.readthedocs.io/en/latest/#relative-dates

Outputs


There are no outputs for this script.

Script Example

!ModifyDateTime value=2020,02,02 variation="1 day"

Context Example

{}

Human Readable Output

2020-02-01T00:00:00