Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

Note

Steve on Image Processing with MATLAB has been archived and will not be updated.

Time zone terminology

An email thread about a date-related design question had me thinking about terminology this week. (Note: There's nothing about image processing in today's post. Sorry!)

Specifically, I was thinking about the term time zone. In the U.S. you might see a date written this way:

November 21, 2004 1:02 PM (EST)

EST means Eastern Standard Time in the U.S. Or you might see a date written like this:

11/21/2004 8:30 (-04:00)

Many people refer to EST or -04:00 as the "time zone." However, because I work across the hall from the chief designer and implementer (Hi, P^2) of the new datetime suite of functions in MATLAB R2014b, I know that these are more properly referred to as time zone offsets.

A time zone is a much more complex concept than a simple offset from UTC. For example, a time zone encompasses both current and historical rules for using Daylight Saving Time.

The time zone offset for a given time depends not only on the time zone but also both the year and the day-of-year.

The dependence on the day-of-year is pretty obvious. In the fall season where I live, the time zone offset depends on whether the day is before or after the first Sunday in November.

d1 = datetime('October 15, 2014','TimeZone','America/New_York')
d1 = 

   15-Oct-2014

tzoffset(d1)
ans = 

   -4:00

d2 = datetime('November 15, 2014','TimeZone','America/New_York')
d2 = 

   15-Nov-2014

tzoffset(d2)
ans = 

   -5:00

Most of the state of Arizona does not observe Daylight Saving Time.

d3 = datetime('October 15, 2014','TimeZone','America/Phoenix')
d3 = 

   15-Oct-2014

tzoffset(d3)
ans = 

   -7:00

d4 = datetime('November 15, 2014','TimeZone','America/Phoenix')
d4 = 

   15-Nov-2014

tzoffset(d4)
ans = 

   -7:00

The dependence of time zone offset on the year is somewhat less obvious. It turns out that Daylight Saving Time rules change occasionally. In the U.S. the rules were last changed in 2007. Let's compare the time zone offset for noon on Halloween for 2007 and 2006.

d5 = datetime('October 31, 2007 12:00 PM','TimeZone','America/New_York')
d5 = 

   31-Oct-2007 12:00:00

tzoffset(d5)
ans = 

   -4:00

d6 = datetime('October 31, 2006 12:00 PM','TimeZone','America/New_York')
d6 = 

   31-Oct-2006 12:00:00

tzoffset(d6)
ans = 

   -5:00

Now for just a little fun with MATLAB R2014b date calculations. Most of the U.S. changes from Daylight Saving time to Standard Time at 2:00 AM on the first Sunday of November. Given the year, how can we compute that?

Here's just one way. Start with 2:00 AM on November 1 of the specified year.

year = 2014;
d = datetime(year,11,1,2,0,0)
d = 

   01-Nov-2014 02:00:00

Now use dateshift to move to the first Sunday.

dateshift(d,'dayofweek','Sunday')  % You can also use 1 instead of 'Sunday'.
ans = 

   02-Nov-2014 02:00:00

My horoscope for today said "Your next blog will include an example of an anonymous function for no good reason." So here it is. Let's make an anonymous function that returns the exact time for the transition from Daylight Saving Time to Standard time in the U.S.

f = @(year) dateshift(datetime(year,11,1,2,0,0),'dayofweek','Sunday')
f = 

    @(year)dateshift(datetime(year,11,1,2,0,0),'dayofweek','Sunday')

f(2014)
ans = 

   02-Nov-2014 02:00:00

f(2015)
ans = 

   01-Nov-2015 02:00:00

f(2016)
ans = 

   06-Nov-2016 02:00:00

Of course, this function only works for years between 2007 and whenever the U.S. Congress changes its mind again!

Next week, I promise, I'll get back to writing about image processing. Or maybe colormaps. Unless I write about something else.




Published with MATLAB® R2014b

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。