时间编程是LinuxC的重要部分,这里介绍几个常用时间相关函数,s 和 ms 级时间。
头文件:
#include
#include // for gettimeofday.
时间的获取和转换
获取世界统一时间,秒级。
time_t time(time_t *tloc); // UTC
获取本地时间,毫秒级。
int gettimeofday(struct timeval *restrict tp, void *restrict tzp); // 今天,这可是一个本地时间哦。
转换为本地时间,顺便把格式也转换一下。// 为什么要顺便转换格式?想想谁会只需要一个本地的秒呢。
struct tm *localtime(const time_t *timer);
转换为字符串
char *asctime(const struct tm *timeptr); // 转换为串 struct tm -> char*
char *ctime(const time_t *clock); // ctime = localtime + asctime
转换时间格式:time_t -> struct tm
struct tm *gmtime(const time_t *timer);
OK,这几个函数也许不够使用,不过还是先来巩固一下:
下面是这些函数的help和示例。
获取时间
函数:
#include
time_t time(time_t *tloc);
说明:
The time() function shall return the value of time in seconds since the Epoch.
函数:
#include
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
说明:
The gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tp. The resolution of the system clock is unspecified.
格式转换
函数:
#include
struct tm *gmtime(const time_t *timer);
说明:
The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).
函数:
#include
struct tm *localtime(const time_t *timer);
说明:
The localtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as a local time.
The function corrects for the timezone and any seasonal time adjustments. Local timezone information is used as though localtime() calls tzset().
函数:
#include
char *asctime(const struct tm *timeptr);
说明:
The asctime() function shall convert the broken-down time in the structure pointed to by timeptr into a string in the form:
Sun Sep 16 01:03:52 1973\n\0
using the equivalent of the following algorithm:
char *asctime(const struct tm *timeptr)
{
static char wday_name[7][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char mon_name[12][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char result[26];
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 + timeptr->tm_year);
return result;
}
时间编程:获取当前时间
-
/*
-
Getting the Current Time
-
*/
-
#include <stdio.h>
-
#include <time.h>
-
#include <sys/time.h>
-
-
-
int main(void)
-
{
-
// s
-
time_t result;
-
result = time(NULL);
-
printf("ctime=localtime+asctime :%s", ctime(&result));
-
printf("local time:%s", asctime(localtime(&result)));
-
printf("local time_t:%ju secs since the Epoch\n", (uintmax_t)result);
-
-
-
// us
-
struct timeval now;
-
gettimeofday(&now, NULL);
-
printf("local timeval = %u.%06u\n",
-
now.tv_sec, now.tv_usec);
-
-
-
return (0);
-
}
更多时间函数 ref:
Linux C 时间操作相关函数分析-ChinaUnix
http://blog.chinaunix.net/uid-26642637-id-3898652.html
更详细的说明 ref:
Linux时间操作(time、gettimeofday)-CSDN.NET
http://blog.csdn.net/scottgly/article/details/6568513
阅读(1646) | 评论(0) | 转发(0) |