Quantcast

Question re. Daylight Savings Time

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Question re. Daylight Savings Time

Marshall Blythe
I've never used Joda time before now, but I know of it's reputation
and it seems like a natural fit for the problem I'm trying to solve.

I'm working on an application that sends notifications to users'
iPhones. These notifications can be generated at any time of day, and
I have a requirement to support the concept of a "Do Not Disturb"
(DND) interval for each user. The reasoning is that some users may
wish to avoid being bothered with notifications during certain hours
of the day (such as the overnight hours). If the application needs to
send a notification to a user it should do so only if the current time
falls outside of the user's specified DND interval.

I plan on storing the following information in each user's profile:

 - User's time zone
 - DND begin hour of day  & minute of hour
 - DND end hour of day  & minute of hour

With this information a user can effectively tell the system, for
example, "I'm in the US/Pacific time zone, and I do not wish to be
notified between 00:00 and 06:00". In this case I create the following
Joda objects using information from the user's profile:

 LocalTime dndBeginTime = new LocalTime(0, 0);
 LocalTime dndEndTime = new LocalTime(6, 0);
 DateTimeZone userTimeZone = DateTimeZone.forID("US/
Pacific");

Now I need to figure out where the current time falls in relation to
the DND interval specified by the user, so I use this code:

 // First create an Interval with the user's time zone and
 // bounded by the begin & end times in the user's profile
 DateTime dndBegin = dndBeginTime.toDateTimeToday(userTimeZone);
 DateTime dndEnd = dndEndTime.toDateTimeToday(userTimeZone);
 Interval dndInterval = new Interval(dndBegin, dndEnd);

 if (dndInterval.containsNow()) {
    // Inside the DND interval- do not send the notification
 }
 else {
    // Outside the DND interval- send the notification
 }

This code seemed to work well until I hit upon a problem caused by
Daylight Savings Time. Suppose the user specifies a time that is
within the 02:00 hour, for example:

 LocalTime dndBeginTime = new LocalTime(2, 30);

In this case my code will fail on the following line during the
transition from Standard Time to Daylight Savings Time:

 DateTime dndBegin = dndBeginTime.toDateTimeToday(userTimeZone);

I get the following exception (I used DateTimeUtils to set the date
for testing- great feature BTW):

 org.joda.time.IllegalFieldValueException: Value 2 for hourOfDay is
not supported:
 Illegal instant due to time zone offset transition:
2010-03-14T02:30:00.000 (America/Los_Angeles)

I understand why this is happening: there is no 02:30 instant on March
14, 2010. Time jumps from 01:59 directly to 03:00. However, I'd like
to deal with this more gracefully. Logically, the user still has a DND
internal- it's just been shortened by 30 minutes.

Does anyone have any suggestions for dealing with this? I suspect
there may be a better way somewhere in the API, but I'm not seeing it
right now.

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question re. Daylight Savings Time

jodastephen
In Joda-Time, you have to catch the exception, and then use the
low-level utilities in DateTimeZone to find the next transition. That
allows you to adjust the instant to a valid one.

As you might expect, JSR-310 handles this much better.

Stephen


On 15 July 2010 13:27, Marshall Blythe <[hidden email]> wrote:

> I've never used Joda time before now, but I know of it's reputation
> and it seems like a natural fit for the problem I'm trying to solve.
>
> I'm working on an application that sends notifications to users'
> iPhones. These notifications can be generated at any time of day, and
> I have a requirement to support the concept of a "Do Not Disturb"
> (DND) interval for each user. The reasoning is that some users may
> wish to avoid being bothered with notifications during certain hours
> of the day (such as the overnight hours). If the application needs to
> send a notification to a user it should do so only if the current time
> falls outside of the user's specified DND interval.
>
> I plan on storing the following information in each user's profile:
>
>  - User's time zone
>  - DND begin hour of day  & minute of hour
>  - DND end hour of day  & minute of hour
>
> With this information a user can effectively tell the system, for
> example, "I'm in the US/Pacific time zone, and I do not wish to be
> notified between 00:00 and 06:00". In this case I create the following
> Joda objects using information from the user's profile:
>
>  LocalTime dndBeginTime = new LocalTime(0, 0);
>  LocalTime dndEndTime = new LocalTime(6, 0);
>  DateTimeZone userTimeZone = DateTimeZone.forID("US/
> Pacific");
>
> Now I need to figure out where the current time falls in relation to
> the DND interval specified by the user, so I use this code:
>
>  // First create an Interval with the user's time zone and
>  // bounded by the begin & end times in the user's profile
>  DateTime dndBegin = dndBeginTime.toDateTimeToday(userTimeZone);
>  DateTime dndEnd = dndEndTime.toDateTimeToday(userTimeZone);
>  Interval dndInterval = new Interval(dndBegin, dndEnd);
>
>  if (dndInterval.containsNow()) {
>     // Inside the DND interval- do not send the notification
>  }
>  else {
>     // Outside the DND interval- send the notification
>  }
>
> This code seemed to work well until I hit upon a problem caused by
> Daylight Savings Time. Suppose the user specifies a time that is
> within the 02:00 hour, for example:
>
>  LocalTime dndBeginTime = new LocalTime(2, 30);
>
> In this case my code will fail on the following line during the
> transition from Standard Time to Daylight Savings Time:
>
>  DateTime dndBegin = dndBeginTime.toDateTimeToday(userTimeZone);
>
> I get the following exception (I used DateTimeUtils to set the date
> for testing- great feature BTW):
>
>  org.joda.time.IllegalFieldValueException: Value 2 for hourOfDay is
> not supported:
>  Illegal instant due to time zone offset transition:
> 2010-03-14T02:30:00.000 (America/Los_Angeles)
>
> I understand why this is happening: there is no 02:30 instant on March
> 14, 2010. Time jumps from 01:59 directly to 03:00. However, I'd like
> to deal with this more gracefully. Logically, the user still has a DND
> internal- it's just been shortened by 30 minutes.
>
> Does anyone have any suggestions for dealing with this? I suspect
> there may be a better way somewhere in the API, but I'm not seeing it
> right now.
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
>

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question re. Daylight Savings Time

Marshall Blythe
In reply to this post by Marshall Blythe
> In Joda-Time, you have to catch the exception, and then use the
> low-level utilities in DateTimeZone to find the next transition. That
> allows you to adjust the instant to a valid one.

Thanks- that works!

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...