perl mysql 时间戳_nginx log 时间转换时间戳在php,perl,python中的使用

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::Parseperl  -e 'use Date::Parse ; print str2time("24/Apr/2011:10:37:06") . "\n";'

源地址:http://www.chenqing.org/2013/10/nginx-log-time-transform-to-unix-timestmp-use-3p.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值