Skip to content

Instantly share code, notes, and snippets.

@atelierhide
Created March 12, 2015 03:28
Show Gist options
  • Save atelierhide/ff1ff33880a78b19d2a0 to your computer and use it in GitHub Desktop.
Save atelierhide/ff1ff33880a78b19d2a0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Work with Timezones in Python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hideki Tanaka (@atelierhide) 11/03/2015 \n",
"\n",
"CPython 2.7.9\n",
"IPython 3.0.0\n"
]
}
],
"source": [
"%load_ext watermark\n",
"%watermark -a 'Hideki Tanaka (@atelierhide)' -v -d"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"from pytz import timezone"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"jst = timezone('Asia/Tokyo')\n",
"pdt = timezone('US/Pacific')\n",
"fmt = '%Y-%m-%d %H:%M:%S %Z'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2015-03-11 15:32:28 JST\n",
"2015-03-10 23:32:28 PDT\n"
]
}
],
"source": [
"print datetime.now(jst).strftime(fmt)\n",
"print datetime.now(pdt).strftime(fmt)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2015-03-11 14:46:18 JST\n"
]
}
],
"source": [
"print datetime(2015, 3, 11, 14, 46, 18, tzinfo=jst).strftime(fmt)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Today's the 4th anniversary of Great East Japan Earthquake.\n"
]
}
],
"source": [
"t1 = datetime(2011, 3, 11, 14, 46, 18, tzinfo=jst)\n",
"t2 = datetime(2015, 3, 11, 14, 46, 18, tzinfo=jst)\n",
"dif_year = (t2 - t1).total_seconds()//(60*60*24*365)\n",
"print \"Today's the {:.0f}th anniversary of Great East Japan Earthquake.\".format(dif_year)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment