The problem is converting to strings instead of just comparing...
What will work to compute the offset including DST if in effect is--
v=now;
t1=datetime(v,'ConvertFrom','datenum','TimeZone','America/New_York','Format','d-MMM-y HH:mm:ss Z');
t2=datetime(v,'ConvertFrom','datenum','TimeZone','Africa/Khartoum','Format','d-MMM-y HH:mm:ss Z');
u=t2-t1
u =
duration
-7:00:00
The above sets the local time a variable before calling the conversion factors to ensure there isn't a rollover between the two calls to datetime that would introduce a small error in the result if were to occur.
(*) Interesting shortcoming that TMW hasn't introduced a datetime-aware routine to parallel the old standby that returns a datenum.
ADDENDUM
An alternative would be to use the timezones table data--
A=timezones('Africa');
N=timezones('America');
Z1=N(contains(N.Name,'New_York');
Z2=A(contains(A.Name,'Khartoum');
u=Z2.UTCOffset+Z2.DSTOffset-(Z1.UTCOffset+Z1.DSTOffset)
u =
7
The problem with this is that I don't believe the table will update the DTS offset with time of year; it's just the value used in the location. So, this is probably not viable actual solution lacking a test for whether DST is in effect in both locations at the time of interest.
Best Answer