Date and times logic

7 ビュー (過去 30 日間)
Adnan Jayyousi
Adnan Jayyousi 2022 年 6 月 24 日
回答済み: Steven Lord 2022 年 6 月 25 日
Hello,
I am trying to make array that contains 8760 Hours, starting from 1/1/2021 ending in 31/12/2021,
in the second column in the same array I want to fill in the corresponding electricty tariff at that certain time.
the tariff values may vary according to the month/days/hours/holidays along the year.
My intention is to make a logic using while loop (8760 loops) using index "i".
I am having trouble to find easy logic that can be applied on the "DateNtime" array in order to fill the tariff.
I will give an example for one scenario in words :
If Season is 1 (one of months Jan/Feb/Dec) & the hour is between 8:00 and 13:00 & the day is normal week day (Sunday till thursday), the tariff wil be 0.7170 (put into column 2 -- > DateNtime(x,2) = 0.7170
any suggestions ?
%% Define date&time arrays :
t1 = datetime(2021,1,1,0,0,0);
t2 = datetime(2021,12,31,23,0,0);
DateNtime = (t1:hours(1):t2)'
%% Create Holidays arrays :
ht1 = datetime(2021,3,27,0,0,0);
ht2 = datetime(2021,3,28,23,0,0);
HT1 = (ht1:hours(1):ht2)'
%%
ht3 = datetime(2021,4,2,0,0,0);
ht4 = datetime(2021,4,3,23,0,0);
HT2 = (ht3:hours(1):ht4)'
%%
ht5 = datetime(2021,4,14,0,0,0);
ht6 = datetime(2021,4,15,23,0,0);
HT3 = (ht5:hours(1):ht6)'
%%
ht7 = datetime(2021,4,14,0,0,0);
ht8 = datetime(2021,4,15,23,0,0);
HT4 = (ht7:hours(1):ht8)'
%%
ht9 = datetime(2021,5,16,0,0,0);
ht10 = datetime(2021,5,17,23,0,0);
HT5 = (ht9:hours(1):ht10)'
%%
ht11 = datetime(2021,9,6,0,0,0);
ht12 = datetime(2021,9,7,23,0,0);
HT6 = (ht11:hours(1):ht12)'
%%
ht13 = datetime(2021,9,15,0,0,0);
ht14 = datetime(2021,9,16,23,0,0);
HT7 = (ht13:hours(1):ht14)'
%%
ht15 = datetime(2021,9,20,0,0,0);
ht16 = datetime(2021,9,20,23,0,0);
HT8 = (ht15:hours(1):ht16)'
%%
ht17 = datetime(2021,9,27,0,0,0);
ht18 = datetime(2021,9,27,23,0,0);
HT9 = (ht17:hours(1):ht18)'
%%
%% Holidays Array :
Hdays = [HT1 ; HT2 ; HT3 ; HT4 ; HT5 ; HT6 ; HT7 ; HT8 ; HT9];
%% Start Loop for filling tariffs :
i = 1:1:8760;
while ( i < 8760 )
????
end
%% Create tariff table according to seasons (season 1 (Dec,Feb,Jan), Season 2 (March,april,may,october,november),season 3(june,july,august,september) :
Season1Tariffs = [0.7170,0.2307];
array2table(Season1Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Season2Tariffs = [0.2578,0.2243];
array2table(Season2Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Season3Tariffs = [1.0789,0.2701];
array2table(Season3Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Thanks !
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 6 月 25 日
Note - Your Hdays array doesn't have 8760 elements.
Use a for loop -
%random date and time array
dt=[datetime(2021,3,29,10,20,30) datetime(2021,6,15,12,10,8) datetime(2021,12,1,13,17,19)]
dt = 1×3 datetime array
29-Mar-2021 10:20:30 15-Jun-2021 12:10:08 01-Dec-2021 13:17:19
DateNtime=zeros(numel(dt),2); %preallocating
for i=1:numel(dt)
if hour(dt(i))>=8 & hour(dt(i))<13 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2}
DateNtime(i,2)=0.7170;
case {3,4,5,10,11}
DateNtime(i,2)=0.2578;
case {6,7,8,9}
DateNtime(i,2)=1.0789;
end
end
end
DateNtime
DateNtime = 3×2
0 0.2578 0 1.0789 0 0
Adnan Jayyousi
Adnan Jayyousi 2022 年 6 月 25 日
Thanks Dyuman,
That helped me alot,
I have implemented this and have added more logical data, i've a feeling that i haven't done it in efficient way...but works.
%% Define date&time arrays :
t1 = datetime(2021,1,1,0,0,0);
t2 = datetime(2021,12,31,23,0,0);
dt = (t1:hours(1):t2)'
%holidyas date array
Holidays = [datetime(2021,3,27,0,0,0) datetime(2021,3,28,0,0,0) datetime(2021,4,2,0,0,0) datetime(2021,4,3,0,0,0) datetime(2021,4,14,0,0,0) datetime(2021,4,15,0,0,0) datetime(2021,5,16,0,0,0) datetime(2021,5,17,0,0,0) datetime(2021,9,6,0,0,0) datetime(2021,9,7,0,0,0) datetime(2021,9,15,0,0,0) datetime(2021,9,16,0,0,0) datetime(2021,9,20,0,0,0) datetime(2021,9,27,0,0,0)]
Holidays = Holidays'
DateNtime=zeros(numel(dt),2); %preallocating
%% Hours between 00:00 - 16:00 "Off Peak" - Days of week. (All Seasons)
for i=1:numel(dt)
if hour(dt(i))>=0 & hour(dt(i))<=16 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% Hours between 22:00 - 23:00 "Off Peak" - Days of week. (Season#1 only)
for i=1:numel(dt)
if hour(dt(i))>=22 & hour(dt(i))<=23 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
end
end
end
%% Hour 23:00 "Off Peak" - Days of week. (Season#2 & Season #3)
for i=1:numel(dt)
if hour(dt(i))== 23 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% Weekends. (All seasons)
for i=1:numel(dt)
if ismember(day(dt(i),'dayofweek'),[6 7])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% On Peak - Days of week only (All Seasons)
for i=1:numel(dt)
if hour(dt(i))>=17 & hour(dt(i))<=21 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2578;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=1.0789;
end
end
end
%% Hour 22:00 "On Peak" - Days of week. (Season#2 & Season #3)
for i=1:numel(dt)
if hour(dt(i)) == 22 ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2578;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=1.0789;
end
end
end
%% Holidays
doy = day (Holidays(:,:),'dayofyear')
for i=1:numel(dt)
if ismember(day(dt(i),'dayofyear'),doy)
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
DateNtime

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2022 年 6 月 25 日
Let's say you had some sample dates with time components.
rng default
d = datetime(2022, randi(12, 10, 1), randi(31, 10, 1), randi(24, 10, 1), randi([0 59], 10, 1), 0)
d = 10×1 datetime array
05-Oct-2022 16:42:00 01-Dec-2022 01:01:00 02-Mar-2022 21:16:00 16-Nov-2022 23:02:00 25-Aug-2022 17:05:00 05-Feb-2022 19:49:00 14-Apr-2022 18:41:00 29-Jul-2022 10:19:00 25-Dec-2022 16:57:00 30-Dec-2022 05:02:00
And you had a vector of holidays with the time component set to midnight of that day.
holidays = dateshift(d([2 7 4]), 'start', 'day')
holidays = 3×1 datetime array
01-Dec-2022 14-Apr-2022 16-Nov-2022
We can see which of the elements in d fall on the same days as those in holidays.
isaHoliday = ismember(day(d, 'dayofyear'), day(holidays, 'dayofyear'))
isaHoliday = 10×1 logical array
0 1 0 1 0 0 1 0 0 0
Or if you don't want to use day you could use dateshift to make a copy of d with the time components set to midnight.
isaHoliday2 = ismember(dateshift(d, 'start', 'day'), holidays)
isaHoliday2 = 10×1 logical array
0 1 0 1 0 0 1 0 0 0
If you need to consider only part of a day a holiday (say if you work for a company that closes at noon on the day before Thanksgiving in the US) you could use timeofday to extract the time components of d and use relational operators.
t = timeofday(d)
t = 10×1 duration array
16:42:00 01:01:00 21:16:00 23:02:00 17:05:00 19:49:00 18:41:00 10:19:00 16:57:00 05:02:00
isAfternoon = t > duration(12, 0, 0)
isAfternoon = 10×1 logical array
1 0 1 1 1 1 1 0 1 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by