0

I'm trying to change:

pytz.timezone('Europe/Ljubljana').localize(datetime.datetime.now().replace(microsecond=0))

to print time in Europe/Ljubljana but it still has offset of 2 hours (uses UTC and not local time as in Europe/Ljubljana. Anyone knows what should i change to fix to corect time?

thank you.

was trying different answers from stack with no luck

pytz.timezone('Europe/London').localize(datetime.datetime.now(tzlocal).replace(microsecond=0))
pytz.timezone('Europe/Ljubljana').localize(datetime.datetime.now().replace(microsecond=0))
pytz.timezone('Europe/London').localize(datetime.datetime.now().replace(microsecond=0))
pytz.timezone('Europe/London').localize(datetime.datetime.now().replace(microsecond=0))
pytz.timezone('Europe/London').localize(datetime.datetime.now().replace(microsecond=0))
utc_now.astimezone(pytz.timezone("Pacific/Honolulu"))
pytz.utc.localize().astimezone(pytz.timezone('EST')).replace(microsecond=0))
pytz.timezone().astimezone(datetime.datetime.now(timezone.utc).replace(microsecond=0))
datetime.datetime.now(pytz.timezone('Europe/Ljubljana'))
datetime.datetime.now(ZoneInfo("America/Los_Angeles"))
datetime.datetime.now().astimezone().tzinfo
datetime.timezone(datetime.timedelta(seconds=36000), 'AEST')
datetime.now(timezone.utc).astimezone()
datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
(datetime.datetime.now(datetime.timezone(datetime.timedelta(0))).astimezone().tzinfo)

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Resolved
  • 1
  • 1
  • Could you update the title? Since you have utc_test = .... I assumed that you want the UTC time and not the local time. Also as improvement of the question. Should be easier to see if you had code split on more lines instead of make it compact. Since now there is a need to scroll sideways to see all code. Just a suggestion. – starking Mar 26 '23 at 13:27
  • Just use `datetime.datetime.now(pytz.timezone('Europe/Ljubljana'))` to get the current time in 'Europe/Ljubljana'. Note though that this does not *change* the time zone of the machine you run this code on. – FObersteiner Mar 26 '23 at 16:38

1 Answers1

0

I think you got confused by the "+02:00" in 2023-03-26 16:13:33+02:00 that current timezone is +2 hours.

If you just print print(datetime.now().replace(microsecond=0))

You get without "+02:00" and the time is in local timezone.

Here is one way of doing it:

from datetime import datetime
import pytz

ljubljana_tz = pytz.timezone('Europe/Ljubljana')

ljubljana_time_now = datetime.now()
ljubljana_time_now_no_ms = ljubljana_time_now.replace(microsecond=0)

# Local time:
print(ljubljana_time_now_no_ms)

# Local time with timezone indicated. 
print( pytz.timezone('Europe/Ljubljana').localize(ljubljana_time_now_no_ms) )

# Incase some one want it in etc
print(ljubljana_time_now_no_ms.astimezone(pytz.utc))

Hope I got it right now. That you had the time correct just missunderstood the output.

However since you are not converting I guess timezones is not even needed. You can just write:

print(datetime.now().replace(microsecond=0))

Output something like this: 2023-03-26 16:55:09

starking
  • 153
  • 10