Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1488537
  • 博文数量: 226
  • 博客积分: 3997
  • 博客等级: 少校
  • 技术积分: 2369
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-19 17:26
个人简介

Never save something for a special occasion. Every day in your life is a special occasion.

文章分类

全部博文(226)

文章存档

2018年(5)

2017年(11)

2016年(1)

2015年(17)

2014年(14)

2013年(30)

2012年(5)

2011年(52)

2010年(107)

分类: C/C++

2013-09-13 20:26:39

时间编程是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;
}


时间编程:获取当前时间


点击(此处)折叠或打开

  1. /*
  2. Getting the Current Time
  3. */
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <sys/time.h>


  7. int main(void)
  8. {
  9.     // s
  10.     time_t result;
  11.     result = time(NULL);
  12.     printf("ctime=localtime+asctime :%s", ctime(&result));
  13.     printf("local time:%s", asctime(localtime(&result)));
  14.     printf("local time_t:%ju secs since the Epoch\n", (uintmax_t)result);


  15.     // us
  16.     struct timeval now;
  17.     gettimeofday(&now, NULL);
  18.     printf("local timeval = %u.%06u\n",
  19.      now.tv_sec, now.tv_usec);


  20.     return (0);
  21. }


更多时间函数 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


阅读(1592) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~