MFC如何获取时间戳 例如输出:1392904431274

zj4819 2014-02-20 09:54:49
请教 各位高人 MFC 如何获取1392904431274 为cstring

写个完整的例子 越简单越好 本人环境 VC6.0 MFC

谢谢了!!!!!
...全文
2507 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wulinbang 2016-03-22
  • 打赏
  • 举报
回复
很奇怪,在vs2012上运行这段代码是可以生成时间戳的,但是生成的时间戳与cString组合,为空,比如CString str="ss"+时间戳,结果还是ss,很奇怪,百思不得其解,但是用char转换后就可以显示,而且用这段代码后,程序就出现很多错误,把他注销就没有问题,奇怪啊
快乐鹦鹉 2014-02-21
  • 打赏
  • 举报
回复
8楼的行 SYSTEMTIME tmSys; GetLocalTime(&tmSys); CTime tm3(tmSys); __int64 tmDst = __int64(tm3.GetTime())*1000 + tmSys.wMilliseconds; CString sMS; _i64toa(tmDst,sMS.GetBuffer(100),10); sMS.ReleaseBuffer();
zj4819 2014-02-21
  • 打赏
  • 举报
回复
引用 8 楼 zgl7903 的回复:

  __int64 tmSrc = 1392904431274i64;
  time_t tm = (time_t)(tmSrc/1000);
  int msSecod = (int)(tmSrc%1000);
  CTime tm2(tm);
  TRACE(_T("%I64d = %s.%03d\n"), 
    tmSrc, tm2.Format(_T("%Y-%m-%d %H:%M:%S")), msSecod);
  //out 1392904431274 = 2014-02-20 21:53:51.274

  SYSTEMTIME tmSys;
  GetLocalTime(&tmSys);
  CTime tm3(tmSys);
  __int64 tmDst = tm3.GetTime()*1000i64 + tmSys.wMilliseconds;
  TRACE(_T("%s.%03d = %I64d\n"),
    tm3.Format(_T("%Y-%m-%d %H:%M:%S")), tmSys.wMilliseconds, tmDst);
  //out 2014-02-21 11:56:15.015 = 1392954975015
老哥 帮人帮到家。两端代码 帮做两个 cstring 输出一下呗?
zj4819 2014-02-21
  • 打赏
  • 举报
回复
引用 6 楼 dahaiI0 的回复:

CTime tm = CTime::GetCurrentTime();
CString str;
str.Format(_T("%u"),tm.GetTime() );
秒数 毫秒没有啊
zgl7903 2014-02-21
  • 打赏
  • 举报
回复

  __int64 tmSrc = 1392904431274i64;
  time_t tm = (time_t)(tmSrc/1000);
  int msSecod = (int)(tmSrc%1000);
  CTime tm2(tm);
  TRACE(_T("%I64d = %s.%03d\n"), 
    tmSrc, tm2.Format(_T("%Y-%m-%d %H:%M:%S")), msSecod);
  //out 1392904431274 = 2014-02-20 21:53:51.274

  SYSTEMTIME tmSys;
  GetLocalTime(&tmSys);
  CTime tm3(tmSys);
  __int64 tmDst = tm3.GetTime()*1000i64 + tmSys.wMilliseconds;
  TRACE(_T("%s.%03d = %I64d\n"),
    tm3.Format(_T("%Y-%m-%d %H:%M:%S")), tmSys.wMilliseconds, tmDst);
  //out 2014-02-21 11:56:15.015 = 1392954975015
搬砖的奶霸 2014-02-21
  • 打赏
  • 举报
回复
CTime time = CTime::GetCurrentTime(); DWORD dwTime= time.GetTime(); CString strTime; strTime.Format(_T("%d"),dwTime);
dahaiI0 2014-02-21
  • 打赏
  • 举报
回复 1

CTime tm = CTime::GetCurrentTime();
CString str;
str.Format(_T("%u"),tm.GetTime() );
「已注销」 2014-02-21
  • 打赏
  • 举报
回复
根据其公式可以写出来一个函数:
ULONGLONG GetUnixTimeStamp(void)
{
/*
//  因为这值算出来是定死的,因此直接写死在程序里。
    FILETIME ft1 = { 0 };
    SYSTEMTIME st1 = { 1970, 1, 0, 1, 0, 0, 0, 0 };
    ULARGE_INTEGER ull1 = { 0 };
    ::SystemTimeToFileTime(&st1, &ft1);
    ull1.LowPart = ft1.dwLowDateTime;
    ull1.HighPart = ft1.dwHighDateTime;
//  ull1.QuadPart == 116444736000000000ULL
*/
    FILETIME ft = { 0 };
    SYSTEMTIME st = { 0 };
    ULARGE_INTEGER ull = { 0 };
    ::GetSystemTime(&st);
    ::SystemTimeToFileTime(&st, &ft);
    ull.LowPart = ft.dwLowDateTime;
    ull.HighPart = ft.dwHighDateTime;
//  return (ull.QuadPart - ull1.QuadPart) / 10000000ULL;
    return (ull.QuadPart - 116444736000000000ULL) / 10000000ULL;
}
许文君 2014-02-21
  • 打赏
  • 举报
回复

TCHAR buff[512]={0};
SYSTEMTIME st;GetLocalTime(&st);
_stprintf(msgCnt,TEXT("%04d%02d%02d%02d%02d%02d"),st.wYear,st.wMinute,st.wDay,st.wHour,st.wMinute,st.wSecond);
输出201402210923
我系麦叔叔 2014-02-21
  • 打赏
  • 举报
回复
网上抄了一个下来!
typedef unsigned long  uint32;
typedef unsigned char uchar;
typedef unsigned short int16u;  /*无符号16位整型变量*/
typedef short int uint16;


typedef struct
{
  int16u UnixYear;
  uchar  UnixMonth;
  uchar  UnixDay;
  uchar  UnixHour;
  uchar  UnixMinute;
  uchar  UnixSecond;
  uint32 Unixtime ;
}TIME_UNIX;


/********************************************************************************************************
* 函数名称: void Unixtime_To_Localtime(uint32 unixtimetemp)
* 功能描述: unixtime转地方时间
*********************************************************************************************************/
void Unixtime_To_Localtime(uint32 unixtimetemp,TIME_UNIX * Localtime)
{
  static uint16  month[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};      /*注意溢出*/
  uchar  i;
  uint16 x,extraday=0;
  uint16  unixtimeyear,unixtimeday;
  
  unixtimetemp = unixtimetemp + 28800;    /*加上北京时间*/
  unixtimeyear = unixtimetemp/31536000;
  Localtime->UnixYear = 1970+unixtimeyear;
  for( x=1970;x<Localtime->UnixYear;x++)               /*加上闰年二月多的一天*/
  {
    if(((x % 4) == 0 && ((x % 100) != 0 || (x % 400) == 0)))
    {
      extraday++;
    }
  }
  if((((Localtime->UnixYear) % 4) == 0 && (((Localtime->UnixYear) % 100) != 0 || ((Localtime->UnixYear) % 400) == 0)))
  {
    for(i=2;i<12;i++)
    {
      month[i] = month[i] + 1;
    }
  }
  
  unixtimeday = unixtimetemp%31536000/86400 - extraday;
  for(i=0;i<12;i++)
  {
    if((unixtimeday>=month[i])&&(unixtimeday<month[i+1]))
    {
      Localtime->UnixMonth = i+1;
      Localtime->UnixDay   = unixtimeday - month[i]+1;
    }
  }
  
  Localtime->UnixHour = unixtimetemp%86400/3600;
  Localtime->UnixMinute = unixtimetemp%3600/60;
  Localtime->UnixSecond = unixtimetemp%60;
}

/********************************************************************************************************
* 函数名称: uint32 Localtime_To_Unixtime(uint16 year,uchar month,uchar day,uchar hour,uchar minute,uchar second)
* 功能描述: 地方时间转unixtime
*********************************************************************************************************/
uint32 Localtime_To_Unixtime(uint16 year,uint16 month,uint16 day,uint16 hour,uint16 minute,uint16 second)
{
  static uint16 monthtable[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
  uint32  unixtimetemp,unixtimeday,unixtimehour;
  uchar i;
  uint16 x,extraday=0;
  uint16  unixtimeyear;
  uchar  unixtimemonth;
  
  for(x=1970;x<year;x++)               /*加上闰年二月多的一天*/
  {
    if(((x % 4) == 0 && ((x % 100) != 0 || (x % 400) == 0)))
    {
      extraday++;
    }
  }
  
  unixtimeyear = year - 1970;
  if((((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0)))
  {
    for(i=2;i<12;i++)
    {
      monthtable[i] = monthtable[i] + 1;
    }
  }
  unixtimemonth = month;
  unixtimehour  = hour;
  unixtimeday = unixtimeyear*365 + monthtable[unixtimemonth-1] + day-1 + extraday;
  /*修改UnixHour-8可能导致变成负数的问题,原来为“(UnixHour-8)*3600”*/
  /*ZG20080126 修改UnixHour*3600可能导致数据溢出的问题,现在改为(unixtimehour)*3600*/
  unixtimetemp = unixtimeday*86400 + (unixtimehour)*3600-28800+minute*60 + second;
  return(unixtimetemp);
}
快乐鹦鹉 2014-02-21
  • 打赏
  • 举报
回复
这是时间的毫秒值吧?把SYSTEMTIME的各项值乘一下就有了,用INT64,再_atoi64就行了吧
zj4819 2014-02-21
  • 打赏
  • 举报
回复
引用 11 楼 happyparrot 的回复:
8楼的行 SYSTEMTIME tmSys; GetLocalTime(&tmSys); CTime tm3(tmSys); __int64 tmDst = __int64(tm3.GetTime())*1000 + tmSys.wMilliseconds; CString sMS; _i64toa(tmDst,sMS.GetBuffer(100),10); sMS.ReleaseBuffer();
大版主 第一段我理解是 时间戳转换时间格式 你能帮我也写一个输出吗 就是8楼的
oyljerry 2014-02-20
  • 打赏
  • 举报
回复
GetLocalTime得到SYSTEMTIME,然后转换成FILETIME,再转车INT64

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧