Package qubx :: Module date
[hide private]
[frames] | no frames]

Source Code for Module qubx.date

 1  """Date/time conversion between module datetime, Delphi, and "HH:MM:SS". 
 2   
 3  Copyright 2007-2012 Research Foundation State University of New York  
 4  This file is part of QUB Express.                                           
 5   
 6  QUB Express is free software; you can redistribute it and/or modify           
 7  it under the terms of the GNU General Public License as published by  
 8  the Free Software Foundation, either version 3 of the License, or     
 9  (at your option) any later version.                                   
10   
11  QUB Express is distributed in the hope that it will be useful,                
12  but WITHOUT ANY WARRANTY; without even the implied warranty of        
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
14  GNU General Public License for more details.                          
15   
16  You should have received a copy of the GNU General Public License,    
17  named LICENSE.txt, in the QUB Express program directory.  If not, see         
18  <http://www.gnu.org/licenses/>.                                       
19   
20  """ 
21   
22  import datetime 
23  import time 
24   
25  epoch = datetime.datetime(1899, 12, 30) 
26   
27  DayPerSec = 1.0 / (3600*24) 
28  DayPerUsec = DayPerSec / 1e6 
29   
30 -def DelphiToDatetime(delphi):
31 """Delphi stores datetime as double-precision seconds since 12/30/1899.""" 32 return epoch + datetime.timedelta(delphi)
33 -def DatetimeToDelphi(dt):
34 """Delphi stores datetime as double-precision seconds since 12/30/1899.""" 35 delta = dt - epoch 36 return (delta.days 37 + delta.seconds * DayPerSec 38 + delta.microseconds * DayPerUsec)
39 40 # ***************************************************************************** 41
42 -def SecondsToHMS(x, min_places=1):
43 """Returns x seconds formatted as "[-] [[HH:]MM:]SS." """ 44 if x < 0: 45 x = -x 46 prefix = '-' 47 else: 48 prefix = '' 49 s = x % 60 50 s, fs = int(s), s - int(s) 51 s_str = "%02d" % s 52 if fs: 53 s_str = "%s%s" % (s_str, ("%.3f" % fs)[1:]) 54 m = int(x) / 60 55 h, m = m/60, m%60 56 if h or (min_places >= 3): 57 return "%s%02d:%02d:%s" % (prefix,h,m,s_str) 58 elif m or (min_places == 2): 59 return "%s%02d:%s" % (prefix,m,s_str) 60 else: 61 return "%s%s" % (prefix,s_str)
62
63 -def HMSToSeconds(hms):
64 """Returns the number of seconds in "[-] [[HH:]MM:]SS." """ 65 result = 0 66 part = hms 67 while True: 68 i = part.find(':') 69 if i < 0: 70 return 60*result + int(part) 71 result = 60*result + int(part[:i]) 72 part = part[i+1:]
73 74
75 -def formatUnixDate(idate):
76 return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(idate))
77