Skip to main content

Afternoon,

 

Does anyone know a way of calculating working days between dates?

 

I have used the DateTimeCalculator to work out the calendar days but the department that is asking for the work to be completed are asking for working days as it is linked to processing times.

 

Thanks in advance

Darren

 

 

Do you need to consider holidays as well as weekends?


There's an API for that, which may just be the easiest way to go.


How do you define a working day? Monday-Friday? What about public holidays?


How do you define a working day? Monday-Friday? What about public holidays?

@david_r it would just be Monday to friday


@david_r it would just be Monday to friday

In that case there are several good solutions using Python here:

https://stackoverflow.com/questions/3615375/count-number-of-days-between-dates-ignoring-weekends

Let us know if you need help implementing it in a PythonCaller.


Do you need to consider holidays as well as weekends?

No just literally work out how many working days between a start date and end date not worried about holidays


In that case there are several good solutions using Python here:

https://stackoverflow.com/questions/3615375/count-number-of-days-between-dates-ignoring-weekends

Let us know if you need help implementing it in a PythonCaller.

Have you got any guides on using python as i have never used it before!

Here's a workspace template with a PythonCaller that calculates the number of days [Monday..Friday] between the dates specified in attribute start_date and end_date.

get_working_days.fmwt


Here's a workspace template with a PythonCaller that calculates the number of days [Monday..Friday] between the dates specified in attribute start_date and end_date.

get_working_days.fmwt

Many thanks for that - Just need to get Python installed on laptop now, I will let you know who I get on.


Many thanks for that - Just need to get Python installed on laptop now, I will let you know who I get on.

You're welcome. No need to install Python, however, it's already installed with FME. Just download and run.


You're welcome. No need to install Python, however, it's already installed with FME. Just download and run.

ive tried to run it and i am getting the following error message

Python Exception <ImportError>:

Importing the multiarray numpy extension module failed. Most

likely you are trying to import a failed build of numpy.

If you're working with a numpy git repo, try `git clean -xdf` (removes all

files not under version control). Otherwise reinstall numpy.

Original error was: DLL load failed: The network name cannot be found.

Error executing string `import<space>fmeobjects<lf>import<space>numpy<space>as<space>np<lf>import<space>datetime<space>as<space>dt<lf><lf>def<space>get_working_days<openparen>feature<closeparen>:<lf><space><space><space><space>start_date<space>=<space>feature.getAttribute<openparen><apos>start_date<apos><closeparen><lf><space><space><space><space>end_date<space>=<space>feature.getAttribute<openparen><apos>end_date<apos><closeparen><lf><space><space><space><space>if<space>start_date<space>and<space>end_date:<lf><space><space><space><space><space><space><space><space>days<space>=<space>np.busday_count<openparen>start_date<comma><space>end_date<closeparen><lf><space><space><space><space><space><space><space><space>feature.setAttribute<openparen><apos>working_days<apos><comma><space>int<openparen>days<closeparen><closeparen>'

Factory proxy not initialized

PythonCaller (PythonFactory): PythonFactory failed to process feature

 

Have you seen that before?


ive tried to run it and i am getting the following error message

Python Exception <ImportError>:

Importing the multiarray numpy extension module failed. Most

likely you are trying to import a failed build of numpy.

If you're working with a numpy git repo, try `git clean -xdf` (removes all

files not under version control). Otherwise reinstall numpy.

Original error was: DLL load failed: The network name cannot be found.

Error executing string `import<space>fmeobjects<lf>import<space>numpy<space>as<space>np<lf>import<space>datetime<space>as<space>dt<lf><lf>def<space>get_working_days<openparen>feature<closeparen>:<lf><space><space><space><space>start_date<space>=<space>feature.getAttribute<openparen><apos>start_date<apos><closeparen><lf><space><space><space><space>end_date<space>=<space>feature.getAttribute<openparen><apos>end_date<apos><closeparen><lf><space><space><space><space>if<space>start_date<space>and<space>end_date:<lf><space><space><space><space><space><space><space><space>days<space>=<space>np.busday_count<openparen>start_date<comma><space>end_date<closeparen><lf><space><space><space><space><space><space><space><space>feature.setAttribute<openparen><apos>working_days<apos><comma><space>int<openparen>days<closeparen><closeparen>'

Factory proxy not initialized

PythonCaller (PythonFactory): PythonFactory failed to process feature

 

Have you seen that before?

Which FME version are you using, and are you using Python 2.x or 3.x?


Which FME version are you using, and are you using Python 2.x or 3.x?

We are on version 2019.0 of Fme and in tools - fme options - in the preferred Python Interpretor it has FME Python 3.5+ and in the navigator under scripting it has Python 3.5+ in the python compatability


We are on version 2019.0 of Fme and in tools - fme options - in the preferred Python Interpretor it has FME Python 3.5+ and in the navigator under scripting it has Python 3.5+ in the python compatability

Weird, it worked perfectly on my FME 2019.2 64-bit with Python 3.7+

Are you on Windows?


Weird, it worked perfectly on my FME 2019.2 64-bit with Python 3.7+

Are you on Windows?

yes we are on windows


yes we are on windows

Strange. Sorry, I have no idea. You may want to try modifying the Python code to use one of the other methods from the Stack Overflow link I posted earlier.


If you wanted to try some alternative code that avoids numpy

import fme
import fmeobjects
from datetime import datetime,timedelta

def processFeature(feature):
    weekdaycount = 0
    d1 = datetime.strptime(feature.getAttribute('start_date'), "%Y-%m-%d")
    d2 = datetime.strptime(feature.getAttribute('end_date'), "%Y-%m-%d")
    days = (d2 - d1).days
    for i in range(0,days):
        mydate = d1 + timedelta(days=i)
        if mydate.weekday()<5:
            weekdaycount +=1
    feature.setAttribute('weekdays',weekdaycount)

Here's a workspace that does the calculations in an AttributeManger, not Python. I too got my algorithm from StackOverflow.

xxxx - see below for new workspace - xxxx.fmw

I tested the results against this web site and they seemed to match, so I think it's OK. I haven't tried it when the start/end date are in different years, but I'm hopeful that it will work.


There's an API for that, which may just be the easiest way to go.

I always think that a lot of FME workspaces on the hub could be wrapped up into micro-services like this, but I've never seen anyone do it yet. A lot of potential there, in my opinion anyway.


Here's a workspace that does the calculations in an AttributeManger, not Python. I too got my algorithm from StackOverflow.

xxxx - see below for new workspace - xxxx.fmw

I tested the results against this web site and they seemed to match, so I think it's OK. I haven't tried it when the start/end date are in different years, but I'm hopeful that it will work.

OK, I'm taking a second look and there is something wrong with this. I'm investigating...


Here's a workspace that does the calculations in an AttributeManger, not Python. I too got my algorithm from StackOverflow.

xxxx - see below for new workspace - xxxx.fmw

I tested the results against this web site and they seemed to match, so I think it's OK. I haven't tried it when the start/end date are in different years, but I'm hopeful that it will work.

Right. I ended up taking the whole algorithm apart and couldn't figure out what they had done. So I basically rewrote it. What a pain. Try this workspace. It's got a custom transformer with an extra parameter to let you decide which days of the week count as the weekend. That's the part I haven't fully tested yet, but I'm about to finish for the day and Saturday/Sunday weekends work just fine, so I'm guessing it will be OK for you.

wdaysdifferencedevelopment.fmw


You could calculate the number of days between the two dates with a DateTimeCalulator, use a Cloner to create a feature for each day, another DateTimeCalculator using _copynum to assign the correct date to each feature, a DateTimeConverter to format the day as Mon, Tue etc., an AttributeFilter to split them into weekdays and weekends followed by a StatisticsCalculator to count the weekdays.


Right. I ended up taking the whole algorithm apart and couldn't figure out what they had done. So I basically rewrote it. What a pain. Try this workspace. It's got a custom transformer with an extra parameter to let you decide which days of the week count as the weekend. That's the part I haven't fully tested yet, but I'm about to finish for the day and Saturday/Sunday weekends work just fine, so I'm guessing it will be OK for you.

wdaysdifferencedevelopment.fmw

Many thanks for this I will take a look, and will let you know how I got on


Many thanks for this I will take a look, and will let you know how I got on

In case it helps, I bundled the whole thing into a custom transformer, expanded on the functionality a bit, and posted it to the FME Hub. You can find it here: https://hub.safe.com/publishers/safe-lab/transformers/workingdayscalculator#description


Reply