RssUtils.py :  » Development » SnapLogic » snaplogic » common » Rss » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » common » Rss » RssUtils.py
# $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])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.