[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Convert Epoch Date to Serial Date
From: |
Bård Skaflestad |
Subject: |
RE: Convert Epoch Date to Serial Date |
Date: |
Thu, 11 Apr 2019 07:54:56 +0000 |
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 <address@hidden> On Behalf Of Fritz Sonnichsen
Sent: Wednesday, April 10, 2019 9:04 PM
To: Richardson, Anthony <address@hidden>; Help GNU Octave <address@hidden>
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
>