Skip to main content

Methods for formatting and parsing friendly timestamps

Project description

Here are some methods for formatting and parsing friendly timestamps.

Installation

easy_install -U whenIO

Usage

>>> import whenIO
>>> import datetime
>>> w = whenIO.WhenIO(timezone='US/Eastern')

>>> w.format(datetime.datetime.now(), fromUTC=False)
'Today 2:30pm'
>>> w.format(datetime.datetime.utcnow())
'Today 2:30pm'
>>> w.format(datetime.datetime.utcnow(), forceDate=True)
'Today 3/10/2013 2:30pm'
>>> date1 = datetime.datetime(2000, 1, 1, 0, 0)
>>> date2 = datetime.datetime(2000, 1, 1, 1, 0)
>>> w.format([date1, date2], fromUTC=False)
'1/1/2000 12am 1am'

>>> w.parse('10am', toUTC=False)[0]
[datetime.datetime(2013, 3, 10, 10, 0)]
>>> w.parse('today 10am', toUTC=False)[0]
[datetime.datetime(2013, 3, 10, 10, 0)]
>>> w.parse('tom 8pm', toUTC=False)[0]
[datetime.datetime(2013, 3, 11, 20, 0)]
>>> w.parse('mon 10am 12pm', toUTC=False)[0]
[datetime.datetime(2013, 3, 11, 10, 0),
 datetime.datetime(2013, 3, 11, 12, 0)]

>>> rdelta = whenIO.parse_duration('111 weeks')
>>> whenIO.format_duration(rdelta)
'2 years 3 months 3 weeks'
>>> whenIO.format_duration(rdelta, precision=2, style='abbreviations')
'2 yrs 4 mos'

1.5

  • Added unit abbreviations and letters to parse_duration(), format_duration()

  • Added control over rounding behavior to format_duration()

  • Added microseconds to format_duration() thanks to Javier Ruere

1.4

  • Added forceDate to format(), format_date()

  • Replaced withStartDate to omitStartDate

1.3

  • Changed constructor to use timezones from pytz and tzlocal

  • Added weeks to format_duration()

  • Added withLeadingZero to format(), format_date(), format_time()

  • Added withStartDate to format()

  • Removed parse_offset(), format_offset()

1.2

  • Removed python-dateutil==1.5 requirement

  • Restored test coverage to 100%

1.1

  • Added format_duration() for formatting relativedelta objects

  • Added parse_duration() for parsing text into relativedelta objects

1.0

  • Expanded test coverage to 100%

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page