Tag Archives: Time Zone

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

Advertisement

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

PostgreSQL – Set and Get Timezone

There is a timezone configuration on PostgreSQL server. If you want to change the default globally, you can edit the postgresql.conf. On Ubuntu, it is located at

  • /etc/postgresql/<version>/main/postgresql.conf
# - Locale and Formatting -

datestyle = 'iso, mdy'
#intervalstyle = 'postgres'
#timezone = 'Hongkong'
timezone = 'UTC'

 

We could also tweak the timezone only in your current PostgreSQL session.

1. Get the current timezone.

SELECT current_setting('TIMEZONE');

Continue reading PostgreSQL – Set and Get Timezone

CakePHP – Apply Time Zone for Users

Update @ 2012-09-25: This approach could not handle the DST zones. Thanks for the comment made by Lucas. =)

You can make use the TimeHelper to offset the time by user specified time zone. What u need is adding a new time_zone column in the users table just like the following example.

--MySQL
CREATE TABLE IF NOT EXISTS users (
	user_id INT NOT NULL auto_increment,
	name VARCHAR(45) NOT NULL,
	password VARCHAR(45) NOT NULL,
	time_zone DECIMAL(5,1) NOT NULL,
	created DATETIME DEFAULT NULL,
	PRIMARY KEY (user_id)
) ENGINE = InnoDB;

Continue reading CakePHP – Apply Time Zone for Users