Skip to main content
Question

Converting UTC datetime to CET/CEST

  • April 25, 2020
  • 4 replies
  • 1210 views

fgiron
Supporter
Forum|alt.badge.img+25

Hi,

I'm trying to convert UTC datetimes to CET/CEST but can't find the way of getting a datetime that takes into account the Daylight Saving Time differences. The local settings of the DateTimeStamper or @TimeZoneSet function are of no use because the workspace is run in an FME Server where the local time is UTC.

Thanks a lot in advance!

 

Francisco
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Celebrity
  • 7843 replies
  • April 25, 2020

Hi @fgiron, according to the definition of CET/CEST, timezone +02:00 is applied in 210 days between 01:00 UTC on the last Sunday of March and 01:00 UTC on the last Sunday of October, otherwise timezone +01:00 is applied. Therefore, if you get start and end dates of the CEST duration in the year, you can determine the correct timezone for a specific datetime by testing the datetime is within the duration.

This screenshot illustrates a possible way.

0684Q00000ArK40QAF.png

 

Alternatively, a PythonCaller with this script works as well.

from datetime import datetime, timedelta

class CESTStartEndDatetimeCalculator(object):
    def __init__(self):
        self.cestStartEnd = {}
        
    def input(self, feature):
        def getCestStartEnd(year):
            s = datetime(int(year), 3, 25, 1)
            while s.strftime('%w') != '0':
                s = s + timedelta(days=1)
            e = s + timedelta(days=210)
            return (
                s.strftime('%Y%m%d%H%M%S') + '+00:00',
                e.strftime('%Y%m%d%H%M%S') + '+00:00'
            )
            
        year = str(feature.getAttribute('_timestamp_utc'))[:4]
        s, e = self.cestStartEnd.setdefault(year, getCestStartEnd(year))
        
        feature.setAttribute('_cest_start_datetime', s) # 01:00 UTC, the last Sunday of March
        feature.setAttribute('_cest_end_datetime', e) # 01:00 UTC, the last Sunday of October
        self.pyoutput(feature)
        
    def close(self):
        pass

ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • April 27, 2020

Dateutil has a timezone module so another python solution

import fme
import fmeobjects
from datetime import datetime
from dateutil import tz

def convertUTC(feature):

    from_zone = tz.gettz('UTC')
    to_zone = tz.gettz('Europe/Paris')
    
    utc = datetime.strptime(str(feature.getAttribute('date')), '%Y%m%d%H%M%S')

    # set date to UTC
    utc = utc.replace(tzinfo=from_zone)

    # Convert time zone
    central = utc.astimezone(to_zone)
    
    feature.setAttribute("converted_date", central.strftime('%Y%m%d%H%M%S%z'))

0684Q00000ArJuKQAV.png


fgiron
Supporter
Forum|alt.badge.img+25
  • Author
  • Supporter
  • 92 replies
  • April 27, 2020

Hi @fgiron, according to the definition of CET/CEST, timezone +02:00 is applied in 210 days between 01:00 UTC on the last Sunday of March and 01:00 UTC on the last Sunday of October, otherwise timezone +01:00 is applied. Therefore, if you get start and end dates of the CEST duration in the year, you can determine the correct timezone for a specific datetime by testing the datetime is within the duration.

This screenshot illustrates a possible way.

0684Q00000ArK40QAF.png

 

Alternatively, a PythonCaller with this script works as well.

from datetime import datetime, timedelta

class CESTStartEndDatetimeCalculator(object):
    def __init__(self):
        self.cestStartEnd = {}
        
    def input(self, feature):
        def getCestStartEnd(year):
            s = datetime(int(year), 3, 25, 1)
            while s.strftime('%w') != '0':
                s = s + timedelta(days=1)
            e = s + timedelta(days=210)
            return (
                s.strftime('%Y%m%d%H%M%S') + '+00:00',
                e.strftime('%Y%m%d%H%M%S') + '+00:00'
            )
            
        year = str(feature.getAttribute('_timestamp_utc'))[:4]
        s, e = self.cestStartEnd.setdefault(year, getCestStartEnd(year))
        
        feature.setAttribute('_cest_start_datetime', s) # 01:00 UTC, the last Sunday of March
        feature.setAttribute('_cest_end_datetime', e) # 01:00 UTC, the last Sunday of October
        self.pyoutput(feature)
        
    def close(self):
        pass

Hi @takashi, thanks a lot for your answer!

It hadn't occurred to me that summer time always lasts the same. Nor had I thought about replicating the algorithm (last Sunday of March/October) in FME. I love it!!


fgiron
Supporter
Forum|alt.badge.img+25
  • Author
  • Supporter
  • 92 replies
  • April 27, 2020

Dateutil has a timezone module so another python solution

import fme
import fmeobjects
from datetime import datetime
from dateutil import tz

def convertUTC(feature):

    from_zone = tz.gettz('UTC')
    to_zone = tz.gettz('Europe/Paris')
    
    utc = datetime.strptime(str(feature.getAttribute('date')), '%Y%m%d%H%M%S')

    # set date to UTC
    utc = utc.replace(tzinfo=from_zone)

    # Convert time zone
    central = utc.astimezone(to_zone)
    
    feature.setAttribute("converted_date", central.strftime('%Y%m%d%H%M%S%z'))

0684Q00000ArJuKQAV.png

Hi @ebygomm, very nice and simple python script. It looks like a very simple possible addition to the DateTimeCalculator. Thank you!!