Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
function [secondsSinceYYYY] = date2seconds(date, startYear)
% %
% % Calculate a date string of fixed formate to the number of seconds since 00 UTC
% % of 01 January of a given year.
% %
% % Input parameter(s):
% % date : date and time string of the formate YYYY-MM-DDTHH:MM:SS*;
% % any characters at the end (*) are not used
% % startYear (optional) : year from which on seconds will be calculated
% %
% % Output parameter(s):
% % secondsSinceYYYY : Number of seconds since input start year
if nargin < 2
startYear = 1970;
end
nYears = str2num(date(1:4));
nMonths = str2num(date(6:7));
nDays = str2num(date(9:10));
nHours = str2num(date(12:13));
nMinutes = str2num(date(15:16));
nSeconds = str2num(date(18:19));
% convert number of years (excluding the current one) to number of days
yyyy = startYear;
numDays = 0;
while yyyy < nYears
if (mod(yyyy,4)~=0)
numDays = numDays + 365;
else
numDays = numDays + 366;
end
yyyy = yyyy + 1;
end
% convert number of months of current year to number of days
if (nMonths > 1)
daysPerMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
mm = 1;
while mm < nMonths
numDays = numDays + daysPerMonth(mm);
if ( mm==2 && mod(nYears,4)==0 )
numDays = numDays + 1;
end
mm = mm + 1;
end
end
if (nDays > 1)
numDays = numDays + (nDays-1);
end
secondsSinceYYYY = numDays*24*3600 + nHours*3600 + nMinutes*60 + nSeconds;