ConvertDatetoUTC

Converts a date from a different timezone to UTC timezone.

python · Common Scripts

Details

IDConvertDatetoUTC
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility

README

Converts a date from a different timezone to UTC timezone

Script Data


Name Description
Script Type python3
Tags Utility

Inputs


Argument Name Description
date Date to convert to UTC
date_format Default is “%Y-%m-%dT%H:%M:%S”
timezone Timezone accepted by pytz. Default is America/Los_Angeles

Outputs


Path Description Type
UTCDate Converted Date in UTC timezone Unknown
UTCDateEpoch Date in Epoch (Unix time) Unknown
import demistomock as demisto  # noqa: F401
import pytz
from CommonServerPython import *  # noqa: F401

""" MAIN FUNCTION """


def main():
    try:
        date = demisto.getArg("date")
        date_format = demisto.getArg("date_format")
        time_zone = demisto.getArg("timezone")

        time_zone = pytz.timezone(time_zone)

        # Initialize datetime
        date = datetime.strptime(date, date_format)

        # Convert to timezone aware date
        localized_date = time_zone.localize(date)

        # Convert to UTC timezone
        utc_converted_date = localized_date.astimezone(pytz.timezone("UTC"))
        epoch_time = utc_converted_date.strftime("%s")

        # Initialize entry context to return
        entry_context = {
            "UTCDate": utc_converted_date.strftime(date_format),
            "UTCDateEpoch": epoch_time,
        }

        return_results(
            {
                "Contents": json.dumps(entry_context),
                "ContentsFormat": formats["json"],
                "EntryContext": entry_context,
            }
        )

    except Exception as exc:
        demisto.error(traceback.format_exc())  # print the traceback
        return_error(f"Failed to execute ConvertDateToUTC. Error: {exc!s}")


""" ENTRY POINT """


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