struct tm->time() localtime() gmtime()

struct tm {
int tm_sec; /*代表目前秒数,正常范围为0-59,但允许至61秒 */
int tm_min; /*代表目前分数,范围0-59*/
int tm_hour; /*从午夜算起的时数,范围为0-23*/
int tm_mday; /*目前月份的日数,范围01-31*/
int tm_mon; /*代表目前月份,从一月算起,范围从0-11*/
int tm_year; /*从1900 年算起至今的年数*/
int tm_wday; /*一星期的日数,从星期一算起,范围为0-6 */
int tm_yday; /*从今年1月1日算起至今的天数,范围为0-365*/
int tm_isdst; /*日光节约时间的旗标*/
};

time()

编程语言C语言中的函数。
头文件:time.h
函数原型:time_t time(time_t * timer)
功能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。
补充说明:time函数的原型也可以理解为 long time(long *tloc),即返回一个long型整数。因为在time.h这个头文件中time_t实际上就是:
#ifndef _TIME_T_DEFINED
typedef long time_t; /* time value */
#define _TIME_T_DEFINED /* avoid multiple defines of time_t */
#endif
即long。
#include <stdio.h>
#include <stddef.h>
#include <time.h>
int main(void)
{
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//这一句也可以改成time(&timer);
tblock = localtime(&timer);
printf("Local time is: %s\n",asctime(tblock));
return 0;
}
 
localtime()
功 能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为日历时间 。
说明:此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间。
用 法: struct tm *localtime(const time_t *clock);
返回值:返回指向tm 结构体指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.
需要注意的是年份加上1900,月份加上1。
#include<time.h>
#include<stdio.h>
int main()
{
struct tm *t;
time_t tt;
time(&tt);
t=localtime(&tt);
printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
return 0;
}

gmtime()

头文件:time.h
原型:struct tm *gmtime(long *clock);
功能:把日期和时间转换为格林威治(GMT)时间的函数。将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值 返回结构tm代表目前UTC 时间
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
int main(void)
{
time_t t;
struct tm *gmt, *area;
tzset(); /* tzset()*/
t = time(NULL);
area = localtime(&t);
printf("Local time is: %s", asctime(area));
gmt = gmtime(&t);
printf("GMT is: %s", asctime(gmt));
return 0;
}

struct tm->time() localtime() gmtime()的更多相关文章

  1. C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换

    使用gmtime函数或localtime函数将time_t类型的时间日期转换为structtm类型: 使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年.月.日 ...

  2. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...

  3. struct tm 和 time_t 时间和日期的使用方法(转

    关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) .概念 在C/C++中,对字符串的操作有很多值得注意的问题,同样,C ...

  4. time_t和struct tm之间的转换

    time_t到struct tm的转换: #include <time.h> struct tm *localtime(const time_t *timep); struct tm到ti ...

  5. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

  6. 时间操作(struct tm、time_t)求指定日期 前n天的日期

    1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值 ...

  7. struct tm

    struct tm { int tm_sec; /* 秒–取值区间为[0,59] */ int tm_min; /* 分 - 取值区间为[0,59] */ int tm_hour; /* 时 - 取值 ...

  8. localtime和localtime_r

    上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...

  9. C语言中tm结构体

    struct tm { int tm_sec; /* Seconds. [0-60] (1 leap second) */ int tm_min; /* Minutes. [0-59] */ int ...

随机推荐

  1. MySQL原生HA方案 – Fabric体验之旅

    http://www.csdn.net/article/2014-08-20/2821300

  2. Xamarin.Forms DataGrid

    控件出处 https://components.xamarin.com/ https://components.xamarin.com/gettingstarted/ZumeroDataGrid/tr ...

  3. Building Web Apps with SignalR, Part 1

    Building Web Apps with SignalR, Part 1 In the first installment of app-building with SignalR, learn ...

  4. Android ListView嵌套Button,Button事件覆盖item事件解决办法

    方法就是修改item布局的xml文件: 在根布局里加上: android:descendantFocusability="blocksDescendants" 然后在按钮布局里加上 ...

  5. C语言自动类型转换

    自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.(eg:int型和long型运算时,先把int量转成lo ...

  6. Highcharts 带有数据标签曲线图表

    <html> <head> <meta charset="UTF-8" /> <title>Highcharts</title ...

  7. (转)最近研究xcodebuild批量打包的一些心得

    以前的时候只知道做安卓开发的兄弟挺辛苦的,不但开发的时候要适配一堆的机型,好不容易开发完了还要打一堆不同的包给不同的市场.没想到现在这些市场都开辟iOS市场,于是需要打一堆的包给不同的市场,面对暂时给 ...

  8. volatile-java关键字

    volatile的作用: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序: XBYTE[2]=0x55; XBYTE[2 ...

  9. [SVG] Simple introduce for SVG

    Just like create html page, you can create a svg tag by: <?xml version="1.0" encoding=& ...

  10. 使用CXF+spring创建一个web的接口项目

    一.web project整合spring 1.1.打开Myeclipse,建立web project(eclipse为dynamic web project),使用J2EE5.0. 1.2.加入Sr ...