ConvertTimezoneFromUTC

Takes UTC and converts it to the specified timezone. Format must match the UTC date's format and output will be the same format. Can use in conjunction with ConvertDateToString.

python · Common Scripts

Source

from traceback import format_exc

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


def determine_correct_format(time: str, fmt: str) -> datetime:
    time_as_datetime = datetime.strptime(time, fmt)
    return time_as_datetime


def convert_UTC_Timezone_command(time: datetime, timezone: str, fmt: str) -> str:
    desired_timezone = pytz.timezone(timezone)

    # convert me to desired timezone
    desired_time = time.astimezone(desired_timezone).strftime(fmt)
    return desired_time


def main():  # pragma: no cover
    try:
        # Get Args
        args = demisto.args()
        str_utc_time = args.get("value")
        requested_timezone = args.get("timezone")
        fmt = args.get("format")

        # Convert UTC time string to a datetime type
        utc_time = determine_correct_format(time=str_utc_time, fmt=fmt)

        # Convert to requested Timezone and format
        return_results(convert_UTC_Timezone_command(time=utc_time, timezone=requested_timezone, fmt=fmt))

    except Exception as e:
        demisto.error(format_exc())
        return_error(f"ConvertTimezone command failed. Error: {e}")


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

README

Takes UTC and converts it to the specified timezone. Format must match the UTC date’s format and output will be the same format. Can use in conjunction with ConvertDateToString

Script Data


Name Description
Script Type python3
Tags transformer, date

Inputs


Argument Name Description
value Time in UTC in the format specified
format Format that the input expects, and output will format to (i.e “%Y-%m-%d %H:%M:%S”)
timezone Timezone to be converted to (i.e. ‘US/Eastern’, ‘Etc/Greenwich’, ‘Canada/Eastern’). Review documentation on http://pytz.sourceforge.net/#helpers for more available timezones .

Outputs


There are no outputs for this script.