# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2008, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
#
# $
# $Id: RssUtils.py 1438 2008-03-10 19:02:16Z dhiraj $
"""
Utility module for RSS related functions.
"""
# Imports
import re, time, datetime
from snaplogic.common.snap_exceptions import *
# Constants
minDate = '1970-01-01'
maxDate = '2037-12-31'
datetimeFormat1 = '%a, %d %b %Y %H:%M:%S'
datetimeFormat2 = '%Y-%m-%dT%H:%M:%S'
datetimeFormats = {
'rss20': datetimeFormat1,
'atom10': datetimeFormat2,
}
def timezoneStr():
"""
Get the timezone string to be appended to a date-time string.
For example, PST would be '-08:00' and PDT would be '-07:00'.
@return: The timezone string
@rtype: string
"""
if time.daylight:
tz = time.altzone
else:
tz = time.timezone
hr = tz / 60 / 60
if hr < 0:
stz = '+'
hr = -hr
else:
stz = '-'
if hr > 9:
stz += str(hr) + ':00'
else:
stz += '0' + str(hr) + ':00'
return stz
def timeTupleStrToInt(ts):
"""
Convert string format of time tuples (9) to tuple.
The string format is used for transported in a record field. The expected format looks like:
'(2007, 4, 21, 17, 42, 58, 5, 111, 0)', for example.
@param ts: The string fromat of time tuples.
@type ts: string
@return: A tuple of the result.
@rtype: tuple of 9
"""
newts = ts.replace('(', '').replace(')', '').replace(',', '')
tslist = newts.split(' ')
tilist = [ int(t) for t in tslist ]
return tuple(tilist)
def timeStrToTuple(timestr):
"""
Convert the time string to tuples
Tentatively define these supported protocol:
- rss20: 'Mon, 06 Jul 2007 12:00:00 PST'
- atom10: '2007-07-06T12:00:00' or '2007-07-06 12:00:00' or with trailing timezone string
Note that there is no timezone support - Python time package does not support timezone well.
@param timestr: The time in string format, e.g. '2007-01-01T12:00:00' or 'Sat, 11 Jan 2007 16:47:28'
@type timestr: string
@return: The time in tuple (of 9 integers).
@rtype: tuple of 9
"""
# Simply to determine the time format
if timestr[0:3] in [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]:
# Hack - get rid of EDT, EST, CDT, CST, PDT, PST because %Z doesn't work
ts = re.sub(' EST| EDT| CST| CDT| PST| PDT| GMT', '', timestr)
tfromat = datetimeFormat1
else:
# Not including the trailing timezone stuff
ts = timestr[0:len('YYYY-mm-ddTHH:MM:SS')]
ts = re.sub(' ', 'T', ts)
tfromat = datetimeFormat2
# Not catching any exception, let the caller handle it ...
return time.strptime(ts, tfromat)
def timeTupleToString(tup, proto = 'atom10'):
"""
Convert a tuple of 9 time numbers to a string. The format is different for different protocol (atom, rss).
@param tup: The tuple.
@type tup: tuple
@param proto: The protocol for the output format - atom10 or rss20.
@type proto: string
@return: The string format of time.
@rtype: string
"""
if proto == 'rss20':
return time.strftime(datetimeFormat1, tup)
elif proto == 'atom10':
return time.strftime(datetimeFormat2, tup)
return ''
def timeDateTimeToString(dt, proto = 'atom10'):
"""
Convert a datetime object to string format.
@param dt: The datetime object.
@type dt: datetime.datetime
@param proto: The protocol for the output format - atom10 or rss20.
@type proto: string
@return: The string format of datetime.
@rtype: string
"""
if dt is None:
return ''
return dt.strftime(datetimeFormats[proto])
|