Recipe 3.8. Checking Whether Daylight Saving Time Is in EffectProblemYou want to see whether the current time in your locale is normal time or Daylight Saving/Summer Time. SolutionCreate a Time object and check its
DiscussionTime objects representing UTC times will always return false when isdst is called, because UTC is the same year-round. Other Time objects will consult the daylight saving time rules for the time locale used to create the Time object. This is usually the sysem locale on the computer you used to create it: see Recipe 3.7 for information on changing it. The following code demonstrates some of the rules pertaining to Daylight Saving Time across the United States:
The C library on which Ruby's Time class is based handles the complex rules for Daylight Saving Time across the history of a particular time zone or locale. For instance,
A U.S. law passed in 2005 expands Daylight Saving Time into March and November, beginning in 2007. Depending on how old your zoneinfo file is, Time objects you create for dates in 2007 and beyond might or might not reflect the new law.
This illustrates a general point. There's nothing your elected officials love more than passing laws, so you shouldn't rely on isdst to be accurate for any Time objects that represent times a year or more into the future. When that time actually comes around, Daylight Saving Time might obey different rules in your locale. The Date class isn't based on the C library, and knows nothing about time zones or locales, so it also knows nothing about Daylight Saving Time. See Also
|
Thursday, October 22, 2009
Recipe 3.8. Checking Whether Daylight Saving Time Is in Effect
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment