11 Comments
author

The one hour difference shouldn’t be linked to the f-string though. It could be earlier in your code?

Expand full comment
author

Hi Rolf, this is a great note. I wrote this code for my personal use and it works but I use a non-standard format specifier which I believe doesn’t work on Windows. I assume you’re on Windows?

My version gives the hour without a leading zero but with a space. Normally I’d avoid these variations in my tutorials, but this wasn’t intended as a tutorial so I forgot!

Expand full comment
author

Hi Rolf, this is a great note. I write this code for my personal use and it works but I use a non-standard format specifier which I believe doesn’t work on Windows. I assume you’re on Windows?

My version gives the hour without a leading zero but with a space. Normally I’d avoid these variations in my tutorials, but this wasn’t intended as a tutorial so I forgot!

Expand full comment

Hi Stephen,

the code in this form results in an error: ValueError: Invalid format string. As a beginner in Python I asked chatGPT whats wrong. The issue with the provided Python f-string seems to be related to the formatting of tz_time. The syntax %l is not a valid strftime directive for Python's datetime formatting.

If you intend to format tz_time as the hour in 12-hour clock format without leading zero, you should use %I instead of %l. Here's the corrected version of your f-string:

f"{tz_time:%I:%M %p} {tz.key.split('/')[1].replace('_', ' ')}{day_difference}"

This will format tz_time as the hour in 12-hour clock format with leading zero if necessary, followed by minutes, and the period (AM or PM). The rest of the f-string remains unchanged.

This works (with the second occurence correctet too) but the results differ one hour.

# 05:00 PM London

# 12:00 PM New York

# 09:00 AM Los Angeles

# 06:00 PM Berlin

# 04:00 AM Sydney (+1d)

# 10:30 PM Kolkata

# 05:00 PM UTC/GMT

So when should we listen to your broadcast?

Kind regards

Rolf

Expand full comment
author

My previous replies went to the main comment thread, but I'll carry on the conversation here:

- There was in indeed bug, but unrelated to the f-string. I'm guessing you're based in CET!

The issue was with the use or `.replace()` which returns a copy but doesn't change the `datetime.datetime` object in place. I've updated the code in the article now to reassign to the variable

- I've done my research and the more standard format specifier for what I want is %-I (capital i with the - sign). Also updated in text now

Expand full comment
author

I think my reply went to the main comments rather than in this thread

Expand full comment

Stephen,

the corrected code gives now the right time.

I'm sorry, I don't want to be a pain, but

f"{tz_time:%-I:%M %p} {tz.key.split('/')[1].replace('_', ' ')}{day_difference}"

with '%-I (capital i with the - sign)' still does not work. (Win8.1)

Actually it was not updated in the text.

Kind regards

Rolf

Expand full comment
author

No worries about asking–that's the reason there'a a comments section!!

The text on the web-platform should show the `%-I` version - you may need to refresh the page, perhaps. Did this still not work on a Windows? I don't have a Windows to check this so had to go by what I read. Let me look again

Expand full comment
author

I think there's no equivalent format specifier that works on Windows, so either you remove it manually, or leave the leading zero!

Expand full comment

I never objected the leading zeros. Only that Python showed an error. This did not work under Windows, not a refresh problem.

But f"{tz_time:%I:%M %p} {tz.key.split('/')[1].replace('_', ' ')}{day_difference}" is working as I wrote in the beginning.

I wonder if it works under Apple and Linux.

Expand full comment
author

I left a message elsewhere in this chat (the substack commenting system is confusing!). The -I works on Linux/Unix/Mac but not on Windows. Best to keep the leading zero then!!

Expand full comment