Domanda

I'd like to add the variable Correction to the current time in order to get a new time.

Here is my code to get the current time (working):

datestr(now);
d = rem(now,1);
datestr(d);
time = datestr(d, 'HH:MM');

Here is my code on showing how to get the Correction factor (working):

c = clock();
tsNow = datenum(c);
tsStart = datenum([c(1) 1 1 0 0 0]); 
daysInYear = tsNow - tsStart;
DayOfYear = floor(daysInYear);

B = 360/365*(DayOfYear-81);
EoT = 9.87*sind(2*B)-7.53*cosd(B)-1.5*sind(B);
Correction = EoT - (4*(0-3.173));
Declination = 23.45*sind((360/365)*(284+DayOfYear));

How do I parse Correction so that it adds its value in HH:MM format to the current time? Simply putting

AST = time + Correction;

Prints AST as a vector with 6 values.

Note: HH:MM format is 24 hour format and Correction is usually adding between 0-60 minutes to the clock time so i'm not sure how it will deal with the remainder.

È stato utile?

Soluzione

By adding time which is of type string to Correction which is a number you won't get anything reasonable.

datenum can help you here. It can converts a date vector to date number. I am no expert on this issue. But I know that the data format the command now returns is called a date number. (See here for other functions and see their input and output types)

However in your case it's easier to deal with date vector representation of time:

A full date vector has six elements, specifying year, month, day, hour, minute, and second, in that order. .... Example: [2003,10,24,12,45,07]

So, you can convert the Correction time to date number using datenum and then add it to value acquired from now (here d):

datestr(d + datenum([0 0 0 0 0 Correction]))

This is considering Correction to be in seconds.

Hope it helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top