Adding Time to a Timestamp Value
In this example, we will add a time duration in minutes to the value in a Timestamp column.
Consider the following sample Event, where the timestamp value (in milliseconds) is in the dateFrom
column:
Note: A timestamp is a 10-digit number if it is in seconds and a 13-digit number if it is in milliseconds.
{
"event_name": "API",
"properties": {
"dateFrom": 1665619200000
}
}
The following Transformation script:
-
Adds 60 minutes to the value in the
dateFrom
column. -
Writes the changed value to the new column,
updated_time
.
from io.hevo.api import Event
def transform(event):
# Get event name from the event #
eventName = event.getEventName()
# Get properties from the event #
properties = event.getProperties()
time_to_add = 60*60*1000
added_time = properties['dateFrom'].getTime() + time_to_add
properties['updated_time'] = added_time
return event
Note: If your timestamp value is in seconds, change the value of the time_to_add
variable in the script above to 60*60.
The output from the above snippet is:
{
"event_name": "API",
"properties": {
"dateFrom": 1665422736000,
"updated_time": 1665426336000
}
}
The updated_time
field is the timestamp value, in milliseconds, obtained after adding 60 minutes to dateFrom
. The human-readable format of both timestamp values is:
Field Name | Timestamp Value (in milliseconds) | Human-readable Time (in GMT) |
---|---|---|
dateFrom |
1665422736000 | Monday, October 10, 2022 5:25:36 PM |
updated_time |
1665426336000 | Monday, October 10, 2022 6:25:36 PM |