Convert Epoch Date to Serial Date

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert Epoch Date to Serial Date

Fritz Sonnichsen
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



Reply | Threaded
Open this post in threaded view
|

RE: Convert Epoch Date to Serial Date

Tony Richardson
> 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


Reply | Threaded
Open this post in threaded view
|

Re: Convert Epoch Date to Serial Date

Fritz Sonnichsen
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
>



Reply | Threaded
Open this post in threaded view
|

Re: Convert Epoch Date to Serial Date

apjanke-floss


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


Reply | Threaded
Open this post in threaded view
|

RE: Convert Epoch Date to Serial Date

Bård Skaflestad
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 <help-octave-bounces+bard.skaflestad=[hidden email]> 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
>




Reply | Threaded
Open this post in threaded view
|

Re: Convert Epoch Date to Serial Date

Andreas Weber-6
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


Reply | Threaded
Open this post in threaded view
|

Re: Convert Epoch Date to Serial Date

Fritz Sonnichsen
Thanks. That diagram is delightful and worth a thousand words (or lines
of code!)

Fritz

On 4/11/2019 5:59 AM, Andreas Weber wrote:
> 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
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Convert Epoch Date to Serial Date

Andreas Weber-6
 > On 4/11/2019 5:59 AM, Andreas Weber wrote:
 >> https://wiki.octave.org/Date/Time_functions

Am 11.04.19 um 15:52 schrieb Fritz Sonnichsen:
> Thanks. That diagram is delightful and worth a thousand words (or lines
> of code!)

Thank you. Can you help improving it with some suggestions?

it's a SVG so you can download it and start modify it.

-- Andy