c# - Given a DateTimeZone and two Instants, determine if a LocalTime falls between the two Instants -


for each of users, store tzid convert datetimezone holding information local time zone.

i want send user daily email @ 8 local time; if 8 ambiguous whatever reason daylight savings shift, need pick 1 of 8 am; don't care which.

my job runs hourly, , have instant containing last run time of job, , instant containing next run time of job.

given these 2 instant called previousrun , nextrun, , datetimezone called tz how determine whether localtime called eightam falls between bounds of job run? if does, need send user email.

given these 2 instant called previousrun , nextrun, , datetimezone called tz how determine whether localtime called eightam falls between bounds of job run?

doing in general way tricky, think. however, if can rely on time want being away midnight and job run every hour (so don't need consider happens if hasn't run between midnight , 8am, example) think this:

public static bool shouldsendemail(instant previousrun, instant nextrun,                                    datetimezone zone) {     // find instant @ should send email day containing     // last run.     localdate date = previousrun.inzone(zone).date;     localdatetime datetime = date + new localtime(8, 0);     instant instant = datetime.inzoneleniently(zone).toinstant();      // check whether that's between last instant , next one.     return previousrun <= instant && instant < nextrun; } 

you can check docs inzoneleniently check result give, sounds don't mind: still send 1 email day, in hour contains 8am.

i haven't parameterized time of day precisely because harder handle general case time of day near midnight.

edit: if can store "next date send" it's easy - , don't need previousrun part:

public static bool shouldsendemail(localdatetime nextdate, instant nextrun,                                    datetimezone zone, localtime timeofday) {     localdatetime nextemaillocal = nextdate + timeofday;     instant nextemailinstant =  nextdatetime.inzoneleniently(zone).toinstant();     return nextrun > nextemailinstant; } 

basically says, "work out when next want send email - , if next run later that, should send now."


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -