This is no doubt a newbie question. I have had KM for about a year but am still using it for fairly simple text expansion issues. In one such macro, I use the string:
Entry Time: %ShortTime%
However, I am now in CST and this entry refers to an equity purchase (or other financial instrument) which relies on EST (+1 hour in this instance). Ideally, I would like to just have current EST time inserted here, but if I need to adjust the differential (i.e. +1; -2) based on where I currently am, I can live with this solution as well.
Thanks in advance for your help and support in addressing this issue!
I don’t know of any way to automatically insert times from a particular time zone using Keyboard Maestro, but as long as you’re using KM8 it’s easy enough to take advantage of the new %ICUDateTimePlus% and %ICUDateTimeMinus% tokens to generate times at the offset of your choosing. Here’s how to replicate the %ShortTime% token with an additional hour added for EST:
I don’t know of a native KM Action/Token/Function that will convert between time zones, but this AppleScript should do the job:
AppleScript To Convert From Local TZ to US ET TZ
(adjust as needed)
property ptyScriptName : "Convert Date/Time From Local Time Zone to Other Time Zone"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2018-01-10"
property ptyScriptAuthor : "JMichaelTX" -- based on script by Nigel Garvey
use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation" -- this may not be required
use framework "AppKit" -- this may not be required
use scripting additions
--- Use property to Speed Up Handlers ---
property ptyNsFormatter : missing value
--- CHANGE AS NEEDED ---
-- (see script at bottom to get list of TZ Names)
property ptyDestTZName : "America/New_York"
--- GET Date/Time String of Local TZ from Keyboard Maestro --
### Requires Keyboard Maestro 8.0.3+ to Use Local/Instance Variabales ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set dateTimeStr to getvariable "Local__DateTimeISO" instance kmInst
end tell
--- IF KM Variable Does NOT Exist, or is Empty, USE Current Date/Time ---
if (dateTimeStr = "") then
set dateTime to current date
else -- Convert to AppleScript Date
set dateTime to my dateFromisoString(dateTimeStr)
end if
--- Get Name of Local TZ ---
set localTimeZoneName to (do shell script "perl -le 'print( readlink(\"/etc/localtime\") =~m{zoneinfo/(.*)} )' ")
----------------------------------------------------
--- Get AS Date in New (Destination) Time Zone ---
----------------------------------------------------
set dateNewTZ to TZtoTZ(dateTime, localTimeZoneName, ptyDestTZName)
--- Convert from AS Date to ISO Date String "YYYY-MM-DD HH:MM" ---
set dateNewTZISO to (dateNewTZ as «class isot» as string)
set dateNewTZISO to text 1 thru 10 of dateNewTZISO & " " & text 12 thru 16 of dateNewTZISO
return dateNewTZISO
--~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on TZtoTZ(pTZ1date, pTZ1Name, pTZ2Name)
(* VER: 1.0 2013-05-26
----------------------------------------------------
AUTHOR: Nigel Garvey
REF:
• Transposing dates between time zones, MacScripter.net
• https://macscripter.net/viewtopic.php?pid=163706#p163706
*)
return (do shell script ("eraTime=$(TZ=" & pTZ1Name & " date -jf '%FT%T' '" & (pTZ1date as «class isot» as string) & "' '+%s') ; TZ=" & pTZ2Name & " date -r \"$eraTime\" '+%FT%T'") as «class isot») as date
end TZtoTZ
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on dateFromisoString(pDateISOStr)
(* VER: 1.0 2017-12-01
------------------------------------------------------------
AUTHOR: @alldritt (Mark Alldritt)
REF:
• Formatting Dates, Late Night Software Ltd.,
• http://forum.latenightsw.com/t/formatting-dates/841
------------------------------------------------------------
*)
if ptyNsFormatter is missing value then
set ptyNsFormatter to current application's NSDateFormatter's new()
ptyNsFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
ptyNsFormatter's setDateFormat:"yyyy'-'MM'-'dd'T'HH':'mm':'ss"
end if
set nsDateTime to ptyNsFormatter's dateFromString:pDateISOStr
return nsDateTime as date
end dateFromisoString
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(*
# To Get List of Valid Time Zone Names, Run this script separately:
# AUTHOR: @ShaneStanley
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
current application's NSTimeZone's knownTimeZoneNames() as list
*)
To use in KM Macro, just put the AppleScript in an _Execute AppleScript_ Action, like this:
<img src="/uploads/default/original/3X/1/3/13f7d08885d53c867d1c33ca7cba6c03f0ab3083.png" width="424" height="524">
The macro/script assume the following:
1. Convert from Current date/time in local time zone
* You can change this by setting the KM Variable:
`Local__DateTimeISO`
in this format: `"YYYY-MM-DD HH:MM"`
2. Convert to US ET Time Zone ("America/New_York")
* You can change this by changing this line in the script:
`property ptyDestTZName : "America/New_York"`
* Use the script (current in comments) at the bottom of the script to get a list of valid time zone names.
3. Return date/time in ISO format like this:
* `"YYYY-MM-DD HH:MM"`
Questions?
If I got it correctly, what you want is %ShortTime% for a given time zone (EST).
The easiest way to get this would be with the date command line tool:
date accepts a time zone setting as well as output formatting with the usual strftime notation:
TZ="EST" date "+%R"
Result: 09:53
In KM you can save it to a variable like this:
Alternatively, with Swift it isn’t complicated either:
import Foundation
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeZone = NSTimeZone(name: "EST") as TimeZone!
let date = formatter.string(from: Date())
print(date, terminator: "")
Thanks to all who have provided solutions! I plan on trying to implement each idea (as a learning experience for me to become better versed in KM tools. I know there is so much more KM can do for me and with the help of this community, I am now equally sure I can expand the breath of my knowledge. Appreciate everyones’ input!
A question about the TZ setting. How did you know to use “EST”?
I can’t find this in date(1) Mac OS X Manual Page
ENVIRONMENT
The following environment variables affect the execution of date:
TZ The timezone to use when displaying dates. The normal format is a pathname relative to
/usr/share/zoneinfo. For example, the command ``TZ=America/Los_Angeles date'' displays the
current time in California. See environ(7) for more information.
The normal format is a pathname relative to
/usr/share/zoneinfo. For example, the command ``TZ=America/Los_Angeles date’’
It didn’t say we could get the list from /usr/share/zoneinfo
For those, like me, who don’t quite understand the terminal command @Tom used, it is: ls /usr/share/zoneinfo
I read “ls” as “is”. LOL
+VERSION CST6CDT Europe Hongkong MST Portugal WET
Africa Canada Factory Iceland MST7MDT ROC Zulu
America Chile GB Indian Mexico ROK iso3166.tab
Antarctica Cuba GB-Eire Iran NZ Singapore posixrules
Arctic EET GMT Israel NZ-CHAT Turkey zone.tab
Asia EST GMT+0 Jamaica Navajo UCT
Atlantic EST5EDT GMT-0 Japan PRC US
Australia Egypt GMT0 Kwajalein PST8PDT UTC
Brazil Eire Greenwich Libya Pacific Universal
CET Etc HST MET Poland W-SU
So, to get a list of time zone names in the root of that folder, you can use this clever script by our friend @ccstone (with some minor mods by me):
set cmdStr to "
numberOfOutputColumns=3
searchDir='/usr/share/zoneinfo'
cd \"$searchDir\"
find * -maxdepth 0 -type f | rs '' $numberOfOutputColumns
"
set timeZoneTableStr to do shell script cmdStr
return timeZoneTableStr
Chris’ script was a bash script, and I translated that to AppleScript, so I can easily use it with other stuff.
Example Output
No Folders. Files only.
+VERSION CET CST6CDT
Cuba EET EST
EST5EDT Egypt Eire
Factory GB GB-Eire
GMT GMT+0 GMT-0
GMT0 Greenwich HST
Hongkong Iceland Iran
Israel Jamaica Japan
Kwajalein Libya MET
MST MST7MDT NZ
NZ-CHAT Navajo PRC
PST8PDT Poland Portugal
ROC ROK Singapore
Turkey UCT UTC
Universal W-SU WET
Zulu iso3166.tab posixrules
zone.tab
Some of those are obviously NOT time zones, like “posixrules”.
BTW, this is a great example of how to reformat a text list (of one column) into multiple columns, and apply text table formatting to make the columns align when using a monospace font.
The other interesting thing to note is that the ls command does NOT support an output of just files. This is very irritating. But the find command does; so that’s the solution/workaround.
You are right, tree is not part of the Mac standard BSD. Sorry, I completely forgot about that.
If you want it, you can install it with Homebrew:
brew install tree
(or certainly also with MacPorts)
Edit:
Just seen it now: If you simply remove the maxdepth limit from your find command line you get the same as with tree. Throw in a sort to get it alphabetically: