view src/ltpdarepo/utils.py @ 254:b448bb5a6d9f

Fix help text for 'admin grant' command.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Fri, 04 Jan 2013 18:45:06 +0100
parents e6ed4e03074f
children
line wrap: on
line source

# Copyright 2011 Daniele Nicolodi <nicolodi@science.unitn.it>
#
# This software may be used and distributed according to the terms of
# the GNU Affero General Public License version 3 or any later version.


from __future__ import absolute_import
from datetime import *

import dateutil.parser
import dateutil.tz


class datetimetz(datetime):
    # subclass of `datetime.datetime` with default string
    # representation including the timezone name
    def __str__(self):
        return self.strftime('%Y-%m-%d %H:%M:%S %Z')


def parsedatetime(string):
    # parse datetime string representation and returns a timezone
    # aware subclass of datetime with default string representation
    # including the timezone name

    # parsing default is midnight today in UTC timezone
    default = datetime.utcnow().replace(
        tzinfo=dateutil.tz.tzutc(), hour=0, minute=0, second=0, microsecond=0)

    value = dateutil.parser.parse(string, dayfirst=True, yearfirst=True, default=default)
    return datetimetz(value.year, value.month, value.day, value.hour,
                      value.minute, value.second, value.microsecond, value.tzinfo)


def toDATETIME(value):
    if not isinstance(value, datetime):
        return value
    if value.tzinfo is not None:
        value = value.astimezone(dateutil.tz.tzutc())
    return value.strftime('%Y-%m-%d %H:%M:%S')