ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux获取系统时间

2022-02-04 23:03:49  阅读:174  来源: 互联网

标签:02d int linux 系统 number 获取 range tm time


linux获取系统时间

0.相关结构体介绍

// Broken-down time is stored in the structure tm, which is defined in <time.h> as follows:
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
/*
tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be
up to 60 to allow for leap seconds.

tm_min The number of minutes after the hour, in the range 0 to 59.

tm_hour The number of hours past midnight, in the range 0 to 23.

tm_mday The day of the month, in the range 1 to 31.

tm_mon The number of months since January, in the range 0 to 11.

tm_year The number of years since 1900.

tm_wday The number of days since Sunday, in the range 0 to 6.

tm_yday The number of days since January 1, in the range 0 to 365.

tm_isdst A flag that indicates whether daylight saving time is in effect at the time
described. The value is positive if daylight saving time is in effect, zero if it
is not, and negative if the information is not available.
*/

1.使用步骤

(1)包含time.h头文件

(2)通过time函数获取时间到time_t对象中

(3)通过gmtime函数吧time_t中的内容格式化到struct tm结构体对象中

2.示例代码

#include <stdio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
  time_t t;
  struct tm *p_time = NULL;

  time(&t);
  p_time = gmtime(&t);
  if(NULL != p_time)
    printf("%04d年%02d月%02d日:%02d时%02d分%02d秒\n",
      (1900+p_time->tm_year), p_time->tm_mon, p_time->tm_mday,
      (8+p_time->tm_hour), p_time->tm_min, p_time->tm_sec);
  else
    printf("获取时间失败!\n");

  return 0;
}

 注:年需要加上偏移量1900,小时需要加上偏移量8

标签:02d,int,linux,系统,number,获取,range,tm,time
来源: https://www.cnblogs.com/veis/p/15863574.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有