help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem processing data logs


From: Andrew Janke
Subject: Re: Problem processing data logs
Date: Thu, 3 Jan 2019 17:37:29 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Thunderbird/60.3.3


On 1/3/19 4:44 PM, Mike Miller wrote:
On Thu, Jan 03, 2019 at 13:25:23 +0000, Ian McCallion wrote:
Is there another way to implement  mylocaltime()  in Octave without
recoding from scratch the logic needed to process a timezone string?

If setting and unsetting the TZ environment variable is a big problem on
Windows, I can't think of another way to do it readily in Octave at the
moment.

However, you might be able to rely on an external program which has a
separate environment space. The following example works for me (on
GNU/Linux).

     function lt = mylocaltime (seconds, tz)
       fmt = "%S %M %H %d %m %Y";
       cmd = sprintf ("env TZ='%s' date address@hidden +'%s'", tz, seconds, 
fmt);
       [status, output] = system (cmd);
       values = str2double (strsplit (output));
       lt.sec = values(1);
       lt.min = values(2);
       lt.hour = values(3);
       lt.mday = values(4);
       lt.mon = values(5);
       lt.year = values(6);
     endfunction

You can tailor this to whichever broken-down time fields you need to
extract.


If your Octave is compiled with Java support, you can do it using Java objects. The new java.time API has good support for time zone calculations. That would be faster than shelling out to an external program and parsing results for each date, and portable too.


function out = utc_to_local_time(time, tz)
  % Convert a date in UTC to an arbitrary time zone
  % tz is a string holding the time zone name in "Olson database" format

% In production code, you should cache all these parser values in the fields
  % of an Octave object
  format = 'yyyy-MM-dd kk:mm:ss[.SSS]';
  javaTzUtc = javaMethod('of', 'java.time.ZoneId', 'UTC');
  javaTz = javaMethod('of', 'java.time.ZoneId', tz);
parser = javaMethod('ofPattern', 'java.time.format.DateTimeFormatter', format);
  javaDate = javaMethod('parse', 'java.time.LocalDateTime', time, parser);
  javaDateUtc = javaDate.atZone(javaTzUtc);
  javaDateLocal = javaDateUtc.withZoneSameInstant(javaTz);

% This conversion is kinda inefficient. You could speed it up by grabbing the Unix % timestamp value from javaDate and converting that directly to a datenum. Better % yet, vectorize it. I'm just too lazy to work out the math to do the epoch
  % conversion right now.
  jd = javaDateLocal;
dnum = datenum(double(jd.getYear()), double(jd.getMonthValue()), double(jd.getDayOfMonth()), ...
    double(jd.getHour()), double(jd.getMinute()), double(jd.getSecond()));
  nanos = double(jd.getNano());
  nanosPerDay = (10^9) * 60 * 60 * 24;
  dnum = dnum + (nanos / nanosPerDay);
  out = dnum;
end


Tested on Octave 4.4.1 with Java 1.8.

>> d = utc_to_local_time('2011-12-14 12:34:56', 'America/New_York')
d =  734851.31593
>> datestr(d)
ans = 14-Dec-2011 07:34:56

Cheers,
Andrew



reply via email to

[Prev in Thread] Current Thread [Next in Thread]