1、nginx的日志格式是:
log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” $http_x_forwarded_for’;
2、在python中转换
这里主要使用了time模块中的strptime方法和mktime方法: 其中strptime的用法如下:
strptime(string, format) -> struct_time Parse a string to a time tuple according to a format specification. See the library reference manual for formatting codes (same as strftime()).
Example:
In [16]: time.strptime("30/Oct/2013:19:28:21","%d/%b/%Y:%H:%M:%S") Out[16]: time.struct_time(tm_year=2013, tm_mon=10, tm_mday=30, tm_hour=19, tm_min=28, tm_sec=21, tm_wday=2, tm_yday=303, tm_isdst=-1)
而我们常见的strftime:
strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.
接下来说下mktime:
mktime(tuple) -> floating point number Convert a time tuple in local time to seconds since the Epoch
EXAMPLE:
t = time.strptime("30/Oct/2013:19:28:21","%d/%b/%Y:%H:%M:%S") In [19]: time.mktime(t) Out[19]: 1383132501.0
完整的例子:
#!/usr/bin/python import time log = open('access.log').readlines() num = 0 now = time.time() for line in log: if int(now) - int( time.mktime(time.strptime(line.split()[3].replace('[',''),"%d/%b/%Y:%H:%M:%S"))) <= 3600 : num += 1 print num
2、php 中主要是利用strtotime函数
3、perl中主要是利用Date::Parse
<code>perl -e 'use Date::Parse ; print str2time("24/Apr/2011:10:37:06") . "\n";'</code>
源地址:http://www.chenqing.org/2013/10/nginx-log-time-transform-to-unix-timestmp-use-3p.html
转载于:https://blog.51cto.com/noican/1761465