hello,

using datetimes from a lot of different sources,
in many languages,
I had about 30 python helper routines,
which I now packed in one class,
much simpler.
Although I used the Delphi date-format as the base,
it shouldn't be difficult to rewrite the class for another type.

The input can be one of the following types :
  - None            : the current date-time is used
  - 30000.9         : a Delphi datetime
  - 30000           : a Delphi datetime
  - "30000.9"       : a Delphi datetime as a string
  - "30000,9"       : a Delphi datetime as a (Dutch) string
  - "20-5-11"              : short year notation
  - "20-05-2011"           : long year notation
  - "2009-09-24 10:12:24"  : Access string
  - datetime.datetime ( 2011, 1, 15 )
  - time.struct_time
  - wx.DateTime
  - time.time()  (through method from_time)

Maybe someone can use it.

cheers,
Stef


import time
import datetime
import wx

Delphi_Date_0 = datetime.date ( *time.strptime ( '30-12-1899', '%d-%m-%Y' )[0:3]).toordinal()

# ************************************************************************
# ************************************************************************
class Delphi_Date ( float ) :
  def __new__ ( self, Something = None ) :
    """
Class meant to handle any datetime type, and converts it basically to
a Delphi DateTime (float: number of days since 1-1-1900).

The input can be one of the following types :
  - None            : the current date-time is used
  - 30000.9         : a Delphi datetime
  - 30000           : a Delphi datetime
  - "30000.9"       : a Delphi datetime as a string
  - "30000,9"       : a Delphi datetime as a (Dutch) string
  - "20-5-11"              : short year notation
  - "20-05-2011"           : long year notation
  - "2009-09-24 10:12:24"  : Access string
  - datetime.datetime ( 2011, 1, 15 )
  - time.struct_time
  - wx.DateTime

with extra methods, also the following can be used
  - from_time       : time.time float

The following output methods are available
  - to_time ()
  - to_datetime ()
  - to_String ( self , Format = "%d-%m-%Y" )
  - to_String_Short ()
  - to_String_Date_Time_Short ()
  - to_String_Time_Short ()
  - to_String_Date_Time ()
  - to_wxTime ()
  - to_Iso ()
    """

    # The current date-time is used, if no parameter is specified
    if Something is None :
      Something = datetime.datetime.now ()

    # floating point is assumed to be a Delphi datetime
    # to specify a time.time float, use the method from_time
    #     Delphi_Date().from_time ( time.time()
    # which is equivalent to
    #     Delphi_Date()
    if isinstance ( Something, float ) :
      Value = Something

    # sometimes a Delphi datetime is stored as an integer
    elif isinstance ( Something, int ) :
      Value = Something

    # A string can represent a lot of things
    elif isinstance ( Something, basestring ) :

      # a float or integer,
      # also the Ducth notation where deceimal separator is a comma
      try :
        Value = float ( Something.replace(',','.') )
      except :

        # a string as a short year notation
        try :
          Value = datetime.datetime.strptime ( Something, '%d-%m-%y' )
        except ValueError :

          # a string as a long year notation
          try:
            Value = datetime.datetime.strptime ( Something, '%d-%m-%Y' )
          except :

            # a string as a (Dutch) Access notation
            try :
              # Access string : "2009-09-24 00:00:00"
              Value =  datetime.datetime.strptime ( Something.split(' ')[0], "%Y-%m-%d" )
            except :
              Value = Delphi_Date_0
              import traceback
              traceback.print_exc

        Value = Value.toordinal() - Delphi_Date_0

    # datetime.datetime ()
    elif isinstance ( Something, datetime.datetime ) :
      Value = Something.toordinal() - Delphi_Date_0

    # time.struct_time
    elif isinstance ( Something, time.struct_time ) :
      Value = time.mktime ( Something )
      DT = datetime.datetime.fromtimestamp ( Value )
      Value = DT.toordinal() - Delphi_Date_0

    # wx.DateTime
    elif isinstance ( Something, wx.DateTime ) :
      DT = datetime.date ( Something.GetYear (),
                           Something.GetMonth () + 1,
                           Something.GetDay () )
      Value = DT.toordinal() - Delphi_Date_0

    else :
      print type(Something), Something
      raise error ( 'aap' )

    return float.__new__ ( self, Value )

  def from_time ( self, Time ) :
    DT = datetime.datetime.fromtimestamp ( Time )
    return Delphi_Date ( DT )


  def to_time ( self ):
    return time.mktime ( self.to_datetime().timetuple() )

  def to_datetime ( self ) :
    #return datetime.datetime.fromordinal ( int ( round ( self + Delphi_Date_0 )))
    return datetime.datetime.fromordinal ( self + Delphi_Date_0 )

  def to_String ( self , Format = "%d-%m-%Y" ) :
    DT = self.to_datetime()
    try :
      return DT.strftime ( Format )
    except :
      return '01-01-1900'

  def to_String_Short ( self ) :
    DT = self.to_datetime()
    return DT.strftime ( "%d-%m-%y" )

  def to_String_Date_Time_Short ( self ) :
    return self.to_String ( "%d-%m-%Y %H:%M" )

  def to_String_Time_Short ( self ) :
    return self.to_String ( "%H:%M" )

  def to_String_Date_Time ( self ) :
    return self.to_String ( "%d-%m-%Y %H:%M:%S" )

  def to_wxTime( self ) :
   DT = self.to_datetime()
   WX = wx.DateTime()
   WX.Set ( DT.day, DT.month-1, DT.year )
   return WX

  def to_Iso ( self ) :
    """
  Transforms a Delphi Datetime into an ISO tuple: ( year, week, day-of-week )
    """
    return self.to_datetime().isocalendar ()
# ************************************************************************
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to