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


Reply via email to