Question

YearWeek calculation

  • 10 January 2018
  • 4 replies
  • 10 views

Badge

Hello,

What I would like to do is to create a list of all YearWeeks from a given YearWeek till the current YearWeek automatically.

E.g. I would set the beginning to 201743.

Then the output should look like this:

201743

201744

201745

201746

201747

201748

201749

201750

201751

201752

201801

201802

Please note that all weeks should be 2 digits.

Are there sufficient FME transformers to do so or would I need to use Python?

Your help is highly appreciated.


4 replies

Userlevel 2
Badge +12

I think the transformer you need is the DateTimeCalculator.

You will need to embed that in a custom transformer to create a loop construction and loop until you reach the current week.

Userlevel 2
Badge +12

I have created a workspace that will list the weeks.

You can download it here: weeklister.fmw

Badge

Or the python way: 

In a Python Creator and expose the attribute 'YearWeeks'

import fmeobjects
from datetime import datetime, timedelta


class FeatureCreator(object):
    def __init__(self):
        pass
    def close(self):
        startDateText = "201742" # put a start date of a week before needed
        today = datetime.now() 
        d = timedelta(weeks=1)
        endDateText = today.strftime("%Y%W")
        Currentdate = datetime.strptime("{}0".format(startDateText),"%Y%W%w")
        # had to add a weekday number to get a datetime value
        CurrentText = "0"
       
        while int(CurrentText) < int(endDateText):
            feature = fmeobjects.FMEFeature()
            Currentdate = Currentdate + d
            CurrentText =  Currentdate.strftime("%Y%W")
            feature.setAttribute("YearWeeks",CurrentText) 
            self.pyoutput(feature)

I don't like putting the week before in (201742) and also having numbers as text but datetime uses string as input and outputs. But it does work

Userlevel 2
Badge +17

Although excellent solutions have been provided already, it might also be a good chance to learn FME Date/Time Functions enhanced in 2017. This workflow is an example including use of some Date/Time functions (FME 2017.1+).

Reply