Utils Module

utils.utils

TcEx Utilities Module

class tcex.utils.utils.Utils(tcex=None)[source]

TcEx framework Utils module

any_to_datetime(time_input, tz=None)[source]

Return datetime object from multiple formats.

Formats:

  1. Human Input (e.g 30 days ago, last friday)
  2. ISO 8601 (e.g. 2017-11-08T16:52:42Z)
  3. Loose Date format (e.g. 2017 12 25)
  4. Unix Time/Posix Time/Epoch Time (e.g. 1510686617 or 1510686617.298753)
Parameters:
  • time_input (string) – The time input string (see formats above).
  • tz (string) – The time zone for the returned data.
Returns:

Python datetime.datetime object.

Return type:

(datetime.datetime)

date_to_datetime(time_input, tz=None)[source]

Convert ISO 8601 and other date strings to datetime.datetime type.

Parameters:
  • time_input (string) – The time input string (see formats above).
  • tz (string) – The time zone for the returned data.
Returns:

Python datetime.datetime object.

Return type:

(datetime.datetime)

format_datetime(time_input, tz=None, date_format=None)[source]

Return timestamp from multiple input formats.

Formats:

  1. Human Input (e.g 30 days ago, last friday)
  2. ISO 8601 (e.g. 2017-11-08T16:52:42Z)
  3. Loose Date format (e.g. 2017 12 25)
  4. Unix Time/Posix Time/Epoch Time (e.g. 1510686617 or 1510686617.298753)

Note

To get a unix timestamp format use the strftime format %s. Python does not natively support %s, however this method has support.

Parameters:
  • time_input (string) – The time input string (see formats above).
  • tz (string) – The time zone for the returned data.
  • date_format (string) – The strftime format to use, ISO by default.
Returns:

Formatted datetime string.

Return type:

(string)

human_date_to_datetime(time_input, tz=None, source_datetime=None)[source]
Convert human readable date (e.g. 30 days ago) to datetime.datetime using
parsedatetime module.

Examples:

  • August 25th, 2008
  • 25 Aug 2008
  • Aug 25 5pm
  • 5pm August 25
  • next saturday
  • tomorrow
  • next thursday at 4pm
  • at 4pm
  • eod
  • tomorrow eod
  • eod tuesday
  • eoy
  • eom
  • in 5 minutes
  • 5 minutes from now
  • 5 hours before now
  • 2 hours before noon
  • 2 days from tomorrow
Parameters:
  • time_input (string) – The time input string (see formats above).
  • tz (string) – The time zone for the returned datetime.
  • source_datetime (datetime.datetime) – The reference or source datetime.
Returns:

Python datetime.datetime object.

Return type:

(datetime.datetime)

inflect

Return instance of inflect.

timedelta(time_input1, time_input2)[source]

Calculates time delta between two time expressions.

Parameters:
  • time_input1 (string) – The time input string (see formats above).
  • time_input2 (string) – The time input string (see formats above).
Returns:

Dict with delta values.

Return type:

(dict)

static to_bool(value)[source]

Convert string value to bool.

static unix_time_to_datetime(time_input, tz=None)[source]
Convert (unix time|epoch time|posix time) in format of 1510686617 or 1510686617.298753
to datetime.datetime type.

Note

This method assumes UTC for all inputs.

Note

This method only accepts a 9-10 character time_input.

Parameters:
  • time_input (string) – The time input string (see formats above).
  • tz (string) – The time zone for the returned datetime (e.g. UTC).
Returns:

Python datetime.datetime object.

Return type:

(datetime.datetime)

write_temp_binary_file(content, filename=None)[source]

Write content to a temporary file.

Parameters:
  • content (bytes) – The file content.
  • filename (str, optional) – The filename to use when writing the file.
Returns:

Fully qualified path name for the file.

Return type:

str

write_temp_file(content, filename=None, mode='w')[source]

Write content to a temporary file.

Parameters:
  • content (bytes|str) – The file content. If passing binary data the mode needs to be set to ‘wb’.
  • filename (str, optional) – The filename to use when writing the file.
  • mode (str, optional) – The file write mode which could be either ‘w’ or ‘wb’.
Returns:

Fully qualified path name for the file.

Return type:

str

utils.date_utils