{"id":802,"date":"2012-08-04T23:29:24","date_gmt":"2012-08-04T23:29:24","guid":{"rendered":"http:\/\/joelinoff.com\/blog\/?p=802"},"modified":"2012-08-04T23:29:24","modified_gmt":"2012-08-04T23:29:24","slug":"python-datetime-datetime-now-that-is-timezone-aware","status":"publish","type":"post","link":"https:\/\/joelinoff.com\/blog\/?p=802","title":{"rendered":"Python datetime.datetime.now() that is timezone aware"},"content":{"rendered":"<p>I recently needed to print out the time with timeszone name but when I used the %Z paramter it was blank. I fixed it by using the dateutil package as shown in the short example below.<br \/>\n<!--more--><br \/>\n[crayon lang=&#8221;python&#8221; toolbar=&#8221;always&#8221; title=&#8221;datetime with tzone&#8221;]<br \/>\n#!\/usr\/bin\/env python<br \/>\nimport datetime<br \/>\nfrom dateutil.tz import tzlocal<\/p>\n<p># Get the current date\/time with the timezone.<br \/>\nnow = datetime.datetime.now(tzlocal())<br \/>\nfmt1 = now.strftime(&#8216;%Y-%m-%d %H:%M:%S %Z&#8217;)<br \/>\nfmt2 = now.strftime(&#8216;%A, %B %d, %Y %Z&#8217;)<\/p>\n<p># Print it out.<br \/>\nprint &#8216;fmt1 = %s&#8217; % (fmt1)<br \/>\nprint &#8216;fmt2 = %s&#8217; % (fmt2)<br \/>\n[\/crayon]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently needed to print out the time with timeszone name but when I used the %Z paramter it was blank. I fixed it by using the dateutil package as shown in the short example below.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/802"}],"collection":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=802"}],"version-history":[{"count":3,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/802\/revisions"}],"predecessor-version":[{"id":805,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/802\/revisions\/805"}],"wp:attachment":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}