mktime和localtime_r时间转换问题

2012-08-26 wcdj


由UNIX时间戳转换为系统时间

date -d'1970-01-01 UTC 2147483647 seconds' +"%Y-%m-%d %T %z"           
2038-01-19 11:14:07 +0800
date -d'1970-01-01 UTC 2147483648 seconds' +"%Y-%m-%d %T %z"
date: invalid date "1970-01-01 UTC 2147483648 seconds"
date -d'1970-01-01 UTC 0 seconds' +"%Y-%m-%d %T"          
1970-01-01 08:00:00 +0800 

显示当前的UNIX时间戳

date +%s

Unix时间与系统时间之间的转换
http://shijianchuo.911cha.com/

/* 
 * 2012-08-26 wcdj
 * 使用Gauss算法解决UNIX时间问题
 * 将UNIX时间2038-01-19 03:14:08
 * 支持扩大到2106-02-07 06:28:16
 */
#include <stdio.h>
#include <string.h>
#include <time.h>		// struct tm, mktime
#include <sys/time.h>	// gettimeofday

#define MAX_TIME_DIFF   (24*60*60)
#define SECS_PER_HOUR   (60 * 60)
#define SECS_PER_DAY    (SECS_PER_HOUR * 24)


// calc microseconds
long long get_time_of_day()
{
	struct timeval tv;
	if (gettimeofday(&tv, NULL) == 0) 
	{
		return tv.tv_sec * 1000000ULL + tv.tv_usec;
	}
	else 
	{
		return 0;
	}
}


static long _timezone = MAX_TIME_DIFF+1;	 

/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 *
 * [For the Julian calendar (which wa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值