Given a DateTimeZone date and two instances, determine if LocalTime falls between two instances

For each of my users, I save tzid, which I convert to DateTimeZone, containing information about my local time zone.

I want to send the user a daily email address at 8 a.m. local time; if 8 AM is ambiguous for any reason, like daylight saving, I just need to choose one of 8 AM; I don't care what.

My work is done hourly, and I have Instant, containing the last time the task was completed, and another Instant, containing the next time the task was completed.

Given these two Instant, called previousRunand nextRun, and DateTimeZonecalled tz, how would I determine if the localTimetitle is eightAMbetween the boundaries of this assignment? If so, I need to send the user an email.

+4
source share
1 answer

Given these two Instant Calls previousRun and nextRun, and DateTimeZone is called tz, how would I determine if a localTime called 8AM would fall between the boundaries of this job?

, , . , , , ( , , 8 , ) - :

public static bool ShouldSendEmail(Instant previousRun, Instant nextRun,
                                   DateTimeZone zone)
{
    // Find the instant at which we should send the email for the day containing
    // the last run.
    LocalDate date = previousRun.InZone(zone).Date;
    LocalDateTime dateTime = date + new LocalTime(8, 0);
    Instant instant = dateTime.InZoneLeniently(zone).ToInstant();

    // Check whether that between the last instant and the next one.
    return previousRun <= instant && instant < nextRun;
}

InZoneLeniently, , , , : , , 8 .

, , .

EDIT: " ", - previousRun:

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;
}

: ", , , ".

+4

All Articles