Python: timezone.localize () not working

I'm having trouble getting it timezone.localize()working correctly. My goal is to capture today's date and convert it from CST to EST. Then finally format the date and time before spitting it out. I can format the date correctly, but the date-time does not change from CST to EST. Also, when I format the date, I do not see the textual representation of the included time zone.

Below I have listed a simple program that I created to test this:

#! /usr/bin/python
#Test script

import threading
import datetime
import pexpect
import pxssh
import threading
from pytz import timezone
import pytz

est = timezone('US/Eastern')
curtime = est.localize(datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y"))
#test time change
#curtime = datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y")

class ThreadClass(threading.Thread):
  def run(self):
    #now = (datetime.datetime.now() + datetime.timedelta(0, 3600))
    now = (datetime.datetime.now())
    print "%s says Hello World at time: %s" % (self.getName(), curtime)

for i in range(3):
  t = ThreadClass()
  t.start()
+5
source share
2 answers

.localize() datetime , . . datetime , .

now() , .astimezone() :

est = timezone('US/Eastern')
cst = timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
est_curtime = curtime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y"))

def run(self):
    print "%s says Hello World at time: %s" % (self.getName(), est_curtime)
+8

cst.localize, datetime datetime, .

astimezone, , , .

import pytz
import datetime

est = pytz.timezone('US/Eastern')
cst = pytz.timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
curtime = curtime.astimezone(est)
+4

All Articles