pythondatetimetimezoneunix-timestamp

Python datetime output +3GMT


Please help me to improve my code. I'am trying to calculate current time minus 3 minutes, and output should be in Unix Timestamp +3GMT But instead it prints me utc time (+ 0)

import datetime
import pytz


tz = pytz.timezone('Europe/Moscow')
ct = datetime.datetime.now(tz=tz)


#TIMESTAMP = (datetime.datetime.now(tz=tz) - datetime.timedelta(minutes=3)).timestamp()
TIMESTAMP = (ct - datetime.timedelta(minutes=3)).timestamp()

print(TIMESTAMP)

I am expecting to calculate current time minus 3 minutes and have an output without digits after "."

My output 1723800379.779615, but should be 1723627579


Solution

  • Code Correction

    dateutil provides more flexible timezone handling compared to pytz.

    from datetime import datetime, timedelta
    import dateutil.tz
    tz = dateutil.tz.gettz('Europe/Moscow')
    ct = datetime.now(tz)
    time_minus_3_minutes = ct - timedelta(minutes=3)
    timestamp = int(time_minus_3_minutes.timestamp())#Unix timestamp conversion
    
    print(timestamp)
    

    Output

    1723801868