<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FALLHUNTER &#187; 时区</title>
	<atom:link href="http://www.fallhunter.com/p/tag/%e6%97%b6%e5%8c%ba/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fallhunter.com</link>
	<description>Web &#38; Mobile, Design &#38; Code</description>
	<lastBuildDate>Tue, 08 Feb 2011 11:07:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Python 与 Django 的时区问题</title>
		<link>http://www.fallhunter.com/p/10135</link>
		<comments>http://www.fallhunter.com/p/10135#comments</comments>
		<pubDate>Sun, 22 Nov 2009 10:10:55 +0000</pubDate>
		<dc:creator>fallhunter</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[时区]]></category>

		<guid isPermaLink="false">http://www.fallhunter.com/2009/11/python-%e4%b8%8e-django-%e7%9a%84%e6%97%b6%e5%8c%ba%e9%97%ae%e9%a2%98/</guid>
		<description><![CDATA[在编码中牵扯到时间问题的时候，总是容易被时区问题搞混，一直以来，都是反复试验应付过去，今天终于搞清楚了个中缘由，一个心结也得以化解。 Python 的时区问题 datetime.today() / datetime.now() 这两个函数获得的是当前的系统时间，但得到的datetime对象中的tzinfo是空的，即使系统中设置了时区。 datetime.utcnow() 这个函数获得当前的utc时间，应该是根据当前系统时间和时区来计算的。 例如系统时间为14：00，时区为 Asia/Shanghai (北京时间)，utcnow返回时间为 6：00。同样，得到的对象中的tzinfo 为空。 环境变量 TZ 对以上函数的影响： 当系统中设置了环境变量 TZ 的时候，或者在python中设置了 os.environ[‘TZ’] 的时候，上面的函数获取的时间便是TZ对应时区的时间。其实这里可以认为 TZ 影响的不是这些函数，而是影响的系统时间，这可以从date命令的返回结果看出。datetime.now() 与 date命令返回的结果总是一致的。 Django的时区问题 明白了上面几个python中的函数，django的时区问题看起来就简单了。 在django的setting中，有一个设置是 TIME_ZONE， 来设置程序中使用的时区。 从django的文档中得知，TIME_ZONE的作用就是改变 os.environ[‘TZ’]  ，但改变os.environ[‘TZ’]  并不会改变系统环境变量 TZ ， 因此，如果 TIME_ZONE 的设置于系统时区设置不一致，则在程序中 datetime.now() 获得的时间就与 date 命令的时间不一致了。 因此，TIME_ZONE 应该设置为程序希望使用的时区。对于一个本地的程序，TIME_ZONE 设置为与系统时区一样即可；而对于一个国际化的应用，TIME_ZONE 最好设置为UTC，在显示的时候再根据当前用户所在的时区做调整。 Related posts:GO GO GO, when Python meets [...]<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">无觅猜您也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10138&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">几个django相关的网站</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10194&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">几个 Django 第三方库/applications</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10184&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">django 1.2 alpha</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10149&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">使用 python/django 发送iPhone push消息</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>


Related posts:<ol><li><a href='http://www.fallhunter.com/p/10129' rel='bookmark' title='Permanent Link: GO GO GO,  when Python meets C++'>GO GO GO,  when Python meets C++</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>在编码中牵扯到时间问题的时候，总是容易被时区问题搞混，一直以来，都是反复试验应付过去，今天终于搞清楚了个中缘由，一个心结也得以化解。</p>
<h2>Python 的时区问题</h2>
<ul>
<li>datetime.today() / datetime.now()<br />
这两个函数获得的是当前的系统时间，但得到的datetime对象中的tzinfo是空的，即使系统中设置了时区。</li>
<li>datetime.utcnow()<br />
这个函数获得当前的utc时间，应该是根据当前系统时间和时区来计算的。<br />
例如系统时间为14：00，时区为 Asia/Shanghai (北京时间)，utcnow返回时间为 6：00。同样，得到的对象中的tzinfo 为空。</li>
</ul>
<p>环境变量 TZ 对以上函数的影响：<br />
当系统中设置了环境变量 TZ 的时候，或者在python中设置了 os.environ[‘TZ’] 的时候，上面的函数获取的时间便是TZ对应时区的时间。其实这里可以认为 TZ 影响的不是这些函数，而是影响的系统时间，这可以从date命令的返回结果看出。datetime.now() 与 date命令返回的结果总是一致的。</p>
<h2>Django的时区问题</h2>
<p>明白了上面几个python中的函数，django的时区问题看起来就简单了。</p>
<p>在django的setting中，有一个设置是 TIME_ZONE， 来设置程序中使用的时区。</p>
<p>从django的文档中得知，TIME_ZONE的作用就是改变 os.environ[‘TZ’]  ，但改变os.environ[‘TZ’]  并不会改变系统环境变量 TZ ， 因此，如果 TIME_ZONE 的设置于系统时区设置不一致，则在程序中 datetime.now() 获得的时间就与 date 命令的时间不一致了。</p>
<p>因此，TIME_ZONE 应该设置为程序希望使用的时区。对于一个本地的程序，TIME_ZONE 设置为与系统时区一样即可；而对于一个国际化的应用，TIME_ZONE 最好设置为UTC，在显示的时候再根据当前用户所在的时区做调整。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">无觅猜您也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10138&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">几个django相关的网站</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10194&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">几个 Django 第三方库/applications</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10184&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">django 1.2 alpha</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10149&from=http%3A%2F%2Fwww.fallhunter.com%2Fp%2F10135">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">使用 python/django 发送iPhone push消息</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>

<p>Related posts:<ol><li><a href='http://www.fallhunter.com/p/10129' rel='bookmark' title='Permanent Link: GO GO GO,  when Python meets C++'>GO GO GO,  when Python meets C++</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.fallhunter.com/p/10135/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

