Universal Time to Local Time conversion

Hello
I have an array of univeral time in decimal hours.
I want to compute local time using this equation: LT = t1 + hours(loni/15); where t1 is univeral time and Loni is geographic longitude. But thereuslting LT values are from 00:00 to 12:00 only . I was expecting the usual range 00:00:00 to 23:59:59.
Any suggestions?
Thank you

 Accepted Answer

If LT is datetime() then you need to adjust the Format property of LT
I recently encountered someone who was doing a similar calculation to get some kind of solar magnetic time. In that case I showed that for accurate conversion for that purpose, you needed magnetic readings not just longitude / 15.
If you are not doing solar magnetic calculations, then the better way to convert is to use a datetime object with attached UTC time zone, and then set the TimeZone property of the object according to the time zone of the location. For example local time in Arizona is Mountain Standard Time all year, except within Navajo Nation, but other usa states with the same longitude observe Daylight Savings Time. Given an accurate time zone name, datetime can produce accurate local times including savings time adjustments that differ during the year.
Note: if you have a datetime object that has no TimeZone set and you set the time zone for it, then datetime will assume the time was the correct local time for that time zone. To have it adjust from UTC you must first set the time zone as UTC. Once there has been some time zone set for a datetime object, datetime will adjust the display as you change the time zone property.

4 Comments

I am t1 is the stored UCT data in hh:mm:ss fomrat and i am using this code to do the conversion:
% Create a datetime object with the UTC time and specify the time zone as UTC
utcDateTime = datetime(t1, 'TimeZone', 'UTC');
% Determine the time zone offset based on the longitude
timeZoneOffset = loni/15; % Calculate the time zone offset (in hours)
% Convert the UTC time to local time
LT = utcDateTime + hours(timeZoneOffset);
t1 = ["05:17:23", "19:04:38"]
t1 = 1×2 string array
"05:17:23" "19:04:38"
loni = [-97.1385, 54.3773] %winnipeg, Abu Dhabi
loni = 1×2
-97.1385 54.3773
%
utcDateTime = datetime(t1, 'TimeZone', 'UTC')
utcDateTime = 1×2 datetime array
26-May-2023 05:17:23 26-May-2023 19:04:38
Is it really the case that you want the times to be converted as if they are "today" relative to the execution of the code? It would be more robust if you attached them to a particular date
timeZoneOffset = ceil(loni/15)
timeZoneOffset = 1×2
-6 4
LT = arrayfun(@(DT,TZ) datetime(DT, 'TimeZone', compose("UTC%+d",TZ)), utcDateTime, timeZoneOffset,'uniform', 0)
LT = 1×2 cell array
{[25-May-2023 23:17:23]} {[26-May-2023 23:04:38]}
LT{1}.TimeZone
ans = '-06:00'
LT{2}.TimeZone
ans = '+04:00'
Those are correct Standard Time offsets for Winnipeg and for Abu Dhabi. But Winnipeg (all of the province of Manitoba) uses Central Daylight Time, so local time here is currently UTC-5 not UTC-6 .
Time zones get a bit confusing for small parts of the next province west, Saskatchewan; https://www.timeanddate.com/time/zone/canada/saskatchewan -- see for example Creighton . And see https://www.theglobeandmail.com/opinion/why-the-zigzag-between-sask-and-man/article4202831/ for why the boundary between Manitoba and Saskatchewan is complicated -- you would need a more detailed calculation based on latitude as well to determine from longitude which side of the border you are on (and therefore which timezone you are in.)
This makes more sense and I am accepting your answer!
Thank you Walter!
Basically, "local time" turns out to be more political than geographic.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!