Skip to content

Instantly share code, notes, and snippets.

@w-e-w
Last active October 23, 2022 09:23
Show Gist options
  • Save w-e-w/ba208f745107316e0c084027e0c40a1f to your computer and use it in GitHub Desktop.
Save w-e-w/ba208f745107316e0c084027e0c40a1f to your computer and use it in GitHub Desktop.
customizable filename pattern [datetime] for AUTOMATIC1111/stable-diffusion-webui
import datetime
import pytz
import re
def replace_datetime(input_str: str, time_datetime: datetime.datetime = None):
"""
Args:
input_str (`str`):
the String to be Formatted
time_datetime (`datetime.datetime`)
the time to be used, if None, use datetime.datetime.now()
Formats sub_string of input_str with formatted datetime with time zone support.
accepts sub_string format: [datetime], [datetime<Format>], [datetime<Format><Time Zone>]
case insensitive
e.g.
input: "___[Datetime<%Y_%m_%d %H-%M-%S><Asia/Tokyo>]___"
return: "___2022_10_22 20-40-14___"
handles invalid Formats and Time Zones
time format reference:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
valid time zones
print(pytz.all_timezones)
https://pytz.sourceforge.net/
"""
default_time_format = '%Y%m%d%H%M%S'
if time_datetime is None:
time_datetime = datetime.datetime.now()
# match all datetime to be replace
match_itr = re.finditer(r'\[datetime(?:<([^>]*)>(?:<([^>]*)>)?)?]', input_str, re.IGNORECASE)
for match in reversed(list(match_itr)):
# extract format
time_format = match.group(1)
if time_format == '':
# if time_format is blank use default YYYYMMDDHHMMSS
time_format = default_time_format
# extract timezone
try:
time_zone = pytz.timezone(match.group(2))
except pytz.exceptions.UnknownTimeZoneError as _:
# if no time_zone or invalid, use system time
time_zone = None
# generate time string
time_zone_time = time_datetime.astimezone(time_zone)
try:
formatted_time = time_zone_time.strftime(time_format)
except (ValueError, TypeError) as _:
# if format error then use default_time_format
formatted_time = time_zone_time.strftime(default_time_format)
input_str = input_str[:match.start()] + formatted_time + input_str[match.end():]
return input_str
if __name__ == '__main__':
# list all time zones
for tz in pytz.all_timezones:
print(tz)
print()
# original format [datetime]
test = '___[datetime]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___20221022203816___
# with formatting
test = '___[datetime<%Y_%m_%d %H-%M-%S>]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___2022_10_22 20-38-16___
# with or without formatting at timezone, supports multiple
test = '___[datetime<%Y_%m_%d %H-%M-%S><Asia/Tokyo>] and [Datetime<><America/New_York>]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___2022_10_22 20-38-16 and 20221022073816___
# time zone will use will use system time zone
test = '___[datetime<><invalid_zone>]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___20221022203816___
# warning, if format is invalid use default_time_format
test = '___[datetime<invalid format %-><invalid_zone>]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___20221022203816___
# Note, if format is NOT invalid but with only has no format placeholders then will not use default_time_format
test = '___[datetime<----><invalid_zone>]___'
print(test + '\n' + replace_datetime(test) + '\n')
# ___----___'
@w-e-w
Copy link
Author

w-e-w commented Oct 22, 2022

this allows the user to define formatting time zone
[datetime]
[datetime<format>]
[datetime<format><time zone>]
[datetime<><time zone>]

if no format is defined or invalid use default %Y%m%d%H%M%S
if no timezone is defined or invalid use system time

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