Permalink
Cannot retrieve contributors at this time
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?
showRecordsLive/seconds2date.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
108 lines (97 sloc)
2.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function date = seconds2date(secondsSinceYYYY, 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 | |
% Number of years | |
yyyy = startYear; | |
nSeconds = 0; | |
while nSeconds < secondsSinceYYYY | |
if (mod(yyyy,4)~=0) | |
daysPerYear = 365; | |
else | |
daysPerYear = 366; | |
end | |
if (nSeconds + daysPerYear * 86400 > secondsSinceYYYY) | |
%disp('in here') | |
break; % don't add any more seconds | |
else | |
nSeconds = nSeconds + daysPerYear * 86400; | |
end | |
yyyy = yyyy + 1; | |
end | |
nYears = yyyy; | |
restSeconds = secondsSinceYYYY - nSeconds; | |
% Number of months | |
mm = 1; | |
daysPerMonth = [31 28 31 30 31 30 31 31 30 31 30 31]; | |
nSeconds = 0; | |
while nSeconds < restSeconds | |
if ( mod(yyyy,4)==0 && mm==2 ) | |
DPM = 29; | |
else | |
DPM = daysPerMonth(mm); | |
end | |
if (nSeconds + DPM * 86400 > restSeconds) | |
break; % don't add any more seconds | |
else | |
nSeconds = nSeconds + DPM * 86400; | |
end | |
mm = mm + 1; | |
end | |
nMonths = mm; | |
restSeconds = restSeconds - nSeconds; | |
% Number of days | |
dd = 1; | |
nSeconds = 0; | |
while nSeconds < restSeconds | |
if (nSeconds + 86400 > restSeconds) | |
break; % don't add any more seconds | |
else | |
nSeconds = nSeconds + 86400; | |
end | |
dd = dd + 1; | |
end | |
nDays = dd; | |
restSeconds = restSeconds - nSeconds; | |
% Number of hours | |
hh = 0; | |
nSeconds = 0; | |
while nSeconds < restSeconds | |
if (nSeconds + 3600 > restSeconds) | |
break; % don't add any more seconds | |
else | |
nSeconds = nSeconds + 3600; | |
end | |
hh = hh + 1; | |
end | |
nHours = hh; | |
restSeconds = restSeconds - nSeconds; | |
% Number of minutes | |
min = 0; | |
nSeconds = 0; | |
while nSeconds < restSeconds | |
if (nSeconds + 60 > restSeconds) | |
break; % don't add any more seconds | |
else | |
nSeconds = nSeconds + 60; | |
end | |
min = min + 1; | |
end | |
nMinutes = min; | |
restSeconds = restSeconds - nSeconds; | |
% Number of seconds | |
nSeconds = fix(restSeconds); | |
%restSeconds = restSeconds - nSeconds | |
date = [num2str(nYears,'%4d'),'-',num2str(nMonths,'%02d'),'-',num2str(nDays,'%02d'),' ',... | |
num2str(nHours,'%02d'),':',num2str(nMinutes,'%02d'),':',num2str(nSeconds,'%02d')]; | |