Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active October 13, 2025 09:49
Show Gist options
  • Select an option

  • Save smeech/b9f1e793cf63ee42e8512450ae85dc98 to your computer and use it in GitHub Desktop.

Select an option

Save smeech/b9f1e793cf63ee42e8512450ae85dc98 to your computer and use it in GitHub Desktop.
[Date_Time] Espanso trigger to output time and date in another timezone. Requires Python and its pytz library. Somewhat redundant from Espanso v2.3.0. #espanso #python
# Espanso trigger to output time and date in another timezone
# Requires Python and its pytz library
matches:
- trigger: :tzdate
replace: '{{output}}'
vars:
- name: zones
type: script
params:
args:
- python
- -c
- |
import pytz
print("\n".join(pytz.all_timezones))
- name: zone_choice
type: form
params:
layout: 'Pick a time-zone: [[zone]]'
fields:
zone:
type: list
values: '{{zones}}'
default: Europe/London
- name: output
type: script
params:
args:
- python
- -c
- |
import pytz; from datetime import datetime
timezone = pytz.timezone('{{zone_choice.zone}}')
# Get the current date and time in UTC
utc_now = datetime.utcnow().replace(tzinfo=pytz.utc)
# Convert UTC time to the specified timezone
local_time = utc_now.astimezone(timezone)
print("Current date and time in", timezone, "is:", local_time.strftime('%F %T'))
@flypenguin

Copy link
Copy Markdown

i did the same πŸ˜† ... it's just way faster with the internal data type, and not as wasteful ...

#!/usr/bin/env python3

import datetime as dt
import locale
import time
from argparse import ArgumentParser
from zoneinfo import ZoneInfo

# https://is.gd/DDyxBL
FORMAT_STRINGS = {
    "absdate": "%Y-%m-%d",
    "abstime": "%Y-%m-%dT%H:%M:%S%:z",
    "isodate": "%Y-%m-%d",
    "isotime": "%Y-%m-%dT%H:%M:%S",
    "utcdate": "%Y-%m-%d",
    "utctime": "%Y-%m-%dT%H:%M:%SZ",
}

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("-o", "--offset", type=int, default=0, help="Offset date by this many days")
    parser.add_argument("-u", "--utc", action="store_true", default=False, help="Use UTC date/time")
    parser.add_argument("-l", "--locale", default="de_DE")
    parser.add_argument("-z", "--zone", default=None, help="Use the given time zone")
    parser.add_argument("format_str", default=["isodate"], nargs="*", help="Specify format string or 'isodate'/'isotime'")
    config = parser.parse_args()

    # this IS discouraged, yet this is not a web app, this is a one-shot tool.
    locale.setlocale(locale.LC_ALL, config.locale)

    format_str = " ".join(config.format_str)
    utc = config.utc or format_str.startswith("utc")

    if utc:
        tz = dt.timezone.utc
    elif config.zone:
        tz = ZoneInfo(config.zone)
    else:
        tz = ZoneInfo(time.tzname[0])
    now = dt.datetime.now(tz=tz) + dt.timedelta(days=config.offset)

    format_str = FORMAT_STRINGS.get(format_str, format_str)
    print(now.strftime(format_str))

@smeech

smeech commented Feb 6, 2025

Copy link
Copy Markdown
Author

i did the same πŸ˜† ... it's just way faster with the internal data type, and not as wasteful ...

# this IS discouraged, yet this is not a web app, this is a one-shot tool.

    locale.setlocale(locale.LC_ALL, ".".join(locale.getlocale()))

works here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment