Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1237682
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: C/C++

2011-12-23 11:10:33

#include
#include
#include
using namespace std;
using namespace log4cplus::helpers;
using namespace log4cplus;
int main (int argc, char *argv[])
{
    Time t = Time::gettimeofday();
    
     log4cplus::tstring str = t.getFormattedTime("%m-%d-%y %H:%M:%S,%q");

     cout << "date : " << str <    return(0);
}

编译: 需要添加 -llog4cplus -lpthread 库
输出:
date : 12-23-11 11:09:49,857

分析Time 实现:
日期格式:
     *  
  • %%a -- Abbreviated weekday name

  •      *  
  • %%A -- Full weekday name

  •      *  
  • %%b -- Abbreviated month name

  •      *  
  • %%B -- Full month name

  •      *  
  • %%c -- Standard date and time string

  •      *  
  • %%d -- Day of month as a decimal(1-31)

  •      *  
  • %%H -- Hour(0-23)

  •      *  
  • %%I -- Hour(1-12)

  •      *  
  • %%j -- Day of year as a decimal(1-366)

  •      *  
  • %%m -- Month as decimal(1-12)

  •      *  
  • %%M -- Minute as decimal(0-59)

  •      *  
  • %%p -- Locale's equivalent of AM or PM

  •      *  
  • %%q -- milliseconds as decimal(0-999) -- Log4CPLUS specific
         *  
  • %%Q -- fractional milliseconds as decimal(0-999.999) -- Log4CPLUS specific
         *  
  • %%S -- Second as decimal(0-59)

  •      *  
  • %%U -- Week of year, Sunday being first day(0-53)

  •      *  
  • %%w -- Weekday as a decimal(0-6, Sunday being 0)

  •      *  
  • %%W -- Week of year, Monday being first day(0-53)

  •      *  
  • %%x -- Standard date string

  •      *  
  • %%X -- Standard time string

  •      *  
  • %%y -- Year in decimal without century(0-99)

  •      *  
  • %%Y -- Year including century as decimal

  •      *  
  • %%Z -- Time zone name

  •      *  
  • %% -- The percent sign

  • 主要函数getFormattedTime

    我们的目的是分析怎样获取毫秒级的系统时间,因为其他的日期时间获取可以通过strftime函数获得。
    getFormattedTime 是通过 %q来获取毫秒
    处理代码
    case LOG4CPLUS_TEXT ('q'):
                {
                    if (! q_str_valid)
                    {
                        build_q_value (q_str);            --调用了这个函数获取ms
                        q_str_valid = true;
                    }
                    ret.append (q_str);
                    state = TEXT;
                }

    void Time::build_q_value (log4cplus::tstring & q_str) const
    {
        q_str = convertIntegerToString(tv_usec / 1000);
        size_t const len = q_str.length();
        if (len <= 2)
            q_str.insert (0, padding_zeros[q_str.length()]);
    }

    可以看出 log4cplus是通过gettimeofday函数获得的微妙除以1000然后转换为string来获得系统的ms时间。
    阅读(7017) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~