Task: Use the matlab time functions to determine how many days old you are. Determine the calendar date of the day at which you will be twice as many days old.
Calendars are painful to deal with. The major problem is that the time for the earth to orbit around the sun (the length of a year) is not evenly divisible by the time it takes the earth to spin once on its axis (the length of a day). Some calendars are based on the time it takes the moon to go around the earth (about a month) and how many months are required for the earth to go around the sun is also not evenly divisible.
There are many days (about 365) in a year so it is not always convenient to use yeardays (number of days since the beginning of the year) as a calendar. By traditions passed between various cultures over the years, we separate the year into 12 parts (months which are roughly related to the time for the moon to orbit once around the earth). All of these time-spans almost (but don't exactly) match in convenient ways.
The net effect is that keeping track of years, months and days is complicated and confusing. This is a special problem for scientists who try to keep time records of various measurements (ignoring the problem of measurements being made at different places on the earth where local (solar) time is different).
All of these problems are resolved in MATLAB by an internal time which counts the number of days (and fractions) since some arbitary time in the past (basically day zero of year zero) in the current calendar. This is similar to Julian Days which we will discuss shortly. The benefit of this MATLAB time is that we can easily calculate time intervals between events, among other things.
MATLAB has tools that convert between the currently accepted western (Gregorian) calendar dates and the MATLAB time. As a note, these conversions work over time spans of a few thousand years into the future and past from the current era. These calendar conversions become unreliables over many thousands of years, but that is not really a problem for us.
The calendar date to MATLAB time converter is datenum() which can have one of the three forms:
mtime=datenum(yr,mon,day) mtime=datenum(yr,mon,day,hr,min,s) mtime=datenum('15-May-1930')The first uses year, month and day, and assumes that the time is at the beginning of the day. The second line also includes hour, minute and second representing fractions of a day. Because mtime is represented by a double precision real number (15 significant digits), even fractions of a second can be distinguished in this time system.
datenum() allows a large variety of character representations of calendar dates, both on input and output. Look at help datenum to see these possibilities.
There are converters from MATLAB time to calendar dates of the following form:
[yr mon day hr min sec]=datevec(mtime); V=datevec(mtime);where the MATLAB time is converted into a string of numbers indicating the calendar time. All of the numbers that are returned are integers, except seconds which can have fractional values. Time precision is likely fractions of a millisec.
A second converter returns the calendar date/time as a string:
str=datestr(mtime); V=[yr mon day hr min sec]; str=datestr(V);You will notice that it is possible to convert among these three representations of time. datestr can return a large number of character formats. Use help datestr to see these details.
Here are some examples of these functions:
The flow chart for the task is
%%% get mtime for birthday %%% get mtime for today %%% calculate days old %%% calculate mtime for twice as old %%% get calendar date for that mtime
Solution script for task:
%%% get mtime for birthday Bday=datenum(1960,1,2); %%% get mtime for today Today=datenum(date); %%% calculate days old DaysOld=Today-Bday; disp(['I am now ' num2str(DaysOld) ' days old']); %%% calculate calendar date for twice as old disp(['I will be twice as old on ' datestr(Bday + 2*DaysOld)])