查看: 3166|回复: 45

C++获取当前时间函数

[复制链接]
发表于 2012-8-31 06:39:06 | 显示全部楼层 |阅读模式
struct timeval {  time_t tv_sec; /* seconds */
  suseconds_t tv_usec; /* microseconds */
  };
  time_t time(time_t *); 获取当前时间为一个长整数
  char * ctime(const time_t *);将时间结构time_t转换为字符串
  char * asctime(const struct tm *);将tm结构转换为字符串
  struct tm * gmtime(const time_t *);将时间结构转换为tm结构 GMT
  struct tm * localtime(const time_t *);将时间结构转换为tm结构 LT
  time_t mktime(struct tm *);将tm结构转换为time_t结构
  double difftime(time_t, time_t);将前一个减去后一个的差额
  clock_t clock(void);返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数
  另外tm结构如下:
  struct tm {
  int tm_sec; /* seconds after the minute - [0,59] * /
  int tm_min; /* minutes after the hour - [0,59] * /
  int tm_hour; /* hours since midnight - [0,23] * /
  int tm_mday; /* day of the month - [1,31] * /
  int tm_mon; /* months since January - [0,11] * /
  int tm_year; /* years since 1900 * /
  int tm_wday; /* days since Sunday - [0,6] * /
  int tm_yday; /* days since January 1 - [0,365] * /
  int tm_isdst; /* daylight savings time flag * /
  };
  strftime()函数将时间格式化
  我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:
  size_t strftime(
  char *strDest,
  size_t maxsize,
  const char *format,
  const struct tm *timeptr
  );
  我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
  函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
  %a 星期几的简写
  %A 星期几的全称
  %b 月分的简写
  %B 月份的全称
  %c 标准的日期的时间串
  %C 年份的后两位数字
  %d 十进制表示的每月的第几天
  %D 月/天/年
  %e 在两字符域中,十进制表示的每月的第几天
  %F 年-月-日
  %g 年份的后两位数字,使用基于周的年
  %G 年分,使用基于周的年
  %h 简写的月份名
  %H 24小时制的小时
  %I 12小时制的小时
  %j 十进制表示的每年的第几天
  %m 十进制表示的月份
  %M 十时制表示的分钟数
  %n 新行符
  %p 本地的AM或PM的等价显示
  %r 12小时的时间
  %R 显示小时和分钟:hh:mm
  %S 十进制的秒数
  %t 水平制表符
  %T 显示时分秒:hh:mm:ss
  %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
  %U 第年的第几周,把星期日做为第一天(值从0到53)
  %V 每年的第几周,使用基于周的年
  %w 十进制表示的星期几(值从0到6,星期天为0)
  %W 每年的第几周,把星期一做为第一天(值从0到53)
  %x 标准的日期串
  %X 标准的时间串
  %y 不带世纪的十进制年份(值从0到99)
  %Y 带世纪部分的十制年份
  %z,%Z 时区名称,如果不能得到时区名称则返回空字符。
  %% 百分号
  如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:
  #i nclude “time.h”
  #i nclude “stdio.h”
  int main(void)
  {
  struct tm *ptr;
  time_t lt;
  char str[80];
  lt=time(NULL);
  ptr=localtime(<);
  strftime(str,100,"It is now %I %p",ptr);
  printf(str);
  return 0;
  }
  其运行结果为:
  It is now 4PM
  而下面的程序则显示当前的完整日期:
  #i nclude
  #i nclude
  void main( void )
  {
  struct tm *newtime;
  char tmpbuf[128];
  time_t lt1;
  time( <1 );
  newtime=localtime(<1);
  strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
  printf(tmpbuf);
  }
  运行结果:
  Today is Saturday, day 30 of July in the year 2005.
发表于 2012-8-31 12:34:57 | 显示全部楼层
牛牛牛牛  
发表于 2012-9-1 02:30:35 | 显示全部楼层
(*^__^*) 嘻嘻……   
发表于 2012-9-1 07:16:24 | 显示全部楼层
努力,努力,再努力!!!!!!!!!!!  
发表于 2012-9-2 06:25:31 | 显示全部楼层
接到陌生电话请先说,“你好,找哪位”  
发表于 2012-9-2 16:17:08 | 显示全部楼层
一定要回贴,因为我是文明人哦  
发表于 2012-9-2 22:22:54 | 显示全部楼层
呵呵 大家好奇嘛 来观看下~~~~  
发表于 2012-9-4 01:19:37 | 显示全部楼层
发贴看看自己积分  
发表于 2012-9-4 03:00:11 | 显示全部楼层
我起来了 哈哈 刚才迷了会  
发表于 2012-9-4 20:15:20 | 显示全部楼层
发贴看看自己积分  
高级模式
B Color Image Link Quote Code Smilies

本版积分规则