posixtime - Convert MATLAB datetime to POSIX time - MATLAB (original) (raw)

Main Content

Convert MATLAB datetime to POSIX time

Syntax

Description

p = posixtime([t](#bugbkkf-1%5Fsep%5Fshared-t)) returns POSIX® times equivalent to the datetime values int. The POSIX time is the number of seconds (including fractional seconds) elapsed since 00:00:00 1-Jan-1970 UTC (Coordinated Universal Time), ignoring leap seconds.p is a double array.

The best practice is to specify the time zone of t before calling posixtime.

example

Examples

collapse all

Create datetime values and convert them to the equivalent POSIX® times. Show the differences in POSIX times between zoned and unzoned datetime values. The best practice is to specify a time zone for a datetime array before calling posixtime.

Create a datetime array and specify its time zone.

t1 = datetime('2016-07-29 10:05:24') + calmonths(1:3); t1.TimeZone = 'America/New_York'

t1 = 1×3 datetime 29-Aug-2016 10:05:24 29-Sep-2016 10:05:24 29-Oct-2016 10:05:24

Convert t1 to the equivalent POSIX times. posixtime accounts for the time zone offset when it computes POSIX times.

format longG p1 = posixtime(t1)

p1 = 1×3

            1472479524                1475157924                1477749924

Create a datetime array with the same values as t1, but with no time zone. Convert it to the equivalent POSIX times. posixtime treats the times in t2 as UTC times, with no time zone offset.

t2 = datetime('2016-07-29 10:05:24') + calmonths(1:3); p2 = posixtime(t2)

p2 = 1×3

            1472465124                1475143524                1477735524

Show the differences between p2 and p1. The differences are equal to the time offset, in seconds, between UTC and the time zone of t1.

ans = 1×3

  -14400      -14400      -14400

Input Arguments

collapse all

Input date and time, specified as a datetime array.

Extended Capabilities

expand all

Theposixtime function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced in R2014b