Thanks Jacob! Do you know if this will handle daylight savings time appropriately? The use case is for time-stamping historical exchange data, so the conversion should correctly account for the switch between EST and EDT.
On Wednesday, July 8, 2015 at 3:35:55 PM UTC-4, Jacob Quinn wrote: > > TimeZones.jl isn't quite ready for public consumption yet (enough public > clamor will eventually get me to fix it up). > > How about the following? > > function calcSecondsEpochToMidnight(secondsSinceEpoch::Integer) > utc = DateTime(Date(Dates.unix2datetime(secondsSinceEpoch))) > adjustment = Dates.Second(div(Dates.value(now() - > now(Dates.UTC)),1000)) > return Dates.value(utc+adjustment) > end > > Obviously this will only work if you're in EST (now() will return a > DateTime according to your system time), so it's not very portable, but it > also doesn't require any other timezone fiddling. > > -Jacob > > On Wed, Jul 8, 2015 at 8:59 AM, Tom Breloff <[email protected] > <javascript:>> wrote: > >> I have some code which requires figuring out the number of seconds from >> the Epoch until midnight (local time) in order to quickly compute the local >> TimeOfDay. The reason is that I get passed a field which is seconds since >> Epoch, and I'd like to just subtract off the (cached) # seconds from >> Epoch-->Midnight. >> >> Since I'm using a cached number, I don't care so much how long it takes >> to calculate. Right now I use both Dates and Calendar.jl, but I'm >> wondering if I can accomplish this without the dependency on Calendar.jl >> (which I currently use ONLY to get the hours offset between Eastern US and >> UTC). Is there a better way to write this function? >> >> >> function getHoursAdjustmentFromUTC(year::Integer, month::Integer, >> day::Integer) >> millisEST = *Calendar.ymd*(year, month, day, "EST5EDT").millis >> millisUTC = *Calendar.ymd*(year, month, day, "UTC").millis >> UInt64(round((millisEST - millisUTC) / (secondsInOneHour * >> millisInOneSecond))) >> end >> >> getEpochMillis() = UInt64(DateTime(1970,1,1).instant.periods.value) >> createUTCDateTimeFromSecondsSinceEpoch(secondsSinceEpoch::Integer) = >> DateTime(Dates.UTM(secondsSinceEpoch * millisInOneSecond + >> getEpochMillis())) >> >> >> # this is the function I care about... note that "midnight" refers to >> midnight local to Eastern US >> function calcSecondsEpochToMidnight(secondsSinceEpoch::Integer) >> >> dt = createUTCDateTimeFromSecondsSinceEpoch(secondsSinceEpoch) >> >> # get the hour adjustment using the Calendar module >> y = Dates.year(dt) >> m = Dates.month(dt) >> d = Dates.day(dt) >> hourAdjustment = getHoursAdjustmentFromUTC(y, m, d) >> >> millisMidnightUTC::UInt64 = DateTime(y, m, d).instant.periods.value >> millisMidnightEST::UInt64 = millisMidnightUTC + hourAdjustment * >> secondsInOneHour * millisInOneSecond >> >> return UInt64((millisMidnightEST - getEpochMillis()) / >> millisInOneSecond) >> end >> >> >> >
