Differences between revisions 21 and 22
Revision 21 as of 2010-12-15 20:58:44
Size: 2304
Editor: CarlNobile
Comment:
Revision 22 as of 2010-12-23 04:49:47
Size: 2430
Editor: CarlNobile
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
This utility class will convert different date time formats into the proper format to create a Python {{{datetime}}} object. It will use the current locate to determine differences that can not be determined in any other way such as 01/01/2010 meaning either dd/mm/yyyy or mm/dd/yyyy. This utility class will convert different date time formats into the proper format to create a Python {{{datetime}}} object. It will use the current locale to determine differences that can not be determined in any other way such as 05/12/2010 meaning either dd/mm/yyyy or mm/dd/yyyy.
Line 28: Line 28:
The last three are RFC-2616 valid formats that can be used in HTTP headers such as {{{Last-Modified}}} or {{{If-Modified-Since}}}. The datetime object generated can be used to compare for conditional GETs. The datetime objects that are generated for the RFC-2616 formats are timezone naive since all headers are in GMT (UTC) time and can be compared as is. The last three are RFC-2616 valid formats that can be used in HTTP headers such as {{{Last-Modified}}} or {{{If-Modified-Since}}}. The datetime object generated can be used to compare for conditional GETs. The datetime objects that are generated for the RFC-2616 formats are timezone naive since all headers are in GMT (UTC) time and can be compared as is. Conversions to and from different timezones should be done beforehand with a tool like [[http://pytz.sourceforge.net/|pytz]].

Date/Time Conversion

Download DateUtils

This utility class will convert different date time formats into the proper format to create a Python datetime object. It will use the current locale to determine differences that can not be determined in any other way such as 05/12/2010 meaning either dd/mm/yyyy or mm/dd/yyyy.

Methods

  1. DateUtils.toDatetime(value, format=None)

  2. DateUtils.getFormatTypes()

  3. DateUtils.isValidDate(dObj)

  4. DateUtils.isValidTime(tObj)

  5. DateUtils.isValidDateTime(dtObj)

The methods are all classmethods.

A string value is passed into the DateUtils.toDatetime() method. If you know the format of your date or only want to allow a specific format that format can be passed into the format keyword argument.

Implemented Formats

  1. yyyy-mm-dd hh:mm:ss (with or without milliseconds)
  2. mm-dd-yyyy hh:mm:ss (with or without milliseconds)
  3. dd-mm-yyyy hh:mm:ss (with or without milliseconds)
  4. yyyymmddHHMMSS (with or without milliseconds)
  5. wkday, dd month yyyy hh:mm:ss GMT
  6. weekday, dd-month-yy hh:mm:ss GMT
  7. wkday month dd hh:mm:ss yyyy

The last three are RFC-2616 valid formats that can be used in HTTP headers such as Last-Modified or If-Modified-Since. The datetime object generated can be used to compare for conditional GETs. The datetime objects that are generated for the RFC-2616 formats are timezone naive since all headers are in GMT (UTC) time and can be compared as is. Conversions to and from different timezones should be done beforehand with a tool like pytz.

Examples

  • - Date but no time

from dateutils import DateUtils

DateUtils.toDatetime('2010/04/05')
Out[2]: datetime.datetime(2010, 4, 5, 0, 0)
  • - Date and Time

from dateutils import DateUtils

DateUtils.toDatetime('2010/04/05 23:20:59')
Out[3]: datetime.datetime(2010, 4, 5, 23, 20, 59)
  • - Date, Time and the format is specified.

from dateutils import DateUtils

DateUtils.toDatetime('20100405232059', format="yyyymmddHHMMSS")
Out[6]: datetime.datetime(2010, 4, 5, 23, 20, 59)

Exceptions

  1. A ValueError will be raised if the value does not match the format or if the format can not be determined.

  2. A KeyError will be raised if the the date time format is not supported.

datetimeConversion (last edited 2010-12-23 04:49:47 by CarlNobile)