# Convert Epoch Date to Serial Date

8 messages
Open this post in threaded view
|

## Convert Epoch Date to Serial Date

 I've looked thru the Octave time utilities and could not find a routine for this purpose. Is there a simple function that receives the epoch-date and converts it to a serial-date. (I need to feed the latter to the datetick routine) Thanks Fritz
Open this post in threaded view
|

## RE: Convert Epoch Date to Serial Date

 > Subject: Convert Epoch Date to Serial Date > > I've looked thru the Octave time utilities and could not find a routine for this > purpose. Is there a simple function that receives the epoch-date and > converts it to a serial-date. (I need to feed the latter to the datetick routine) > > Thanks > Fritz I believe localtime will return a structure that you can pull apart and feed to datenum to get a serial date: >> localtime(time()) ans =   scalar structure containing the fields:     usec =  839628     sec =  31     min =  32     hour =  13     mday =  10     mon =  3     year =  119     wday =  3     yday =  99     isdst =  1     gmtoff = 0     zone = Central Daylight Time Tony Richardson
Open this post in threaded view
|

## Re: Convert Epoch Date to Serial Date

 Thanks Anthony.    That seems to work after some code like this.      t=localtime(epoch_date) bf=sprintf("%d/%d/%d,%d:%d:%d",t.year+1900,t.mon+1,t.mday,t.hour,t.min,t.sec)      DTformat='yyyy/mm/dd,HH:MM:SS';      datenum(bf,DTformat)      numerical_date = datenum(bf,DTformat); %days from January 0, 0000 after some thought the same result can be obtained as follows;      numerical_date=  dtetmenum(1)/86400 + datenum(1970,1,1) would be nice if this was included as an "epoch2serial" utility just to keep make it easy to remember! cheers Fritz On 4/10/2019 2:33 PM, Richardson, Anthony wrote: >> Subject: Convert Epoch Date to Serial Date >> >> I've looked thru the Octave time utilities and could not find a routine for this >> purpose. Is there a simple function that receives the epoch-date and >> converts it to a serial-date. (I need to feed the latter to the datetick routine) >> >> Thanks >> Fritz > I believe localtime will return a structure that you can pull apart and feed to datenum to get a serial date: > >>> localtime(time()) > ans = > >    scalar structure containing the fields: > >      usec =  839628 >      sec =  31 >      min =  32 >      hour =  13 >      mday =  10 >      mon =  3 >      year =  119 >      wday =  3 >      yday =  99 >      isdst =  1 >      gmtoff = 0 >      zone = Central Daylight Time > > > Tony Richardson >
Open this post in threaded view
|

## Re: Convert Epoch Date to Serial Date

 On 4/10/19 3:04 PM, Fritz Sonnichsen wrote: > Thanks Anthony. >    That seems to work after some code like this. >      t=localtime(epoch_date) > bf=sprintf("%d/%d/%d,%d:%d:%d",t.year+1900,t.mon+1,t.mday,t.hour,t.min,t.sec) > >      DTformat='yyyy/mm/dd,HH:MM:SS'; >      datenum(bf,DTformat) >      numerical_date = datenum(bf,DTformat); %days from January 0, 0000 > > after some thought the same result can be obtained as follows; >      numerical_date=  dtetmenum(1)/86400 + datenum(1970,1,1) > > would be nice if this was included as an "epoch2serial" utility just to > keep make it easy to remember! > > cheers > Fritz If Octave gets support for the @datetime class, this will become readily available: there's a `datetime(x, 'ConvertFrom','posixtime')` option that will do this conversion. Cheers, Andrew
Open this post in threaded view
|

## RE: Convert Epoch Date to Serial Date

 In reply to this post by Fritz Sonnichsen If you're willing to ignore leap seconds and either don't need or can perform your own time-zone conversions from UTC to local time, you can get pretty far by exploiting the fact that 'datenum' does proper duration handling.  You can pass your epoch value as the sixth column in an argument to 'datenum' and add datenum([1970, 0, 0]) to the result:     epoch = 2147483647;  # Some epoch value     datevec(datenum([1970,0,0]) + datenum([zeros(10,5), epoch + (0:9).']))     ans =        2038      1     18      3     14      7        2038      1     18      3     14      8        2038      1     18      3     14      9        2038      1     18      3     14     10        2038      1     18      3     14     11        2038      1     18      3     14     12        2038      1     18      3     14     13        2038      1     18      3     14     14        2038      1     18      3     14     15        2038      1     18      3     14     16 This is just a small extension of the method suggested below. Regards, Bård Skaflestad SINTEF Digital, Mathematics & Cybernetics Compuational Geosciences group -----Original Message----- From: Help-octave On Behalf Of Fritz Sonnichsen Sent: Wednesday, April 10, 2019 9:04 PM To: Richardson, Anthony <[hidden email]>; Help GNU Octave <[hidden email]> Subject: Re: Convert Epoch Date to Serial Date Thanks Anthony.    That seems to work after some code like this.      t=localtime(epoch_date) bf=sprintf("%d/%d/%d,%d:%d:%d",t.year+1900,t.mon+1,t.mday,t.hour,t.min,t.sec)      DTformat='yyyy/mm/dd,HH:MM:SS';      datenum(bf,DTformat)      numerical_date = datenum(bf,DTformat); %days from January 0, 0000 after some thought the same result can be obtained as follows;      numerical_date=  dtetmenum(1)/86400 + datenum(1970,1,1) would be nice if this was included as an "epoch2serial" utility just to keep make it easy to remember! cheers Fritz On 4/10/2019 2:33 PM, Richardson, Anthony wrote: >> Subject: Convert Epoch Date to Serial Date >> >> I've looked thru the Octave time utilities and could not find a >> routine for this purpose. Is there a simple function that receives >> the epoch-date and converts it to a serial-date. (I need to feed the >> latter to the datetick routine) >> >> Thanks >> Fritz > I believe localtime will return a structure that you can pull apart and feed to datenum to get a serial date: > >>> localtime(time()) > ans = > >    scalar structure containing the fields: > >      usec =  839628 >      sec =  31 >      min =  32 >      hour =  13 >      mday =  10 >      mon =  3 >      year =  119 >      wday =  3 >      yday =  99 >      isdst =  1 >      gmtoff = 0 >      zone = Central Daylight Time > > > Tony Richardson >
Open this post in threaded view
|

## Re: Convert Epoch Date to Serial Date

 In reply to this post by Fritz Sonnichsen Am 10.04.19 um 19:26 schrieb Fritz Sonnichsen: > I've looked thru the Octave time utilities and could not find a routine > for this purpose. Have you found this one? https://wiki.octave.org/Date/Time_functions-- Andy