Use Case
- Calculating a Date in the future or past that is NOT based on Current Date (although this macro/action will work for current date as well).
- Basing it on any other date requires use of the ICUDateTimeFOR token with the TIME function which is in GMT.
- This causes some compromises in time, and complications in Actions to use.
- The below macro presents one simple Shell Script to made date calculations easy, while providing much flexibility
- Since ALL of the KM Variables are outside of the actual script, and are entered as Parameters to the Action, it makes changing them easy and reduces errors.
- It can easily be refactored as a reusable Action.
Reusable Execute Shell Script
# Enter KM Variables (or text) for these Bash Variables
# BaseDate AdjustBy [ AdjustUnits DateFormatIN DateFormatOUT ]
# Variables in brakets [ ] are optional; Must enter prior Variables to not enter subsequent variables.
# Default for Both input and output date format is YYYY-MM-DD
read myBaseDate myAdjust myAdjustUnits myDateFormatIn myDateFormatOut
# --- DEFAULT VALUES If NOT Passed Via stdin ---
myAdjustUnits=${myAdjustUnits:-d}
myDateFormatIn=${myDateFormatIn:-%Y-%m-%d}
myDateFormatOut=+${myDateFormatOut:-%Y-%m-%d}
# Adjustment Input Format Output Format
date -j -v$myAdjust$myAdjustUnits -f $myDateFormatIn "$myBaseDate" $myDateFormatOut
Example Output
MACRO: Date Calculator [Reusable Action]
~~~ VER: 2.0 2018-07-23 ~~~
DOWNLOAD:
Date Calculator [Reusable Action].kmmacros (12 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
As always, please feel free to post any questions, issues, and/or suggestions you may have about this macro.
ReleaseNotes
Author.@JMichaelTX
PURPOSE:
-
Provide a Reusable, Configurable, Action to Calculate Dates
- While KM provides extensive handling of dates using the ICU tokens & functions, it is very laborous to write a macro which calculates a new date from a date other than the current date.
- The Action in this macro presents an alternative to using KM ICU tokens.
- It provides one KM Action, a Shell Script with a Bash date command, to accomplishes this with only 5 lines, and yet is very configurable.
- It only requires two inputs, which the macro author can enter as KM Variable Tokens in the Execute Shell Script Action Input area, which are Parameters for the script Standard In (stdin):
- Base Date
- Adjust by amount (in days, +/-)
- By Default, the script uses this data, which can be easily overridden:
- Adjust By Units: Days
- Date Input Format: ISO 8601 YYYY-MM-DD
- Date Output Format: ISO 8601 YYYY-MM-DD
- Override these by adding Parameters to the Script Input area.
- If you don't like the defaults, you can easily change the defaults in the script.
REQUIRES:
- KM 7+
-
macOS 10.10+ (Yosemite)
- However, it has been tested ONLY in KM 8.2.2 with macOS 10.12.6
- However, it has been tested ONLY in KM 8.2.2 with macOS 10.12.6
How To Use
As a Demo
- Just Trigger this macro.
- Enter the Base Date and Offset
- Observe Results
As a Reusable Action
-
Save the Execute Shell Script Action somewhere for easy reuse
(choose one of these:)- I recommend the KMFAM Macro System by @DanThomas
- Setup as a Sub-Macro, which can be called using an Execute Macro Action
- Paste the Script Parameters as Parameters to the Execute Macro Action
- Store in a "template" macro where you can copy/paste into new Macros
MACRO SETUP
-
Carefully review the Release Notes and the Macro Actions
- Make sure you understand what the Macro will do.
- You are responsible for running the Macro, not me. ??
.
- Assign a Trigger to this maro..
- Move this macro to a Macro Group that is only Active when you need this Macro.
- ENABLE this Macro.
.
-
REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
(all shown in the magenta color)- Prompt for User Input
- You can change the defaults for BaseDate and AdjustBy (which MUST include the sign), but do NOT change the Units.
- Prompt for User Input