Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (116 sloc) 3.71 KB
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="image/x-icon" rel="icon" href="/favicon.ico" />
<link type="image/x-icon" rel="shortcut icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<script type="text/javascript" src="/MathJax/MathJax.js?config=TeX-AMS-MML_SVG"></script>
<title>tips</title>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-35399239-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="content">
<h2 id="toc_0.1">Some tips in C</h2>
<h3 id="toc_0.1.1">libstdc</h3>
<ol>
<li>
<code>opendir()</code>打开目录后,目录内容发生改变,<code>readdir()</code>则不能正确获得目录内容。虽
然概率很低,还是会发生地
<li>
<code>rename()</code>不能跨mount point重命名文件,会返回<code>EBUSY</code>。移动、复制文件很复杂,可
以参考<code>coreutils</code>中<code>src/copy.c</code>的实现。
</ol>
<h3 id="toc_0.1.2">Playing with macro</h3>
<ol>
<li>
多条语句的宏用<code>do { ... } while(0)</code>括住
<li>
Round up to power of 2: (<code>uint32_t</code> only)
<pre class="brush: cpp">
#define round_up_to_next_power_of_2(v) do { \
v--; \
v |= v &gt;&gt; 1; \
v |= v &gt;&gt; 2; \
v |= v &gt;&gt; 4; \
v |= v &gt;&gt; 8; \
v |= v &gt;&gt; 16; \
v++; \
} while (0)
</pre>
</ol>
<h3 id="toc_0.1.3">夏令时</h3>
<p>
夏令时会导致很多异常诡异的时间问题。
</p>
<pre class="brush: cpp">
/* tz-dst.c */
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
int main( void ) {
time_t t;
struct tm temp_tm;
struct tm tm_out;
char tmp_time[32];
strptime("2012-10-04 11:12:13", "%Y-%m-%d %H:%M:%S", &amp;temp_tm);
t = mktime(&amp;temp_tm);
localtime_r(&amp;t, &amp;tm_out);
strftime(tmp_time, 27, "%Y-%m-%d %H:%M:%S", &amp;tm_out);
printf("%d - %s\n", t, tmp_time);
return 0;
}
</pre>
<p>
编译执行,再夏令时时区会有问题:
</p>
<pre class="brush: bash">
$ gcc tz-dst.c -o dst-test
$ TZ=CST6CET ./dst-test # CST +6 with daylight saving time
1349370733 - 2012-10-04 12:12:13
$ TZ=CST6 ./dst-test # CST +6 without daylight saving time
1349370733 - 2012-10-04 11:12:13
</pre>
<p>
秒数相同,但是转换后时间相差一小时。<code>struct tm</code>结构中<code>tm_isdst</code>可以用于判断当前
是否处于夏时令。<code>localtime()</code>, <code>localtime_r()</code>, <code>mktime()</code>不会参考这个值,而是从
环境变量<code>TZ</code>中获取是否考虑夏令时。
</p>
<p>
强制<code>mktime()</code>返回<code>UTC</code>时间的方法:
</p>
<pre class="brush: cpp">
time_t mktime_utc(struct tm * timeinfo) {
char * tz_old = getenv("TZ");
time_t ret = 0;
putenv("TZ=UTC");
_tzset();
ret = mktime(timeinfo);
if (!tz_old) {
setenv("TZ=");
} else {
char buf[64];
sprintf(buf, "TZ=%s", tz_old);
setenv(buf);
}
_tzset();
return ret;
}
</pre>
</div>
<div id="copyright"></div>
<div id="disqus_thread"></div>
<div class="footer"></div>
</body>
</script>
<script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/js/wiki.js" type="text/javascript"></script>
</html>