Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'languages,': 0.03; 'elif': 0.04; 'filename:fname piece:py': 0.04; 'int': 0.05; 'parameter': 0.05; '"""': 0.07; "shouldn't": 0.07; 'something,': 0.07; 'type,': 0.07; 'python': 0.08; 'assumed': 0.09; 'integer,': 0.09; 'output': 0.11; 'def': 0.12; '#return': 0.16; '(),': 0.16; 'comma': 0.16; 'methods,': 0.16; 'self,': 0.16; 'simpler.': 0.16; 'skip:) 10': 0.16; 'stef': 0.16; 'valueerror': 0.16; 'class,': 0.16; 'float': 0.16; 'traceback': 0.16; 'input': 0.17; 'meant': 0.18; 'rewrite': 0.19; 'cheers,': 0.19; 'maybe': 0.23; 'converts': 0.23; 'helper': 0.23; 'integer': 0.23; 'specify': 0.25; 'stored': 0.25; 'specified': 0.26; 'subject: : ': 0.26; 'string': 0.26; 'raise': 0.28; 'import': 0.29; 'class': 0.29; 'received:209.85.215': 0.30; 'it.': 0.31; 'equivalent': 0.31; 'print': 0.31; "skip:' 10": 0.32; 'used,': 0.32; 'someone': 0.33; 'to:addr:python-list': 0.33; 'error': 0.33; 'things': 0.33; 'header:User-Agent:1': 0.35; 'round': 0.35; 'try:': 0.35; 'else': 0.35; 'using': 0.35; 'message-id:@gmail.com': 0.36; 'none': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'floating': 0.37; 'another': 0.37; 'hello,': 0.38; 'received:192': 0.38; 'sometimes': 0.39; 'skip:s 20': 0.39; 'received:209': 0.39; 'difficult': 0.39; 'to:addr:python.org': 0.39; 'current': 0.40; 'format': 0.40; 'received:192.168.1': 0.40; 'year': 0.61; 'week,': 0.65; 'skip:* 70': 0.68; ')))': 0.84; 'datetime': 0.84; 'delphi': 0.84; 'packed': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:x-enigmail-version:content-type; bh=tf6bbWK+7AVbpAnBbbxML2aGlEh+lugcP/0v51HwBKo=; b=L2xlr51eCAolo3GTmrBtPdcZaSE4GKmtxq6C1oAfPN4Q/AMtmwap7cjQspJfH+61ie B/ZM/2PvLFI2cwxXGOoymOZqX4K8E6zDIrk8ZxFT5qqlGgPf+MVY/IGLPKODiQs1BcXs 2deuC2XvMr+IpPd9dTxznPfvfkd0OqWYhq4+Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :x-enigmail-version:content-type; b=gJzbFENekPnms6xFo+zYI9A4j/u5jVOKOOUmP0BwX6WYv0tT8Lsk2sBAA6d/wlgFrT uyjzyTbPwRpnoHW0xX25W312ukO/2/jGc1bnavy6HxTt7lWz/4VfDRKXhvItIGEcWG3C 20weDDunxZerqTLxBQmT1KRfNAvBq6oC69t5A= Date: Fri, 20 May 2011 20:27:56 +0200 From: Stef Mientki User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070728 Thunderbird/2.0.0.6 Mnenhy/0.7.5.666 MIME-Version: 1.0 To: python-list@python.org Subject: maybe useful : datetime conversion X-Enigmail-Version: 1.1.1 Content-Type: multipart/mixed; boundary="------------010304010205020303040102" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 200 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305916080 news.xs4all.nl 49177 [::ffff:82.94.164.166]:59210 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5869 This is a multi-part message in MIME format. --------------010304010205020303040102 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 --------------010304010205020303040102 Content-Type: text/x-python; name="module1.py" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="module1.py" 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 () # ************************************************************************ --------------010304010205020303040102--