Jump to content
MuppetGate

Yet another date calculator

Recommended Posts

On 03/09/2016 at 0:55 PM, MuppetGate said:

if I was excluding weekends and which pushed out the date to another weekend, then I wasn't accounting for the new weekend and pushing the date out even further.

 

From my reading of your code, I think the issue is that you jump straight to the end date, figure out which days should have been excluded, and then adjust the end date accordingly (which you then need to repeat ad infinitum on the new range).

 

Rather, I think the right way to handle excludes would be to increment/decrement the date one day at a time, ignore that date if it's excluded, and keep going until your count matches the requested delta:

delta = 10  # wanted difference in days
exclude = 'weekends'
count = 0  # current difference in days
current = start_date

while True:
    if is_excluded(current, exclude):  # some magic goes here
        continue

    if count == delta:  # got result
        break
 
    # increment counter and current date for next loop
    current += timedelta(days=1)
    count += 1

print(current)  # the requested end date

 

Share this post


Link to post

Excellent workflow, many thanks!!

 

I have a couple of suggestions:

  • Date format yymmdd.
  • Output format in number of days, not months, years, or weeks, without words – to make it easy to work with in eg Numbers.
  • Calculating accumulated time, like 20180102-20180107 + 20180327-20180328.

:)

Share this post


Link to post

Thank you very much for creating such a great workflow.

 

I have found a few things that do not work and how to fix them.

  • The contents of /usr/bin/osascript currently refer to Alfred 2, not Alfred 3: to get the workflow to work with Alfred 3, all instances of: tell application "Alfred 2", need to be replaced with: tell application "Alfred 3"
  • The first /usr/bin/osascript (the one linked to dcalc) has all escapings active. Removing all escapings should allow the workflow to work properly without throwing an error when the output is anything else than just a date. For example if the output of the workflow is "DATE at TIME" or anything else resulting for a calculation that is more than just a date (m MONTHS, w WEEKS, d DAYS, h HOURS) an error will be thrown currently.
  • dcalcset date and time: currently the call to this command doesn't work. The reason is that on line 26 of the file date_time_format_list.py the call to date_time = datetime.datetime.now() should be replaced with date_time = datetime.now()
  • I recommend activating "Automatically paste to frontmost app" in the "Copy to Clipboard" module that is connected to dcalc

Share this post


Link to post

 

Okay, I’ve scored a bit of time to revisit the date app and fix a few bugs. Not everything has been looked at because I’ve got to head off again for a while, but what I have done is:


–    Upgraded the workflow library
–    Fixed a few of the scripts that referred to Alfred 2 instead of Alfred 3 (I guess this means Alfred 2 is no longer supported).
–    A bit of a general code tidy up


The big piece of work though, is the restoration of the exclude functionality, which should work properly, but with limitations. To prevent it blowing things up if the search combination doesn’t produce a valid match, I’ve limited the workflow to about 300 attempts to look for a match. After that, I’ll throw an error.


So how does it work? Here’s a quick example.


Say you’re a busy project bod and you have a piece of work that’s going to take three weeks. So you enter the data expression:


dcalc today + 3w


And that gives you a date three weeks into the future. But hang on a sec: no one works weekends, so how long will it take if there are no weekends included:


dcalc today + 3w exclude weekends


So now the date will be further into the future because weekends are not counted as part of the project. (Another way to look at it is if people worked weekends then the project could be brought in at an earlier date).


You can exclude individual days:


dcalc today + 3w x weekends 28.10.2018 


This will exclude all weekends and also the 28th of October. Note that you can just use x or ex instead of exclude.
We can exclude other days:


dcalc today + 3w x Tuesdays Wednesdays Fridays


We can also do ranges:


dcalc today + 3w exclude Tuesdays 30.10.2018 to 05.11.2018 02.12.2018


This beast will exclude Tuesdays, all days between the 30.10.2018 and 05.11.2018, and it will also exclude 02.12.2018
We can also do:


dcalc today + 3w exclude all except tuesdays


Error checking is minimal, but this kind of stuff won’t work:


dcalc today + 3w ex all except wednesdays wednesdays
dcalc today + 3w x weekends weekdays

 

Because every day has been excluded.

 

The update can be found in the usual place:

 

http://www.packal.org/workflow/date-calculator

 

The usual disclaimers apply, so don't use this to schedule brain surgery.  Use a calendar.

 

Edited by MuppetGate

Share this post


Link to post

It seems that the examples that you have given above only work if you write without capitals: dcalc today + 3w x tuesdays wednesdays fridays

 

Edited by Guy Stan

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×