Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created September 28, 2013 00:52
Show Gist options
  • Save ironpythonbot/6737169 to your computer and use it in GitHub Desktop.
Save ironpythonbot/6737169 to your computer and use it in GitHub Desktop.
CodePlex Issue #18106 Plain Text Attachments
import time
import datetime
import sys
_ordinal_1899_12_31=datetime.date(1899,12,31).toordinal()-1
def COMDateFromTuple(YMDHMSmsTuple):
d = datetime.date(YMDHMSmsTuple[0],YMDHMSmsTuple[1],YMDHMSmsTuple[2])
integerPart = d.toordinal() - _ordinal_1899_12_31
ms = ((YMDHMSmsTuple[3]*60 \
+YMDHMSmsTuple[4])*60 \
+YMDHMSmsTuple[5])*1000 \
+YMDHMSmsTuple[6]
fractPart = ms / 86400000.0
return integerPart + fractPart
def DateTupleFromCOMDate(comDate):
'Returns ticks since 1970'
fcomDate = float(comDate)
secondsperday=86400 # 24*60*60
#ComDate is number of days since 1899-12-31
t=time.gmtime(secondsperday*(fcomDate-25569.0))
return t #year,month,day,hour,minute,second,weekday,julianday,daylightsaving=t
def DateTimeFromCOMDate(comDate):
#ComDate is number of days since 1899-12-31
fComDate=float(comDate)
millisecondsperday=86400000 # 24*60*60*1000
integerPart = int(fComDate)
floatpart=fComDate-integerPart
if floatpart == 0.0:
return datetime.date.fromordinal(integerPart + _ordinal_1899_12_31)
dte=datetime.datetime.fromordinal(integerPart + _ordinal_1899_12_31) \
+ datetime.timedelta(milliseconds=floatpart*millisecondsperday)
return dte
print
print sys.version
tt = (2002,6,28,15,20,01, 4,31+28+31+30+31+28,-1)
print 'Starting with tt =',tt
cd1 = COMDateFromTuple(tt)
print 'COMDate=',cd1
print 'mktime=',time.mktime(tt)
print 'using time.gmtime=',DateTupleFromCOMDate(cd1)
print 'using datetime = ',DateTimeFromCOMDate(cd1)
try:
import System
Iron = True
except ImportError:
Iron = False
if Iron:
d = System.DateTime.FromOADate(cd1)
print 'using System = ',d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment