Unable to read datetime with format "0600 UTC OCT 25"

When I import data from a kml file using readtable, it stores the date and time in a string array called Name formatted like this:
"1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"
In the latest round of my bout with datetime, I'm struggling to get this into a datetime variable.
I have tried this, but it doesn't work:
datimStorm = datetime( Name, 'InputFormat', 'hhmm ''UTC'' MM yy' );
Can this be done using the 'InputFormat', or do I have to do it myself with substrings?

1 Comment

Thanks to both of you. I made a mistake in cutting and pasting and included 'yy' where I was using 'dd'.
I got confused as well by the difference between "HH" and "hh", but the MATLAB documentation sorted me out.
The format does not include a year. How foolish is that?

Sign in to comment.

 Accepted Answer

That is almost correct. Use 'MMM' for months in that format, and 'HH for hours.
Try this —
Name = ["1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"];
datimStorm = datetime( Name, 'InputFormat', 'HHmm ''UTC'' MMM yy' )
datimStorm = 4×1 datetime array
01-Oct-2010 12:00:00 01-Oct-2010 18:00:00 01-Oct-2011 00:00:00 01-Oct-2011 06:00:00
.

4 Comments

Jon
Jon on 25 Oct 2023
Edited: Jon on 25 Oct 2023
@Star Strider thanks, I should have looked a little more carefully before I sent my answer off. Maybe there is some history to it that makes it more understandable, but for me the capitalization conventions for the time formats seem completely random. For example why are months capitalized by years aren't?
Are the 10 and 11 at the end of your times years (2010, 2011) or days of the month Oct 10, Oct 11? I think @Star Strider assumed they were years, as you end with yy, but was that what you intended?
If they are days, then you should use dd, as in my post, otherwise use yy.
@Jon For a definitive answer to your last question you'd have to ask the authors and reviewers of the Unicode Technical Standard (linked in the documentation for the Format property on the datetime array) whose symbols we (mostly) use. The table in the standard does list a Y symbol as an option for specifying years, but looking at its description I'm not sure how useful that would be for MATLAB which is why I'd guess we didn't implement it.
Two-digit years are certainly acceptable, and were the norm for a while, due to early computer memory and storage limitations. (This was the problem with the ‘Y2K’ concerns, in that years were stored as two digits for decades. Had the software not been updated and records not been corrected to four-digit years prior to the year 2000, the years would have jumped ahead by a century — 1999 would have become 2099 overnight — causing massive calculation errors.)
I was following the provided 'InputFormat' string, with appropriate changes to be certain that the months and hours were imported correctly, since that appeared to hsve been the issue.
EDIT — (25 Oct 2023 at 17:30)
One relatively important feature that I inadvertently omitted was to declare the 'TimeZone' as 'UTC' since that information was provided (also changing ‘yy’ to ‘dd’) —
Name = ["1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"];
datimStorm = datetime( Name, 'InputFormat', 'HHmm ''UTC'' MMM dd', 'TimeZone','UTC' )
datimStorm = 4×1 datetime array
10-Oct-2023 12:00:00 10-Oct-2023 18:00:00 11-Oct-2023 00:00:00 11-Oct-2023 06:00:00
LocalTime = datimStorm;
LocalTime.TimeZone = 'America/Denver'
LocalTime = 4×1 datetime array
10-Oct-2023 06:00:00 10-Oct-2023 12:00:00 10-Oct-2023 18:00:00 11-Oct-2023 00:00:00
You can then change the 'TimeZone' in a copied array (with a different name) as local time. The initial array will remain defined as UTC.
.

Sign in to comment.

More Answers (1)

t = "1200 UTC OCT 10"
t = "1200 UTC OCT 10"
td = datetime(t,'InputFormat','hhmm ''UTC'' MMM dd')
td = datetime
10-Oct-2023

2 Comments

Ooops should have been
t = "1200 UTC OCT 10"
t = "1200 UTC OCT 10"
td = datetime(t,'InputFormat','HHmm ''UTC'' MMM dd')
td = datetime
10-Oct-2023 12:00:00
Thanks, I had got confused by HH and hh. The format doesn't include a year, so dd is appropriate.

Sign in to comment.

Categories

Products

Release

R2023b

Tags

Asked:

on 25 Oct 2023

Edited:

on 25 Oct 2023

Community Treasure Hunt

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

Start Hunting!