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

Details

IDConvertTimezoneFromUTC
Languagepython
From Version6.0.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagstransformer date

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.

from datetime import datetime

from ConvertTimezoneFromUTC import convert_UTC_Timezone_command, determine_correct_format


def test_convert_UTC_Timezone_command():
    """
    Given:
       Specific UTC time, timezone and format to convert
    When:
        The time is a datetime obj
    Then:
        Validate the result is correct and in local time format.
    """
    timezone = "US/Eastern"
    format = "%Y-%m-%d %H:%M:%S"
    time_as_datetime_type = datetime(2023, 1, 4, 18, 14, 18)

    result = convert_UTC_Timezone_command(time=time_as_datetime_type, timezone=timezone, fmt=format)
    # Note: This test will fail locally, due to time differences. It will pass in the build.
    assert result == "2023-01-04 13:14:18"


def test_determine_correct_format():
    """
    Given:
       A time as a string
    When:
        Determine the timezone
    Then:
        Validate the result is a correct datetime object.
    """
    value = "2023-01-04 18:14:18"
    format = "%Y-%m-%d %H:%M:%S"
    time_as_datetime_type = determine_correct_format(time=value, fmt=format)
    assert str(type(time_as_datetime_type)) == "<class 'datetime.datetime'>"