C++时间戳转换成日期时间的步骤和示例代码

因工作需要,经常跟时间戳打交道,但是因为它仅仅是一个数字,我们很难直接看出它有什么意义,或两个时间戳之间究竟差了多长的间隔。于是从MSDN for Visual Studio6上找到了时间戳转换成日期时间的算法。本文除介绍这一算法外,还提供一个示例代码。

1、将时间戳转换成一串32比特的二进制数。有些数字转换之后不够32位,则在前面补充0。这可通过windows自带的计算器完成。比如481522543转换成
0001 1100 1011 0011 0111 0011 0110 1111

2、根据下面格式转换各个字段为10进制数字
YYYY YYYM MMMD DDDD HHHH HMMM MMMS SSSS
0001 1100 1011 0011 0111 0011 0110 1111

Y = year = 0000 1110 = 14
M = month = 0000 0101 = 5
D = day = 0001 0011 = 19
H = hour = 0000 1110 = 14
M = minutes = 0001 1011 = 27
s = seconds = 0000 1111 = 15

注意最右边一位在从日期转换到时间戳的时候砍掉了,因此我们秒这一字段要在最右端加一个补充的0。本例中为 s = seconds = 0 0001 1110 = 30。也因为此,转换后日期时间的“秒”字段总是一个偶数:-)

3、特殊处理:

年这一字段从1980开始计算,因此要加上1980才是正确年份。
这样481522543最终转换成:1994/05/19 14: 27: 30

示例程序如下

 /* File name: ts2tm.c
 Converts an decimal timestamp to human-readable format
 by sillyboard(sillyboard@tom.com)
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct date_time
{
 short dt_year;
 short dt_month;
 short dt_day;
 short dt_hour;
 short dt_minute;
 short dt_second;
}dt;

short bits_per_field[6] = {7, 4, 5, 5, 6, 5};

int main(int argc, char** argv)
{
 long timestamp;
 int i, j;
 unsigned long mask = 0x80000000;
 short bit;
 int accum;
 short* walker;
 if (argc != 2)
 {
  fprintf(stderr, "Usage: %s decimal-timestamp/n", argv[0]);
  exit(1);
 }

 timestamp = atol(argv[1]);
 walker = &dt;
 for (i = 0; i < 6; i ++)
 {
  accum = 0;
  for (j = 0; j < bits_per_field[i]; j ++)
  {
   bit = (timestamp & mask) ? 1 : 0;
   if (bit)
   {
    accum += pow(2, bits_per_field[i] - 1 - j);
   }
   mask = mask >> 1;
  }
  *walker ++ = accum;
 }

 dt.dt_second <<= 1;

 printf("%s/t", argv[1]);
 printf("%d-%d-%d %d:%d:%d/n", dt.dt_year + 1980, dt.dt_month,
  dt.dt_day, dt.dt_hour, dt.dt_minute, dt.dt_second);
 return;
}

这篇文章暂时就介绍这么多,具体的大家可以参考我们以前的文章。

(0)

相关推荐

  • C++自定义函数判断某年某月某日是这一年中第几天

    本文实例讲述了C++自定义函数判断某年某月某日是这一年中第几天的方法.分享给大家供大家参考,具体如下: /* * 作 者: 刘同宾 * 完成日期:2012 年 11 月 30 日 * 版 本 号:v1.0 * * 输入描述: * 问题描述:编写函数判断某年某月某日这一年中是第几天,在主函数中调用该函数. * 程序输出: * 问题分析:略 * 算法设计:略 */ #include<iostream> using namespace std; int main() { void f(int yea

  • VC++ 获取系统时间的方法汇总

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm.Format("现在时间是%Y年%m月%d日 %X"); MessageBox(str,NULL,MB_OK); a,从CTimet中提取年月日时分秒 CTime t = CTime::GetCurrentTime(); int d=t.GetDay(); //获得几号 int y=t.

  • C++取得当前时间的方法

    本文实例讲述了C++取得当前时间的方法,分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: //取本地时间  BOOL GetTime(string &mytime)  {      BOOL b_ret = TRUE;      CHAR szBuf1[256]={0};      CTime   tNow   =   CTime::GetCurrentTime();       sprintf(szBuf1,"%04u%02u%02u%02u%02u%02u"

  • C++基于蔡基姆拉尔森计算公式实现由年月日确定周几的方法示例

    本文实例讲述了C++基于蔡基姆拉尔森计算公式实现由年月日确定周几的方法.分享给大家供大家参考,具体如下: #include <iostream> #include <string> using namespace std; int whatday(int y, int m, int d) { // 返回正确的星期.用 0 - 6 表示 星期 1 - 7 if(m==1||m==2) { y--; m+=12; } return(d+2*m+3*(m+1)/5+y+y/4-y/100

  • C++实现当前时间动态显示的方法

    本文实例讲述了C++实现当前时间动态显示的方法.分享给大家供大家参考.具体如下: /* 24-06-10 10:44 动态显示时间 但不是最优的 功能很单一 本程序关键是对时钟函数的使用 **tm结构定义了 年.月.日.时.分.秒.星期.当年中的某一天.夏令时 **用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间,函数原型: struct tm * localtime(const time_t *) **使用gmtime函数获取格林尼治时间,函数原型:

  • C++实现两个日期间差多少天的解决方法

    计算原理是先求出每个日期距离1年1月1日的天数差值,再进一步做差即可. 复制代码 代码如下: #include <stdio.h>struct MyDate{ int year; int month; int day;}; int GetAbsDays(MyDate x){ int i; int month_day[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int year = x.year-1;  // 因为欲求距离1年1月1日的距离 int da

  • C++获取当前系统时间的方法总结

    本文实例讲述了C++获取当前系统时间的方法.分享给大家供大家参考.具体如下: 方案- 优点:仅使用C标准库:缺点:只能精确到秒级 #include <time.h> #include <stdio.h> int main( void ) { time_t t = time(0); char tmp[64]; strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t)); puts(

  • C++ boost 时间与日期处理详细介绍

    boost 时间与日期处理 导视: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确,可派生个类,调用stream的precision设置 progress_display 图形化显示进度 只能输出到cout 如果还有其他输出则会干扰进度显示. 折中的办法是重新显示 pd.restart(size); pd+= pNum; date 日期结构,时间点 -- da

  • C++设置系统时间及系统时间网络更新的方法

    本文实例讲述了C++设置系统时间及系统时间网络更新的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: //根据返回的时间设置系统时间 void setTimeFromTP(ULONG ulTime) {      FILETIME ft;      SYSTEMTIME st;        //将基准时间转换成windows文件时间      st.wYear = 1900;      st.wMonth = 1;      st.wDay = 1;      st.wHo

  • C++实现日期类(Date类)的方法

    如下所示: #include<iostream> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) //构造 :_year(year) , _month(month) , _day(day) { if (!isInvalidDate(_year, _month, _day)) { _year = 1900; _month = 1; _day = 1; } } D

  • C++中获取UTC时间精确到微秒的实现代码

    在日常开发过程中经常会使用到时间类函数的统计,其中获取1970年至今的UTC时间是比较常使用的,但是在windows下没有直接能够精确到微妙级的函数可用.本文提供方法正好可以解决这类需求问题. 下面先给出C++实现代码: 复制代码 代码如下: #ifndef UTC_TIME_STAMP_H_#define UTC_TIME_STAMP_H_ #include <windows.h>#include <sys/timeb.h>#include <time.h> #if

随机推荐