Why doesn't timezone work with datetime

I want to convert a posix time to a datetime for the time zone at a specific longitude. The timezone function gives you the time zone at a specific longitude, but none of the outputs are accepted by datetime.
[a,b,c] = timezone(-80)
a = 5
b = 'R'
c = '+5 R'
try
datetime(1685585049.879,'ConvertFrom','posixtime','TimeZone',a)
end
try
datetime(1685585049.879,'ConvertFrom','posixtime','TimeZone',b)
end
try
datetime(1685585049.879,'ConvertFrom','posixtime','TimeZone',c)
end

 採用された回答

Star Strider
Star Strider 2023 年 8 月 24 日

1 投票

The timezone function is from the Mapping Toolbox, and its results are not compatible with the datetime function. The otherwise compatible timezones function does not take longitude arguments.
Try this instead —
TZ = string(fix(-80/15)) % Time Zone (-80° Longitude)
TZ = "-5"
Time = datetime(1685585049.879,'ConvertFrom','posixtime','TimeZone',TZ)
Time = datetime
31-May-2023 21:04:09
.

その他の回答 (1 件)

Seth Furman
Seth Furman 2023 年 9 月 14 日
編集済み: Seth Furman 2023 年 9 月 14 日

1 投票

Adding to @Star Strider's answer:
The value of TimeZone can be an ISO 8601 character vector of the form +HH:mm or -HH:mm; for example, '+01:00', to specify a time zone that is a fixed offset from UTC.
lon = 80;
zd = timezone(lon)
zd = -5
tz = compose("%+03d:00",zd)
tz = "-05:00"
dt = datetime(1685585049.879,ConvertFrom="posixtime",TimeZone=tz)
dt = datetime
31-May-2023 21:04:09

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2023a

質問済み:

AB
2023 年 8 月 24 日

編集済み:

2023 年 9 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by