API Reference - Transformation TimeUtils

The TimeUtils class in Transformations provides a few convenience functions for date and time related operations.

fromEpochToDate( int: epoch ) - datetime.date

Converts epoch in milliseconds to datetime.date object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()
    
    epoch = 1046649600000
    dateObject = TimeUtils.fromEpochToDate(epoch)
        ...

dateObject is an instance of datetime.date

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()
    
    epoch = 10466496.00000
    dateObject = TimeUtils.fromEpochToDate(epoch)
        ...

Error: TypeError: fromEpochToDate(): 1st arg can’t be coerced to long ….

fromEpochToDatetime( int: epoch ) - datetime.datetime

Converts epoch in milliseconds to datetime.datetime object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()
    
    epoch = 1046649600000
    datetimeObject = TimeUtils.fromEpochToDatetime(epoch)
        ...

datetimeObject is an instance of datetime.datetime

fromEpochToTime( int: epoch ) - datetime.time

Converts epoch in milliseconds to datetime.time object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()
    
    epoch = 1046649600000
    timeObject = TimeUtils.fromEpochToTime(epoch)
        ...

timeObject is an instance of datetime.time

Following few functions can be used for formatting of date
Pattern syntax has to follow Joda DateTimeFormatter.

Symbol Description Example
G era AD/BC
C Century of era ( >=0 ) 20
Y Year of era ( >=0 ) 1996
x weekyear 1996
w Week of weekyear 27
e Day of week 2
E Day of week (text) Tuesday; Tue
y Year 1996
D Day of year 189
M Month of year July; Jul; 07
d Day of month 10
a Halfday of day AM/PM
K Hour of halfday ( 0 - 11 ) 0
h Clockhour of halfday ( 1-12 ) 12
H Hour of day ( 0-23 ) 0
k Clock hour of day ( 1-24 ) 24
m Minute of hour 30
s Second of minute 55
S Fraction of second 978
z Time zone Pacific Standard Time; PST
Z Time zone offset/id -0800; -08:00; America/Los_Angeles
' Escape for text  
‘ ' Single quote  

changeDateFormat( string: dateString, string: dateFormat, string: requiredFormat ) - string

Parameters:

  • dateString - Date in string form.
  • dateFormat - format of dateString argument
  • requiredFormat - format to which the date string will be converted

Converts dateString which is in dateFormat to requiredFormat

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['formattedDate'] = TimeUtils.changeDateFormat(dateStr, formatOfDate, requiredFormat)
        ...

properties['formattedDate'] = 'January 2, 19. 09:00 AM'

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    requiredFormat = 'MqMqMqM d, yy. hh:mm a' # wrong format
    properties['formattedDate'] = TimeUtils.changeDateFormat(dateStr, formatOfDate, requiredFormat)
        ...

Error: Illegal pattern component: …

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00' # date string does not follow format
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    requiredFormat = 'MqMqMqM d, yy. hh:mm a'
    properties['formattedDate'] = TimeUtils.changeDateFormat(dateStr, formatOfDate, requiredFormat)
        ...

Error:
Invalid format: …

fromEpochToFormattedDate( int: epoch, string: requiredFormat ) - string

Parameters:

  • epoch - timestamp in milliseconds
  • requiredFormat - format to which the epoch timestamp will be converted

Returns: String representing epoch in the requiredFormat.

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    epoch = 1046649600000
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['formattedDate'] = TimeUtils.fromEpochToFormattedDate(epoch, requiredFormat)
        ...

properties['formattedDate'] = 'March 3, 03. 12:00 AM'

fromDateToFormattedDate( datetime.date: inputDate, string: requiredFormat ) - string

Parameters:

  • inputDate - datetime.date object
  • requiredFormat - format to which the date object will be converted

Returns: String representing the inputDate in the requiredFormat.

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateObject = properties['date'] # Date object from a source table
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['formattedDate'] = TimeUtils.fromDateToFormattedDate( dateObject, requiredFormat )
        ...

fromDatetimeToFormattedDate( datetime.datetime: inputDate, string: requiredFormat ) - string

Parameters:

  • inputDate - datetime.datetime object
  • requiredFormat - format to which the date object will be converted

Returns: String representing the inputDate in the requiredFormat.

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateObject = properties['dateTime'] # DateTime object from a source table
    requiredFormat = 'MMMM d, yy. hh:mm a'
    properties['formattedDate'] = TimeUtils.fromDatetimeToFormattedDate( dateObject, requiredFormat )
        ... 

parseDateStringToDate( string: dateString, string: dateFormat ) - datetime.date

Parameters:

  • dateString - Date in string form.
  • dateFormat - format of dateString argument

Returns: datetime.date object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    properties['dateObject'] = TimeUtils.parseDateStringToDate( dateString, dateFormat )
        ...

parseDateStringToDatetime( string: dateString, string: dateFormat ) - datetime.datetime

Parameters:

  • dateString - Date in string form
  • dateFormat - format of dateString argument

Returns: datetime.datetime object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    properties['dateTimeObject'] = TimeUtils.parseDateStringToDateTime( dateString, dateFormat )
        ...  

parseDateStringToTime( string: dateString, string: dateFormat ) - datetime.time

Parameters:

  • dateString - Date in string form
  • dateFormat - format of dateString argument

Returns: datetime.time object

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    dateStr = '01/02/2019 09:00:00 AM'
    formatOfDate = 'MM/dd/yyyy hh:mm:ss a'
    properties['timeObject'] = TimeUtils.parseDateStringToDateTime( dateString, dateFormat )
        ...   

getTimeDifference( integer: timeOne, integer: timeTwo, string: inputUnit, string: outputUnit )

OR

getTimeDifference( object: timeOne, object: timeTwo, string: outputUnit ) - integer

This function returns the difference between two times in desired time unit.
Time units that are supported

  • SECONDS
  • MILLISECONDS
  • MICROSECONDS
  • MINUTES
  • HOURS
  • DAYS
  • NANOSECONDS

Parameters:

  • timeOne - Number indicating time in inputUnit units
  • timeTwo - Number indicating time in inputUnit units
  • inputUnit - Time unit of timeOne and timeTwo
  • outputUnit - Required time unit of the difference in times

OR

  • timeOne -
    • datetime.datetime object
    • datetime.date object
    • datetime.time object
  • timeTwo -
    • datetime.datetime object
    • datetime.date object
    • datetime.time object
  • outputUnit - Required time unit of the difference in times

Returns: (integer) Difference between timeOne and timeTwo in outputUnit units.

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    timeOne = 1046649600000
    timeTwo = 1046649700000
    inputUnit = "MILLISECONDS"
    outputUnit = "SECONDS"
    properties['timeDifference'] = TimeUtils.getTimeDifference(timeOne, timeTwo, inputUnit, outputUnit)
        ...

properties['timeDifference'] = 100

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    timeOne = 1046649600000
    timeTwo = 1046649700000
    inputUnit = "MILLISECONDS"
    outputUnit = "SECONDsss" # time unit spelling mistake
    properties['timeDifference'] = TimeUtils.getTimeDifference(timeOne, timeTwo, inputUnit, outputUnit)
        ...

Error:
No enum constant java.util.concurrent.TimeUnit.

getDifferenceInMinutes( object: timeOne, object: timeTwo ) - integer

This function returns the difference between two times in minutes.
Parameters:

  • timeOne, timeTwo - Can take one of three types
    • int - epoch in milliseconds
    • datetime.datetime Object
    • datetime.date Object
    • datetime.time Object

Returns: (int) Difference between timeOne and timeTwo in minutes

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    timeOne = 1046649600000
    timeTwo = 1106649600000
    properties['timeDifference'] = TimeUtils.getDifferenceInMinutes(timeOne, timeTwo)
        ...  

properties['timeDifference'] = 1000001

getDifferenceInSeconds( object: timeOne, object: timeTwo ) - integer

This function returns the difference between two times in seconds.
Parameters:

  • timeOne, timeTwo - Can take one of three types
    • int - epoch in milliseconds
    • datetime.datetime Object
    • datetime.date Object
    • datetime.time Object

Returns: (int) Difference between timeOne and timeTwo in seconds

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    timeOne = 1046649600000
    timeTwo = 1046659600000
    properties['timeDifference'] = TimeUtils.getDifferenceInSeconds(timeOne, timeTwo)
        ...  

properties['timeDifference'] = 10100

getDifferenceInMillis( object: timeOne, object: timeTwo ) - integer

This function returns the difference between two times in milliseconds.
Parameters:

  • timeOne, timeTwo - Can take one of three types
    • int - epoch in milliseconds
    • datetime.datetime Object
    • datetime.date Object
    • datetime.time Object

Returns: (int) Difference between timeOne and timeTwo in milliseconds

from io.hevo.api import Event
from io.hevo.api import TimeUtils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties() 
    
    timeOne = 1046649600000
    timeTwo = 1046649700000
    properties['timeDifference'] = TimeUtils.getDifferenceInMillis(timeOne, timeTwo)
        ...  

properties['timeDifference'] = 100000