Skip to content

Commit

Permalink
fix issue #50, #51
Browse files Browse the repository at this point in the history
  • Loading branch information
wjo1212 committed Jan 25, 2018
1 parent 6e352ca commit f7d18a8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
14 changes: 7 additions & 7 deletions aliyun/log/logclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ def get_log(self, project, logstore, from_time, to_time, topic=None,
:param logstore: logstore name
:type from_time: int/string
:param from_time: the begin timestamp or format of time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param from_time: the begin timestamp or format of time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:type to_time: int/string
:param to_time: the end timestamp or format of time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param to_time: the end timestamp or format of time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:type topic: string
:param topic: topic name of logs, could be None
Expand Down Expand Up @@ -452,10 +452,10 @@ def get_log_all(self, project, logstore, from_time, to_time, topic=None,
:param logstore: logstore name
:type from_time: int/string
:param from_time: the begin timestamp or format of time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param from_time: the begin timestamp or format of time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:type to_time: int/string
:param to_time: the end timestamp or format of time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param to_time: the end timestamp or format of time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:type topic: string
:param topic: topic name of logs, could be None
Expand Down Expand Up @@ -521,7 +521,7 @@ def get_cursor(self, project_name, logstore_name, shard_id, start_time):
:param shard_id: the shard id
:type start_time: string/int
:param start_time: the start time of cursor, e.g 1441093445 or "begin"/"end", or "%Y-%m-%d %H:%M:%S"
:param start_time: the start time of cursor, e.g 1441093445 or "begin"/"end", or readable time like "%Y-%m-%d %H:%M:%S GMT"
:return: GetCursorResponse
Expand Down Expand Up @@ -726,10 +726,10 @@ def pull_log(self, project_name, logstore_name, shard_id, from_time, to_time):
:param shard_id: the shard id
:type from_time: string/int
:param from_time: curosr value, could be begin, timestamp or readable time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param from_time: curosr value, could be begin, timestamp or readable time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:type to_time: string/int
:param to_time: curosr value, could be begin, timestamp or readable time in format "%Y-%m-%d %H:%M:%S" e.g. "2018-01-02 12:12:10"
:param to_time: curosr value, could be begin, timestamp or readable time in readable time like "%Y-%m-%d %H:%M:%S GMT" e.g. "2018-01-02 12:12:10"
:return: PullLogResponse
Expand Down
20 changes: 16 additions & 4 deletions aliyun/log/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import socket

import six
from datetime import datetime
from datetime import datetime, timezone
from dateutil import parser


def base64_encodestring(s):
Expand Down Expand Up @@ -183,13 +184,24 @@ def h_v_td(header, key, default):
return header.get(key, default)


def parse_timestamp(tm, fmt="%Y-%m-%d %H:%M:%S"):
def parse_timestamp(tm, fmts=("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%S %Z")):
if isinstance(tm, (int, float)) or \
(isinstance(tm, (six.text_type, six.binary_type)) and tm.isdigit()):
return int(tm)

dt = None

for fmt in fmts:
try:
dt = datetime.strptime(tm, fmt)
except ValueError as ex:
pass

if dt is None:
dt = parser.parse(tm)

if six.PY2:
return int((datetime.strptime(tm, fmt) - datetime(1970, 1, 1)).total_seconds())
return int((dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds())
else:
return int(datetime.strptime(tm, fmt).timestamp())
return int(dt.timestamp())

3 changes: 2 additions & 1 deletion requirements-py2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ requests
protobuf
six
futures
enum34
enum34
python-dateutil
3 changes: 2 additions & 1 deletion requirements-py3.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
requests
protobuf
six
enum34
enum34
python-dateutil
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import re

if sys.version_info[0] == 2:
install_requires = ['requests', 'protobuf', 'six', 'enum34', 'futures']
install_requires = ['requests', 'protobuf', 'six', 'enum34', 'futures', 'python-dateutil']
elif sys.version_info[0] == 3:
install_requires = ['requests', 'protobuf', 'six', 'enum34']
install_requires = ['requests', 'protobuf', 'six', 'enum34', 'python-dateutil']


packages = [
Expand Down

0 comments on commit f7d18a8

Please sign in to comment.