cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Ulises.Almaguer Guzman
AppDynamics Team

How do I automatically update event timestamps in an email template to a specific local time zone, accounting for daylight saving time? 

Automatically update event timestamps of the events in an email template to align with a specific time zone’s daylight-saving time changes. 

 

In this article...

 

SaaS controllers and their time zones 

SaaS customers rely on the time zone configured in the Controller to determine the offset between their local time zone and the one used by the Controller. Therefore, you need to manually modify the timestamp value in each template (email or HTTP) and keep it updated according to whether daylight saving is active or not. 

 

Why the need for an automatic script? 

Using an automatic script rather than relying on a manual process to update the timestamp value eliminates potential update gaps for twice-yearly daylight-saving time updates. 

If daylight-saving time is active, customers need to manually update the timestamp value, which can be error-prone and requires being done twice a year. 

 

Considerations and limitations

While this solution implements automated updates for US daylight saving time, you can use it as a template for daylight saving schedules in any other country. 

In the US: 

  • Daylight saving begins on the first Sunday of October at 2:00 a.m. 
  • Daylight saving ends on the second Sunday of March at 2:00 a.m. 

The code currently works for full-hour offsets, but it can be updated to work with partial-hour offsets. 

This implementation considers that the SaaS controller is using the UTC time zone. 

 

How does the code snippet work? 

Daylight Savings is applied during December, January, and February. 

  • During October, the first step is to find the date of the first Sunday of the month. If the current day is on or after the first Sunday after 2:00 am, daylight saving is applied. 
  • During March, the first step is to find the second Sunday of the month. If the current day is on or before the second Sunday before 2:00 am, daylight saving is applied. 

Then, modify the time zone acronym from UTC to the local acronym. 

 

How do I use the time change code snippet? 

  • Define a Custom Templating Variable named localTZ, which will store the customer’s local time zone acronym (for example: ET for Eastern Time). 
  • Define a Custom Templating Variable named offset, which stores the value of the time difference between the SaaS controller’s time zone and customer’s time zone without daylight saving. 
  • In the template, use the code defined in section: Code 

NOTE | the code is using $latestEvent.eventTime as initial time value 

NOTE | The index of the months and weekdays are zero based, so January is index 0 and December is index 11, and the week starts on Sunday with 0 and ends on Saturday with 6. 

Example of the variable declaration in the Custom Templating VariablesExample of the variable declaration in the Custom Templating Variables

 

Code 

NOTE | If required, delete all comments. 

## Variable declaration 

#set($date = $latestEvent.eventTime) 

#set($month = $date.getMonth()) 

#set($day = $date.getDate()) 

#set($weekDay = $date.getDay()) 

  

## By default, the offset is applied to the hours variable 

#set ($newHours = ${date.getHours()} - $offset) 

#set($daylightSavingOffset = $offset + 1) 

  

## In the months of January, February, and December, the daylight-saving time is applied 

#if($month == 11 || $month == 0 || $month == 1) 

     #set($newHours = ${date.getHours()} - $daylightSavingOffset) 

#end 

  

## In the month of October, it is required to find the first Sunday of the month to verify if the daylight saving time needs to be applied 

#if($month == 10) 

     #set($tempDate = $latestEvent.eventTime) 

     #set($firstSunday = $weekDay) 

  

     ## Loop through the first 8 days of the month to find the first Sunday of the month 

     #foreach($tempDay in [1..8]) 

          $tempDate.setDate($tempDay) 

          #set($tempWeekDay = $tempDate.getDay()) 

  

          ## If the current day we are looping is Sunday we store that day and break the loop 

          #if($tempWeekDay == 0) 

               #set($firstSunday = $tempDay) 

               #break 

          #end 

     #end 

  

     ## If the current day is after the first Sunday of October, the daylight-saving time is applied 

     #if($day > $firstSunday) 

          #set($newHours = ${date.getHours()} - $daylightSavingOffset) 

     ## If the current day is the first Sunday of Octuber, the daylight-saving time is only applied if the current hour is or has passed 2:00 a.m 

     #elseif($day == $firstSunday && $newHours >= 2) 

          #set($newHours = ${date.getHours()} - $daylightSavingOffset) 

     #end 

#end 

  

## In the month of March, it is required to find the second Sunday of the month to verify if the daylight-saving time needs to be applied 

#if($month == 2) 

     #set($tempDate = $latestEvent.eventTime) 

     #set($secondSunday = $weekDay) 

  

     ## Variable use to count the number of Sundays that have been looped 

     #set($counter = 0) 

  

     ## Loop through the first 15 days of the month to find the second Sunday of the month 

     #foreach($tempDay in [1..15]) 

          $tempDate.setDate($tempDay) 

          #set($tempWeekDay = $tempDate.getDay()) 

  

          ## If the current day we are looping is Sunday we need to increase the counter if looped Sundays 

          #if($tempWeekDay == 0) 

               #set($counter = $counter + 1) 

  

               ## If the number of looped Sundays is 2, it means that the second Sunday of the month has been found, the day is stored, and the loop stopped 

               #if($counter == 2) 

                    #set($secondSunday = $tempDay) 

                    #break 

               #end 

          #end 

     #end 

  

     ## If the current day is before the second Sunday of March, the daylight-saving time is applied 

     #if($day < $secondSunday)  

          #set($newHours = ${date.getHours()} - $daylightSavingOffset) 

     ## If the current day is the second Sunday of March, the daylight-saving time is only applied if the current hour has not passed 2:00 a.m 

     #elseif($day == $secondSunday && $newHours < 2) 

          #set($newHours = ${date.getHours()} - $daylightSavingOffset) 

     #end 

#end

## Generate the new date using the new hours with the offset 

$date.setHours(${newHours}) 

#set ($dateString =${date.toString()}) 

## Change the timezone acronym 

#set ($replacedDateString=${dateString.replace('UTC',$localTZ)}) 

Version history
Last update:
‎09-22-2023 09:03 PM
Updated by:
On-Demand Webinar
Discover new Splunk integrations and AI innovations for Cisco AppDynamics.


Register Now!

Observe and Explore
Dive into our Community Blog for the Latest Insights and Updates!


Read the blog here