Skip to main content
Question

Converting UTC datetime to CET/CEST


fgiron
Supporter
Forum|alt.badge.img+20

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

4 replies

takashi
Influencer
  • 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), 3251)
            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+32
  • Influencer
  • 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+20
  • Author
  • Supporter
  • April 27, 2020
takashi wrote:

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), 3251)
            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+20
  • Author
  • Supporter
  • April 27, 2020
ebygomm wrote:

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!!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings