Add function that converts date string to unix timestamp

This commit is contained in:
Larry Kim 2013-05-21 22:29:52 +09:00
parent 9d6205d11d
commit 6c9f775f3a
2 changed files with 28 additions and 2 deletions

View File

@ -7,5 +7,5 @@
# \/_/ \/_/ \/_/ \/_/ \/_/ # \/_/ \/_/ \/_/ \/_/ \/_/
from whois import Whois as whois from whois import Whois as whois
from parser import Parser from parser import *
from flags import * from flags import *

View File

@ -10,9 +10,35 @@ import error
import re import re
import sys import sys
import os import os
import time
import logging import logging
def convertDate(s):
"""Convert any date string found in WHOIS to a datetime object.
"""
# Source from https://code.google.com/p/pywhois/source/browse/whois/parser.py
known_formats = [
'%d-%b-%Y', # 02-jan-2000
'%Y-%m-%d', # 2000-01-02
'%d.%m.%Y', # 2.1.2000
'%Y.%m.%d', # 2000.01.02
'%Y/%m/%d', # 2000/01/02
'%d-%b-%Y %H:%M:%S %Z', # 24-Jul-2009 13:20:03 UTC
'%a %b %d %H:%M:%S %Z %Y', # Tue Jun 21 23:59:59 GMT 2011
'%Y-%m-%dT%H:%M:%SZ', # 2007-01-26T19:10:31Z
'%Y. %m. %d.', # 2012. 04. 03. - whois.krnic.net
'%d/%m/%Y %H:%M:%S', # 14/09/2013 00:59:59 - whois.nic.im
'%Y/%m/%d %H:%M:%S (%Z)', # 2012/07/01 01:05:01 (JST) - whois.jprs.jp
]
for known_format in known_formats:
try:
return time.mktime(time.strptime(s.strip(), known_format))
except ValueError as e:
pass # Wrong format, keep trying
return s
class Parser(object): class Parser(object):
def __init__(self, domain, text, whoisServer=None, debug=False): def __init__(self, domain, text, whoisServer=None, debug=False):
if debug: if debug: