Category Archives: Python

Python – Convert a timezone aware datetime to UTC datetime using pytz

Previously:

 

If we have a datetime with timezone, we can also convert it to UTC datetime using pytz.

import pytz
import datetime

local_tz = pytz.timezone ("Asia/Hong_Kong")
datetime_without_tz = datetime.datetime.strptime("2015-02-14 12:34:56", "%Y-%m-%d %H:%M:%S")
datetime_with_tz = local_tz.localize(datetime_without_tz, is_dst=None) # No daylight saving time
datetime_in_utc = datetime_with_tz.astimezone(pytz.utc)

str1 = datetime_without_tz.strftime('%Y-%m-%d %H:%M:%S %Z')
str2 = datetime_with_tz.strftime('%Y-%m-%d %H:%M:%S %Z')
str3 = datetime_in_utc.strftime('%Y-%m-%d %H:%M:%S %Z')

print 'Without Timzone : %s' % (str1)
print 'With Timezone   : %s' % (str2)
print 'UTC Datetime    : %s' % (str3)

Continue reading Python – Convert a timezone aware datetime to UTC datetime using pytz

Python – Specify datetime timezone using pytz

We can use the dateutil package to create timezone aware datetime.

 

We can also use the pytz to specify the timezone. Try the following example.

import pytz
import datetime

local_tz = pytz.timezone ("Asia/Hong_Kong")
datetime_without_tz = datetime.datetime.strptime("2015-02-14 12:34:56", "%Y-%m-%d %H:%M:%S")
datetime_with_tz = local_tz.localize(datetime_without_tz, is_dst=None) # No daylight saving time

str1 = datetime_without_tz.strftime('%Y-%m-%d %H:%M:%S %Z')
str2 = datetime_with_tz.strftime('%Y-%m-%d %H:%M:%S %Z')

print 'Without Timzone : %s' % (str1)
print 'With Timezone   : %s' % (str2)

Continue reading Python – Specify datetime timezone using pytz

Python – Create current datetime object with timezone

Handling timezone is quite a pain in Python. When you create or read a datetime object, you need to make sure whether the datetime you need should be timezone aware or not.

Here is a simple example to create a datetime with timezone. The tzlocal will determine the timezone setting of the operating system and apply it to your datetime object.

import datetime
from dateutil.tz import tzlocal
 
now_without_tz = datetime.datetime.now()
now_with_tz    = datetime.datetime.now(tzlocal())

str1 = now_without_tz.strftime('%Y-%m-%d %H:%M:%S %Z')
str2 = now_with_tz.strftime('%Y-%m-%d %H:%M:%S %Z')
 
print 'Without Timzone : %s' % (str1)
print 'With Timezone   : %s' % (str2)

Continue reading Python – Create current datetime object with timezone

Python – Open URL with HTTP Authentication using urllib2

Update @ 2015-04-01: Mart suggests using the Requests package. =)

We could add HTTP authentication credential when opening and URL using urllib2.

import urllib2
import base64
import simplejson

username = "<username>"
password = "<password>"
request = urllib2.Request("http://127.0.0.1:8000/custom/get/")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)

data = simplejson.load(response)
print data

 

Done =)

Reference: StackOverflow – Python urllib2 Basic Auth Problem

Python – cx_Oracle throws missing libclntsh.so.11.1 when executed by django-chronograph

I have setup the cx_Oracle and i could query the data thru the custom django-admin command.

 

But when i try to schedule the custom command using django-chronograph. It throws the following error.

Traceback (most recent call last):
  File "query.py", line 7, in ?
    import cx_Oracle
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

Continue reading Python – cx_Oracle throws missing libclntsh.so.11.1 when executed by django-chronograph

Python – Install cx_Oracle

I want to query data from an Oracle database from a Python program and this could be done by using the cx_Oracle Python package. This package required some Oracle package pre-requisites so it cannot be installed thru pip directly.

The following steps are done on a Centos 7 machine with Python 2.7.

1. Download and install the following from Oracle.

  • oracle-instantclient11.2-basic
  • oracle-instantclient11.2-devel
  • oracle-instantclient11.2-sqlplus

 

2. The installed client should be located at /usr/lib/oracle/11.2/client64.
Continue reading Python – Install cx_Oracle