?? apr-tutorial-9.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.21"> <TITLE>libapr(apache portable runtime) programming tutorial: time handling</TITLE> <LINK HREF="apr-tutorial-10.html" REL=next> <LINK HREF="apr-tutorial-8.html" REL=previous> <LINK HREF="apr-tutorial.html#toc9" REL=contents></HEAD><BODY><A HREF="apr-tutorial-10.html">Next</A><A HREF="apr-tutorial-8.html">Previous</A><A HREF="apr-tutorial.html#toc9">Contents</A><HR><H2><A NAME="s9">9.</A> <A HREF="apr-tutorial.html#toc9">time handling</A></H2><P>libapr's time APIs are almost based on POSIX scheme. The value of apr_time_t is the elapsed time since UNIX epoch time(1970/1/1). The big differences are that apr_time_t is 64bit(long long) number and it represents microseconds. The most useful API is apr_time_now(). As you can guess, it returns the current time. You can find the protype declaration in apr_time.h.</P><P>/* excerpted from apr_time.h */<BLOCKQUOTE><CODE><PRE>APR_DECLARE(apr_time_t) apr_time_now(void);</PRE></CODE></BLOCKQUOTE></P><P>In many cases, we need to convert apr_time_t value to the other formats. There are mainly two formats.- apr_time_exp_t (time structure)- string formats (e.g. rfc822)</P><P>We use the following APIs to convert apr_time_t to apr_time_exp_t structure.</P><P>/* excerpted from apr_time.h */<BLOCKQUOTE><CODE><PRE>APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result, apr_time_t input);APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result, apr_time_t input);</PRE></CODE></BLOCKQUOTE></P><P>The apr_time_exp_gmt() returns the result in GMT timezone, and apr_time_exp_lt() returns the result in local timezone. The first argument of both APIs is result argument.</P><P>We can do the opposite conversion. We use the following API to convert apr_time_exp_t structure to apr_time_t value.</P><P>/* excerpted from apr_time.h */<BLOCKQUOTE><CODE><PRE>APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *result, apr_time_exp_t *input);</PRE></CODE></BLOCKQUOTE></P><P>There are some APIs to convert apr_time_t to various string formats as follows:</P><P>/* excerpted from apr_time.h */<BLOCKQUOTE><CODE><PRE>APR_DECLARE(apr_status_t) apr_rfc822_date(char *date_str, apr_time_t t);APR_DECLARE(apr_status_t) apr_ctime(char *date_str, apr_time_t t);APR_DECLARE(apr_status_t) apr_strftime(char *s, apr_size_t *retsize, apr_size_t max, const char *format, apr_time_exp_t *tm);</PRE></CODE></BLOCKQUOTE></P><P>On the other hand, if we convert such string formats to apr_time_t value, we have to call apr-util APIs, which are defined in apr_date.h.</P><P>Please take a look at <A HREF="../sample/time-sample.c">time-sample.c</A> about the usage of time APIs. </P><P><EM>REMARK</EM>: As stated above, apr_time_t is long long type (64bit). Note that the following sample code causes overflow.</P><P><BLOCKQUOTE><CODE><PRE>/* BUGGY sample. This overflows */const apr_time_t ONE_HOUR = 1000 * 1000 * 60 * 60;</PRE></CODE></BLOCKQUOTE></P><P>We can fix the bug by explicit type cast, but I recommend you to use implicit type cast which libapr provides. The following code shows it.</P><P><BLOCKQUOTE><CODE><PRE>/* two examples to get around overflow above */const apr_time_t ONE_HOUR = APR_TIME_C(1000) * 1000 * 60 * 60;orconst apr_time_t ONE_HOUR = APR_USEC_PER_SEC * 60 * 60;</PRE></CODE></BLOCKQUOTE></P><P><EM>REMARK</EM>: Sometimes, often in debugging, we want to print out time values. Unfortunately, Unix and Windows have different printf(3) format specifier for 64bit value. On Unix it is "%lld" and on Windows it is "%I64d". For such portability issues, libapr provides format specifiers, e.g. APR_INT64_T_FMT. There is APR_TIME_T_FMT in apr_time.h. We can write portable code with such format specifiers.</P><P><BLOCKQUOTE><CODE><PRE>/* On Unix, APR_INT64_T_FMT is defined in apr.h */#define APR_INT64_T_FMT "lld"/* On Windows, APR_INT64_T_FMT is defined in apr.h */#define APR_INT64_T_FMT "I64d"/* excerpted from apr_time.h */#define APR_TIME_T_FMT APR_INT64_T_FMT/* We can use APR_TIME_T_FMT as follows */printf("The current time: %" APR_TIME_T_FMT "[us]\n", apr_time_now());</PRE></CODE></BLOCKQUOTE></P><HR><A HREF="apr-tutorial-10.html">Next</A><A HREF="apr-tutorial-8.html">Previous</A><A HREF="apr-tutorial.html#toc9">Contents</A></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -