예제 #1
0
def parse_datetime(string, rebase=True):
    """Parses a string into a datetime object.  Per default a conversion
  from the blog timezone to UTC is performed but returned as naive
  datetime object (that is tzinfo being None).  If rebasing is disabled
  the string is expected in UTC.

  The return value is **always** a naive datetime object in UTC.  This
  function should be considered of a lenient counterpart of
  `format_system_datetime`.
  """
    from datetime import datetime
    from time import strptime

    from kay.utils import to_utc

    # shortcut: string as None or "now" or the current locale's
    # equivalent returns the current timestamp.
    if string is None or string.lower() in ('now', _('now')):
        return datetime.utcnow().replace(microsecond=0)

    def convert(format):
        """Helper that parses the string and convers the timezone."""
        rv = datetime(*strptime(string, format)[:7])
        if rebase:
            rv = to_utc(rv)
        return rv.replace(microsecond=0)

    # first of all try the following format because this is the format
    # Texpress will output by default for any date time string in the
    # administration panel.
    try:
        return convert(u'%Y-%m-%d %H:%M')
    except ValueError:
        pass

    # no go with time only, and current day
    for fmt in TIME_FORMATS:
        try:
            val = convert(fmt)
        except ValueError:
            continue
        return to_utc(datetime.utcnow().replace(hour=val.hour,
                                                minute=val.minute,
                                                second=val.second,
                                                microsecond=0))

    # no try various types of date + time strings
    def combined():
        for t_fmt in TIME_FORMATS:
            for d_fmt in DATE_FORMATS:
                yield t_fmt + ' ' + d_fmt
                yield d_fmt + ' ' + t_fmt

    for fmt in combined():
        try:
            return convert(fmt)
        except ValueError:
            pass

    raise ValueError('invalid date format')
예제 #2
0
파일: __init__.py 프로젝트: IanLewis/kay
def parse_datetime(string, rebase=True):
  """Parses a string into a datetime object.  Per default a conversion
  from the blog timezone to UTC is performed but returned as naive
  datetime object (that is tzinfo being None).  If rebasing is disabled
  the string is expected in UTC.

  The return value is **always** a naive datetime object in UTC.  This
  function should be considered of a lenient counterpart of
  `format_system_datetime`.
  """
  from datetime import datetime
  from time import strptime

  from kay.utils import to_utc

  # shortcut: string as None or "now" or the current locale's
  # equivalent returns the current timestamp.
  if string is None or string.lower() in ('now', _('now')):
    return datetime.utcnow().replace(microsecond=0)

  def convert(format):
    """Helper that parses the string and convers the timezone."""
    rv = datetime(*strptime(string, format)[:7])
    if rebase:
      rv = to_utc(rv)
    return rv.replace(microsecond=0)

  # first of all try the following format because this is the format
  # Texpress will output by default for any date time string in the
  # administration panel.
  try:
    return convert(u'%Y-%m-%d %H:%M')
  except ValueError:
    pass

  # no go with time only, and current day
  for fmt in TIME_FORMATS:
    try:
      val = convert(fmt)
    except ValueError:
      continue
    return to_utc(datetime.utcnow().replace(hour=val.hour,
                  minute=val.minute, second=val.second, microsecond=0))

  # no try various types of date + time strings
  def combined():
    for t_fmt in TIME_FORMATS:
      for d_fmt in DATE_FORMATS:
        yield t_fmt + ' ' + d_fmt
        yield d_fmt + ' ' + t_fmt

  for fmt in combined():
    try:
      return convert(fmt)
    except ValueError:
      pass

  raise ValueError('invalid date format')
예제 #3
0
파일: __init__.py 프로젝트: IanLewis/kay
 def convert(format):
   """Helper that parses the string and convers the timezone."""
   rv = datetime(*strptime(string, format)[:7])
   if rebase:
     rv = to_utc(rv)
   return rv.replace(microsecond=0)
예제 #4
0
 def convert(format):
     """Helper that parses the string and convers the timezone."""
     rv = datetime(*strptime(string, format)[:7])
     if rebase:
         rv = to_utc(rv)
     return rv.replace(microsecond=0)