将整数日期时间向量转换为Matlab持续时间。

huangapple go评论58阅读模式
英文:

Vector of integer datetimes to matlab durations

问题

你可以将代表自Unix纪元以微秒为单位的整数向量转换为适用于时间表的Matlab持续时间:

microseconds = [1679909400000000; 1679909500000000; 1679909600000000];
durations = microseconds / 1e6; % 转换为秒
timetable = array2timetable(durations, 'RowTimes', datetime(durations, 'ConvertFrom', 'datenum'));

这会将整数向量转换为Matlab持续时间,然后将其用于时间表。

英文:

How do I convert a vector of integers that represent time in microseconds since the unix epoch into matlab durations suitable for use in timetables?

datetimes = [1679909400000000;1679909500000000;1679909600000000]

答案1

得分: 1

你可以在datetime构造函数中使用ConvertFrom='posixtime'来实现这个,然后再除以1e6以从POSIX纪元中获取秒数。你可以通过传入值0来获得代表POSIX纪元的datetime

>> timePoints = datetime(datetimes/1e6, ConvertFrom = 'posixtime')

timePoints = 

  3x1 datetime array

   27-Mar-2023 09:30:00
   27-Mar-2023 09:31:40
   27-Mar-2023 09:33:20

>> timeSinceEpoch = timePoints - datetime(0, ConvertFrom = 'posixtime')

timeSinceEpoch = 

  3x1 duration array

   466641:30:00
   466641:31:40
   466641:33:20
英文:

You can use ConvertFrom='posixtime' in the datetime constructor to do this, after dividing by 1e6 to get seconds from the POSIX epoch. You can get a datetime representing the POSIX epoch by passing in the value 0.

>> timePoints =  datetime(datetimes/1e6, ConvertFrom = 'posixtime')

timePoints = 

  3x1 datetime array

   27-Mar-2023 09:30:00
   27-Mar-2023 09:31:40
   27-Mar-2023 09:33:20

>> timeSinceEpoch = timePoints - datetime(0, ConvertFrom = 'posixtime')

timeSinceEpoch = 

  3x1 duration array

   466641:30:00
   466641:31:40
   466641:33:20

huangapple
  • 本文由 发表于 2023年4月11日 06:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定