根据时间戳得到常见的日期格式

linux系统几个常用的时间函数的操作
在学习时间函数之前,需要知道几个概念
GMT 格林尼治标准时间(Greenwich Mean Time),该时间是地球上0经度所在的时间,对应的时区是0时区,北京则位于东八区,也就是说,北京时间比格林尼治标准时间早8个小时。
UTC 协调世界时(Coordinated Universal Time),在计算机系统中,它已经成为一个标准,在不太精确的情况下,UTC时间与GMT时间可以认为是一样的。
CST 北京时间(Chinese Standard Time),即中国标准时间。
时间戳 一个数字,在linux世界中,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。


linux提供了几个函数,来为大家方便地使用UTC时间和CST时间和时间戳。


time(),可以取得当前时间的时间戳。
gmtime(),可以将一个时间戳转换成一个UTC时间的struct tm的结构体指针。通过该指针,可以方便地得到对应的年月日和时分秒等。gm既是Greenwich Mean的缩写。
localtime(),可以将一个时间戳转换成一个当地时间(中国即北京时间)的struct tm的结构体指针。通过该指针,可以方便地得到对应的年月日和时分秒等。
asctime(),返回值是一个字符串格式的时间的描述。它的参数是struct tm结构体的指针,它不管这个指针的内容是UTC时间还是北京时间,它仅仅是将其通过字符串格式,显示出来。
ctime(),返回值是一个字符串格式的时间的描述。它的参数是一个timt_t的指针,它返回的是时间戳对应的当地时间的字符串格式。
mktime(),是localtime()的反操作,将struct tm结构体的指针转换成一个时间戳。


举例说明
例子1:获取当前系统的UTC时间描述

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

int main(int argc, char ** argv)
{
	printf("process begin at \t[%p]\n", (void*)&main);

	time_t currentTime;
	time(¤tTime);
	printf("current time is \t[%d]\n", currentTime);
	
	struct tm *p_stm = gmtime(¤tTime);
	printf("address of struct tm *p_stm is \t[%p]. pointer to object is \t[%p]\n", &p_stm, p_stm);
	printf("struct tm *p_stm is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm->tm_year+1900, p_stm->tm_mon+1, p_stm->tm_mday, p_stm->tm_hour,p_stm->tm_min, p_stm->tm_sec, p_stm->tm_zone);
	
	printf("UTC time is \t[%s]\n", asctime(p_stm));
	
	return 0;
}

程序结果:
process begin at        [0x400594]
current time is         [1498125175]
address of struct tm *p_stm is  [0x7fff2f7aace0]. pointer to object is  [0x390f193420]
struct tm *p_stm is     [2017-06-22 09:52:55] time_zone is [GMT]
UTC time is     [Thu Jun 22 09:52:55 2017
]




例子2:获取当前系统的本地时间描述


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

int main(int argc, char ** argv)
{
	printf("process begin at \t[%p]\n", (void*)&main);

	time_t currentTime;
	time(¤tTime);
	printf("current time is \t[%d]\n", currentTime);
	
	struct tm *p_stm = localtime(¤tTime);
	printf("address of struct tm *p_stm is \t[%p]. pointer to object is \t[%p]\n", &p_stm, p_stm);
	printf("struct tm *p_stm is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm->tm_year+1900, p_stm->tm_mon+1, p_stm->tm_mday, p_stm->tm_hour,p_stm->tm_min, p_stm->tm_sec, p_stm->tm_zone);
	
	printf("local time is \t[%s]\n", asctime(p_stm));
	
	printf("another way: localtime is \t[%s]\n", ctime(¤tTime));
	
	return 0;
}

程序结果:
process begin at        [0x4005e4]
current time is         [1498125330]
address of struct tm *p_stm is  [0x7fff1d135460]. pointer to object is  [0x390f193420]
struct tm *p_stm is     [2017-06-22 17:55:30] time_zone is [CST]
local time is   [Thu Jun 22 17:55:30 2017
]
another way: localtime is       [Thu Jun 22 17:55:30 2017
]


说明:这个例子使用了两种方式来显示当前的本地时间。同时也看到了,asctime()就是仅仅是个显示结构体的功能,而不去会进行时区转换。


例子3:获得2010-01-01 10:08:10秒的时间戳


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

int main(int argc, char ** argv)
{
	printf("process begin at \t[%p]\n", (void*)&main);
	
	struct tm p_stm ;
	p_stm.tm_year = 2010 - 1900;
	p_stm.tm_mon = 1 -1;
	p_stm.tm_mday = 1;
	p_stm.tm_hour = 10;
	p_stm.tm_min = 8;
	p_stm.tm_sec = 10;
	
	printf("struct tm is \t[%s]\n",asctime(&p_stm));
	
	time_t t1 = mktime(&p_stm);
	printf("timestamp of 2010-01-01 10:08:10 is \t[%d]\n",t1);
	
	printf("after mktime, time str is \t[%s]\n",ctime(&t1));
	printf("struct tm p_stm is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm.tm_year+1900, p_stm.tm_mon+1, p_stm.tm_mday, p_stm.tm_hour,p_stm.tm_min, p_stm.tm_sec, p_stm.tm_zone);

	return 0;
}

程序结果:
process begin at        [0x400594]
struct tm is    [??? Jan  1 10:08:10 2010
]
timestamp of 2010-01-01 10:08:10 is     [1262311690]
after mktime, time str is       [Fri Jan  1 10:08:10 2010
]
struct tm p_stm is      [2010-01-01 10:08:10] time_zone is [CST]


说明:
设定struct tm类型的一个变量的年月日和时分秒之后,就可以用它来得到时间了,因为没有设定星期,所以,显示为乱码。经过mktime之后,该结构体中的其他元素也被修改正确,且计算出一个表示当前时间的一个数字。同时,显示struct tm类型的变量,可以得到,其对应的时区为CST。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值