Code covered by the BSD License  

Highlights from
daylightGMT.m

image thumbnail
from daylightGMT.m by Andranik Valedi
Converts GMT time to local time taking into account local DST rules. Has dynamic DST evalution.

daylightGMT.m
function[LocalTime]=daylightGMT(cDate,cTimeArray)

%Converts UTC time to local daylight savings time.
%Usage: [LocalTime]=daylightGMT(cDate,cTimeArray)
%
%INPUT:
%   cDate: Date String of format 'mm-dd-yy'.
%   cTimeArray: Numeric array of GMT time values in MATLAB date-number format.
%
%OUTPUT
%   LocalTime:  Numeric Array of adjusted time values in Matlab date-number
%   format.
%
%Modify the parameters listed under 'Variable Declaration' to suit your
%region's DST rules. The default setting is Eastern Time (USA), since we
%made this program in Massachusetts, USA.
%
%PLEASE NOTE: int2strz.m M-File is required for this code to work. Download
%Mr. Carlos Adrian Vargas Aguilera's M-File at:
%
%http://www.mathworks.com/matlabcentral/fileexchange/12973
%
%Place the file int2strz.m in your working directory.
%
%Daylight Savings Time Starts on the Second Sunday in March.
%Daylight Savings Time Ends on the First Sunday in November.
%
%Example:
%
%   timevalues=timescale([50:55])
%
%   timevalues =
%       733774.002268519
%       733774.002314815
%       733774.002361111
%       733774.002407407
%       733774.002453704
%       733774.0025
%
%   localtime=daylightGMT('03-08-09',timevalues)
%
%   localtime =
%       733839.793935185
%       733839.793981481
%       733839.794027778
%       733839.794074074
%       733839.79412037
%       733839.794166667
%
%   datestr(timevalues(1),'HH:MM:SS') --> 00:03:16
%   datestr(localtime(1),'HH:MM:SS') --> 19:03:16

%Created by Shashi Murthy and Andranik Valedi, Beacon Power Corp.

%======Variable Declarations (can be modified to suit local rules)=====

%GMT Adjustment Values - Define Here!
AdjDST=5;               %GMT to Local Time Adjustment Hours (not DST)
AdjNoDST=AdjDST-1;      %GMT to Local Time Adjustment Hours (w/DST)

%DST starts and ends at 2:00 AM
dstHour=2;

%DST starts on the 2nd Sunday of March
dstStartMonth='03';                     %March
dstStartWD=1;                           %Sunday
dstStartWDnum=2;                        %Second Sunday

%DST ends on the 1st Sunday of November
dstEndMonth='11';                       %November
dstEndWD=1;                             %Sunday
dstEndWDnum=1;                          %First Sunday

%---------------------------------------------------------------------

%Gets daylight savings time start date for current year
for i=1:1:(dstStartWDnum*7)                         %Take the maximum number of samples (up to the week of DST starting)
    dstStartDate=datenum([dstStartMonth,'-',int2strz(i,2),'-',cDate(7:8)]);
    sWeekdays(i)=weekday(dstStartDate);
    if sWeekdays(length(sWeekdays))==dstStartWD && length(find(sWeekdays==dstStartWD))==dstStartWDnum
        break
    end
end
dstStartDateTime=dstStartDate+(dstHour/24);         %Official Start Day and Hour of DST

%Gets daylight savings time end date for current year
for i=1:1:(dstEndWDnum*7)           %Take the maximum number of samples (up to the week of DST ending)
    dstEndDate=datenum([dstEndMonth,'-',int2strz(i,2),'-',cDate(7:8)]);
    eWeekdays(i)=weekday(dstEndDate);
    if eWeekdays(length(eWeekdays))==dstEndWD && length(find(eWeekdays==dstEndWD))==dstEndWDnum
        break
    end
end
dstEndDateTime=dstEndDate+(dstHour/24);             %Official End Day and Hour of DST

%===========Append date to input time data array===========
if isnumeric(cTimeArray)==1
    tempTime=floor(cTimeArray(1));
    cDateTimeArray=floor(datenum(cDate))+(cTimeArray-tempTime);
end

%*****************Starts Processing input time array*******************
LocalTime=cDateTimeArray-(AdjDST/24);
for s=1:1:length(cDateTimeArray)
    if LocalTime(s)>=(dstStartDateTime) && LocalTime(s)<(dstEndDateTime)
        LocalTime(s)=LocalTime(s)+(1/24);
    end
end

Contact us at files@mathworks.com