I’ve been working hard on the Sync engine behind SyncTheMilk, which means doing tons of work with the Outlook COM model… it also means getting up to speed with Win32 scripting dates.
If you’re in the market for a good discussion of how Win32 scripting dates work, check out Chip Pearson’s article. If you thought Java dates were ugly..
Win32 dates are kept as an offset from an epoch of Jan 0, 1900. Yep, that’s no typo. January Zero. They have an integer part which represents the number of days since epoch, and then a fractional part that represents the percentage of the current day. So… in Canberra local time…
39220.54722673611 == Fri May 18 13:08:00 EST 2007
I know what you’re thinking… How about a little code so I can google for this later? Sure… Here’s a little quick and dirty Groovy for converting Win32 dates to Java dates:
Date winToJavaDate(BigDecimal d) { if (!d) return null def daysSinceEpoch = d.intValue() def percentOfDay = d - daysSinceEpoch Calendar jCal = Calendar.getInstance() jCal.set(1900, 0, 0, 0, 0, 0) jCal.add(Calendar.DATE, daysSinceEpoch-1) def msPerDay = 1000 * 60 * 60 * 24 def msOffset = msPerDay * percentOfDay jCal.add(Calendar.MILLISECOND, msOffset.intValue()) return jCal.getTime() }
And you’re off and running. A little more work on my Syncing robustness and I’ll have my first little rich UI Groovy-powered app to set free…
Stay tuned…