Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1204556
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: C/C++

2014-08-13 14:00:41

searchd.cpp

  1. /// format current timestamp for logging
  2. int sphFormatCurrentTime ( char * sTimeBuf, int iBufLen )
  3. {
  4. #if !USE_WINDOWS
  5.     struct timeval tv;
  6.     gettimeofday ( &tv, NULL );

  7.     struct tm tmp;
  8.     time_t ts = (time_t) tv.tv_sec; // on some systems (eg. FreeBSD 6.2), tv.tv_sec has another type and we can't just pass it
  9.     localtime_r ( &ts, &tmp );
  10. #else
  11.     struct
  12.     {
  13.         time_t    tv_sec;
  14.         DWORD    tv_usec;
  15.     } tv;

  16.     FILETIME ft;
  17.     GetSystemTimeAsFileTime ( &ft );

  18.     uint64_t ts = ( uint64_t(ft.dwHighDateTime)<<32 ) + uint64_t(ft.dwLowDateTime) - 116444736000000000ULL; // Jan 1, 1970 magic
  19.     ts /= 10; // to microseconds
  20.     tv.tv_sec = (DWORD)(ts/1000000);
  21.     tv.tv_usec = (DWORD)(ts%1000000);

  22.     struct tm tmp;
  23.     tmp = *localtime ( &tv.tv_sec );
  24. #endif

  25.     static const char * sWeekday[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  26.     static const char * sMonth[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

  27. /*
  28.     //eg: Wed Aug 13 13:53:05.491 2014
  29.     return snprintf ( sTimeBuf, iBufLen, "%.3s %.3s%3d %.2d:%.2d:%.2d.%.3d %d",
  30.         sWeekday [ tmp.tm_wday ],
  31.         sMonth [ tmp.tm_mon ],
  32.         tmp.tm_mday, tmp.tm_hour,
  33.         tmp.tm_min, tmp.tm_sec, (int)(tv.tv_usec/1000),
  34.         1900+tmp.tm_year );
  35. */
  36.     //eg: 2014-08-13 13:53:18.988
  37.     return snprintf ( sTimeBuf, iBufLen, "%4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d",
  38.         1900+tmp.tm_year,
  39.         1+tmp.tm_mon,
  40.         tmp.tm_mday, tmp.tm_hour,
  41.         tmp.tm_min, tmp.tm_sec, (int)(tv.tv_usec/1000));
  42. }


阅读(1109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~