SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档320

  • 关于

  • 搜索

C/C++参考文档:C Time & Date

发表于 2020-01-27 更新于 2021-10-29 分类于 计算机 , 技术 , C/C++ 阅读次数: Valine:
本文字数: 2.3k 阅读时长 ≈ 2 分钟

C/C++参考文档

标准C时间与日期函数

asctime

语法:

1
2
#include <time.h>
char *asctime( const struct tm *ptr );

功能: 函数将ptr所指向的时间结构转换成下列字符串:

1
day month date hours:minutes:seconds year\n\0

  例如:

1
Mon Jun 26 12:03:53 2000

clock

语法:

1
2
#include <time.h>
clock_t clock( void );

功能:函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。 转换返回值以秒记, 返回值除以CLOCKS_PER_SECOND。 (注: 如果编译器是POSIX兼容的, CLOCKS_PER_SECOND定义为1000000。)


ctime

语法:

1
2
#include <time.h>
char *ctime( const time_t *time );

功能:函数转换参数time为本地时间格式:

1
day month date hours:minutes:seconds year\n\0

  ctime() 等同

1
asctime( localtime( tp ) );

difftime

语法:

1
2
#include <time.h>
double difftime( time_t time2, time_t time1 );

功能:函数返回时间参数time2和time1之差的秒数表示。


gmtime

语法:

1
2
#include <time.h>
struct tm *gmtime( const time_t *time );

功能:函数返回给定的统一世界时间(通常是格林威治时间),如果系统不支持统一世界时间系统返回NULL。警告!


localtime

语法:

1
2
#include <time.h>
struct tm *localtime( const time_t *time );

功能:函数返回本地日历时间。警告!


mktime

语法:

1
2
#include <time.h>
time_t mktime( struct tm *time );

功能:函数转换参数time 类型的本地时间至日历时间,并返回结果。如果发生错误,返回-1。


strftime

语法:

1
2
#include <time.h>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,然后存储在字符串str中(至多maxsize 个字符)。用于设定时间不同类型的代码为:

代码 含义
%a 星期的缩略形式
%A 星期的完整形式
%b 月份的缩略形式
%B 月份的完整形式
%c 月份的缩略形式
%d 月中的第几天(1-31)
%H 小时, 24小时格式 (0-23)
%I 小时, 12小时格式 (1-12)
%j 年中的第几天(1-366)
%m 月份 (1-12)。 Note: 某些版本的Microsoft Visual C++ 可能使用取值范围0-11。
%M 分钟(0-59)
%p 本地时间的上午或下午(AM or PM)
%S 秒钟(0-59)
%U 年中的第几周,星期天是一周的第一天
%w 星期几的数字表示(0-6, 星期天=0)
%W 一年中的第几周,星期天是一周的第一天
%x 标准日期字符串
%X 标准时间字符串
%y 年(0-99)
%Y 用CCYY表示的年(如:2004)
%Z 时区名
%% 百分号

  函数strftime()返回值为处理结果字符串str中字符的个数,如果发生错误返回零。


time

语法:

1
2
#include <time.h>
time_t time( time_t *time );

功能: 函数返回当前时间,如果发生错误返回零。如果给定参数time ,那么当前时间存储到参数time中。


**警告!**

  This function returns a variable that is statically located, and therefore overwritten each time this function is called. If you want to save the return value of this function, you should manually save it elsewhere.
  Of course, when you save it elsewhere, you should make sure to actually copy the value(s) of this variable to another location. If the return value is a struct, you should make a new struct, then copy over the members of the struct.

-------- 本文结束 感谢阅读 --------
相关文章
  • C/CPP参考文档:C I/O
  • C/C++参考文档:C Memory
  • C/C++参考文档:C String & Character
  • C/CPP参考文档:C++ Algorithm
  • C/CPP参考文档:C Math
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: C/C++参考文档:C Time & Date
  • 本文作者: SiriYang
  • 创建时间: 2020年01月27日 - 11时01分
  • 修改时间: 2021年10月29日 - 18时10分
  • 本文链接: https://blog.siriyang.cn/posts/20200127112219id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
C/C++ 文档
CCF-CSP/201312-3最大的矩形
C/C++参考文档:C Memory
  • 文章目录
  • 站点概览
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
320 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

  1. 标准C时间与日期函数
    1. asctime
    2. clock
    3. ctime
    4. difftime
    5. gmtime
    6. localtime
    7. mktime
    8. strftime
    9. time
蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:41
0%