获取时间戳与当地时间
来源:互联网 发布:java实现视频播放 编辑:程序博客网 时间:2024/06/06 15:47
gettimeofday和time(NULL)返回的都是绝对时间戳,时间戳全世界统一;可读的年月日时间则不一样。
在全世界,执行date +%s的结果都一样,但是执行date +%Y%m%d-%H%M%S的结果则不同
[fall@localhost zookeeper-3.4.6]$ cat test.cc
#include <iostream>
#include <time.h>
#include <sys/time.h>
using namespace std;
unsigned int get_local_secs_1()
{
struct timeval tv1;
gettimeofday(&tv1, NULL);
time_t t1;
t1 = tv1.tv_sec;
struct tm *pTime;
pTime = localtime( &t1 );
return
pTime->tm_hour * 3600 + pTime->tm_min * 60 + pTime->tm_sec;
}
unsigned int get_local_secs_2()
{
time_t t2;
time(&t2); //
或者 t2 = time(NULL);
struct tm *pTime;
pTime = localtime( &t2 );
return
pTime->tm_hour * 3600 + pTime->tm_min * 60 + pTime->tm_sec;
}
int main()
{
std::cout<<
"今日已经过了几秒:"
<<get_local_secs_1()<<std::endl;
std::cout<<
"今日已经过了几秒:"
<<get_local_secs_2()<<std::endl;
return
0;
}
PS:
gmtime与localtime一样,但使用的还是UTC时间,不是当地时间。
数据结构:
#ifndef _TM_DEFINED
struct tm {
int tm_sec;
/* 秒 – 取值区间为[0,59] */
int tm_min;
/* 分 - 取值区间为[0,59] */
int tm_hour;
/* 时 - 取值区间为[0,23] */
int tm_mday;
/* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon;
/* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year;
/* 年份,其值等于实际年份减去1900 */
int tm_wday;
/* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday;
/* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst;
/* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
#define _TM_DEFINED
#endif
#ifndef _TIME_T_DEFINED
typedef long time_t;
/* 时间值 */
#define _TIME_T_DEFINED /* 避免重复定义 time_t */
#endif
0 0
- 获取时间戳与当地时间
- UTC时间与当地时间
- UTC时间与当地时间
- UTC时间与当地时间
- 如何利用C语言来获取当地时间和UTC时间? (设当地时间是北京时间)
- DateTime获取当地时间
- Python获取当地时间---转
- php获取当地时间
- 简单的获取当地时间
- UTC时间和当地时间转换
- js获取当地时间并且拼接时间格式的三种方式
- 用Jquery动态获取当地时间
- GregorianCalendar 当地时间以及日期时间函数
- 根据地区和时区获取当地时间
- NSDate获取指定格式的当地时间
- js获取当地时间做当地指针始终
- 将时间戳转换成当地时间NSDate 转换成当地yyyy-MM-dd HH:mm:ss时间格式
- 从GMT时间转换到当地时间(北京时间)
- 数据结构:直接插入排序
- 动态引入js/css与异步载入
- [leetcode][two pointers] Container With Most Water
- request.getParameter中文乱码问题
- 1_Linux 简介和系统的安装
- 获取时间戳与当地时间
- 如何看股票均线
- ROS Service vs Topic
- 通俗易懂的动态规划
- Python字符串方法详细介绍3_变形
- 黑马程序员——先试试博客的要求
- spring4.1.2使用quartz2.2.1定时器
- C# WinForm基础二 控件、对话框类、容器、进程、线程
- 使用JAVA读写Properties属性文件