PYDATETIME( ) function

Returns a datetime value calculated by a function in an external Python script. Data processing in Python is external to Analytics.

Syntax

PYDATETIME("PyFile,PyFunction" <, field|value <,...n>>)

Parameters

Name Type Description
PyFile,PyFunction

character

The name of the Python script to run followed by a comma and then the name of the function that returns the value:

 "myScript,myFunction"

When specifying the Python script, omit the file extension. The function you call may call other functions within the script or within other scripts, however all scripts that run must be placed inside a folder in the PYTHONPATH system environment variable prior to running.

For more information, see Configuring Python for use with Analytics.

Note

Your PyFunction must return a Python datetime object.

field |value <,...n>

optional

character

numeric

datetime

logical

This list of fields, expressions, or literal values to use as arguments for the Python function. The values are passed into the function you call in the order you specify them.

You may include as many arguments as necessary to satisfy the function definition in the Python script.

Note

Use the ALLTRIM() function to remove any leading or trailing spaces from character input: ALLTRIM(str). For more information, see ALLTRIM( ) function.

Output

Datetime.

Examples

Basic examples

Returns `20170101t0500`:

PYDATETIME("hello, combine_date_time", `20170101`, `t0500`)

External Python script that accepts a date argument and a time argument and returns a combined Datetime object:

# hello.py content
from datetime import datetime

def combine_date_time(d,t):
    return datetime.combine(d,t)

Advanced examples

Adding time to a datetime

Returns `20160101t2230`:

PYDATETIME("hello,add_time", `20160101 150000`, `t073000`)

External Python script that accepts a datetime and a time and adds the time to the datetime: 2016-01-01 15:00:00 + 7 hours, 30 minutes, 00 seconds = 2016-01-01 22:30:00.

# hello.py content
from datetime import timedelta
from datetime import datetime
from datetime import time
def add_time(start, time_to_add):
    return start + timedelta(hours=time_to_add.hour, minutes=time_to_add.minute, seconds=time_to_add.second)
ACL Scripting Guide 14.1