如何解析系统日志时间戳 [英] how to parse syslog timestamp

查看:583
本文介绍了如何解析系统日志时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.syslog.cc/ IETF /汇票/草案,IETF-系统日志协议-23.txt

6.2.3.1。例子的在上面的链接提供不同的时间戳记甲的例子。

6.2.3.1. Examples in the above link provides examples of different timestamp formates.

我怎么能在ç解析这些时间戳

在即时,任何类型的消息可以到达,我希望能够解析它。

On-the-fly, any type of message can arrive and I want to be able to parse it.

推荐答案

日期格式为RFC3339给人一种字符串限制版,如2011-08-18T23:31:42Z

The date format is a stricter version of RFC3339 giving a string such as '2011-08-18T23:31:42Z'

我不能肯定的strptime功能,可处理时区指示符(Z在上面的时间字符串),所以它可能是更容易处理你自己的函数中。它绝对不能处理秒的小数部分,由于结构TM不处理他们。你可以使用结构的timespec存储秒的小数部分如果需要的话。

I'm not certain the strptime function can deal with the timezone specifier (Z in the time string above), so it may be easier to handle that inside your own function. It definitely can't handle fractional seconds, since struct tm doesn't handle them. You could use struct timespec to store the fractional seconds if required.

您可以用strptime解析出大部分的格式为:

You can parse out most of the format using strptime:

struct tm tm;
time_t t
char *extra;
extra = strptime( tmstr, "%C%y-%m-%dT%H:%M:%S", &tm );
tm.tm_isdst = -1;
t = mktime( &tm );

在此之后,额外将输入tmstr的剩余部分。这可能包括小数秒,然后将包含时区的格式。如果额外的以'。只是解析数出与的strtod 功能:

if( extra && extra[0] == '.' )
{
  char *endptr;
  fraction = strtod( extra, &endptr );
  extra = endptr;

  /* use timespec if fractional seconds required */
  struct timespec ts;
  ts.tv_sec = t;
  ts.tv_nsec = fraction * 1000000000;
}

然后额外现在将只包含时区说明。如果是'Z',那么我们就大功告成了,因为反正mktime给您UTC时间。否则,你将有一个例如偏移+03:00,所以你需要通过小时数/分钟修改时间

Then extra will now just contain the timezone specifier. If it is 'Z' then we're done since mktime gives you UTC time anyway. Otherwise you'll have an offset e.g. +03:00, so you will need to modify your time by that number of hours/minutes.

这篇关于如何解析系统日志时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆