[PYTHON DB-SIG] [comp.lang.python] Date-Time requirements (esp. for databases)

Jim Fulton jim.fulton@digicool.com
Tue, 29 Oct 1996 17:39:01 -0500


Harri Pasanen wrote:
> 
> Jim Fulton writes:
> > Andy Eskilsson wrote:
> > >
> > > / Jim Fulton <jim@digicool.com> wrote:
> > > |
> > > |  1. Support conversion from strings in a very wide variety of formats
> > > |     (e.g. 'Oct, 1, 1994 12:34am EST', '1994/10/1 00:34:21.456')
> > >
> > > Thats what I have created my strptime function to do, you can find it
> > > at: http://www.fukt.hk-r.se/~flognat/hacks/, the inverse of strftime.
> >
> > This looks good, but I want something that doesn't require a format
> > string.
> 
> What are you thinking of?  How do you plan to achieve genericity
> without some kind of template, or format string?

Yes.  I've done this before.  It's not practical unless it is 
done in C.

In this case, I'd be inclined to provide handling of a very 
wide variety of formats without requiring format strings, 
but allow the specification of format strings as well.

> Or are you planning to hide the possible format strings/templates
> inside the a Date object factory, which returns a Date object when
> receiving the string representation?

I want a constructor something like this:

  DateTime(*args) -- Return a new date-time object
  
  This function may be invoked with from zero to six arguments.
  
  If the function is called with no arguments, then the current date and
  time is returned.
  
  If the function is invoked with a single string argument, then the
  string is parsed as a date-time value. If the string contains a valid
  date-time value, then the corresponding date-time object is returned,
  otherwise a DateTime.error is raised. As a general rule, any date-time
  representation that is recognized and unambigous to a human is
  acceptable.  A date-time string consists of two components, a date
  component and an optional time component, separated by one or more
  spaces. If the time component is ommited, 12:00am is assumed.
  
  The date component consists of year, month, and day values. The year
  value must be a two- or four-digit integer. If a two-digit year is
  used, the year is assumed to be in the twentieth century. The month
  may an integer, from 1 to 12, a month name, or a month abreviation,
  where a period may optionally follow the abreviation. The day must be
  an integer from 1 to the number of days in the month. The year, month,
  and day values may be separated by periods, hyphens, forward, shashes,
  underscores, or spaces. Extra spaces are permitted around the
  delimiters. Year, month, and day values may be given in any order as
  long as it is possible to distinguish the components. If all three
  components are numbers that are less than 13, then a a month-day-year
  ordering is assumed.
  
  The time component consists of hour, minute, and second values
  separated by colons. The hour value must be an integer between 0 and
  23 inclusively. The minute value must be an integer between 0 and 59
  inclusively. The second value may be an integer value between 0 and
  59.999 inclusively. The second value or both the minute and second
  values may be ommitted. The time may be followed by am or pm in upper
  or lower case, in which case a 12-hour clock is assumed.
  
  If the DateTime function is invoked with two string arguments, then
  the first argument is treated as a format string and the second
  argument is parsed as a date-time value according to the format
  string.
  
  If the DateTime function is invoked with a single Numeric argument,
  then a DateTime object is returned that is that number of days after
  January 1, 1900. A negative argument will yield a date-time value
  before 1900.
  
  If the function is invoked with two numeric arguments, then the first
  is taken to be an integer year, the second argument is taken to be an
  offset in days from the beginning of the year, and the date-time value
  returned is the given offset number of days from the beginning of the
  givem year. The offset may be positive or negative. This form of the
  function is commonly used to convert a year and julean day to a
  date-time value. Two-digit years are assumed to be in the twentieth
  century.
  
  If the function is invoked with three or more numeric arguments, then
  the first is taken to be an integer year, the second is taken to be an
  integer month, and the third is taken to be an integer day. If the
  combination of values is not valid, then a DateTime.error is
  raised. Two-digit years are assumed to be in the twentieth
  century. The fourth, fifth, and sixth arguments are floating point,
  positive or negative offsets in units of hours, minutes, and days, and
  default to zero if not given. The date returned is the date-time
  object corresponding to the given year, month, and day, offset by the
  given hours, minutes, and seconds.

The description above is from my current DateTime module, except that
I've added provision for an optional format string.

My thought is that there would be a DateTime class that would be
called to create instances as described above.  Data such as 
month and day names would be stored in dictionaries as class attributes
that could be replaced with locale-specific values.

> =================
> DB-SIG  - SIG on Tabular Databases in Python
> 
> send messages to: db-sig@python.org
> administrivia to: db-sig-request@python.org
> =================

-- 
Jim Fulton         Digital Creations
jim@digicool.com   540.371.6909
## Python is my favorite language ##
##     http://www.python.org/     ##

=================
DB-SIG  - SIG on Tabular Databases in Python

send messages to: db-sig@python.org
administrivia to: db-sig-request@python.org
=================