"Fossies" - the Fresh Open Source Software Archive

Member "dateutils-0.4.6/contrib/tzconv.c" (19 Mar 2019, 3346 Bytes) of package /linux/privat/dateutils-0.4.6.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tzconv.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.4.5_vs_0.4.6.

    1 /*** tzconv.c -- convert matlab dates between timezones
    2  *
    3  * Copyright (C) 2013-2019 Sebastian Freundt
    4  *
    5  * Author:  Sebastian Freundt <freundt@ga-group.nl>
    6  *
    7  * This file is part of dateutils.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  *
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  *
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * 3. Neither the name of the author nor the names of any contributors
   21  *    may be used to endorse or promote products derived from this
   22  *    software without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
   25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   27  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
   34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  *
   36  ***/
   37 #if defined HAVE_CONFIG_H
   38 # include "config.h"
   39 #endif  /* HAVE_CONFIG_H */
   40 #include <stdlib.h>
   41 #include <stdint.h>
   42 #include <math.h>
   43 /* matlab stuff */
   44 #if defined HAVE_OCTAVE_MEX_H
   45 # include <octave/mex.h>
   46 #else  /* !HAVE_OCTAVE_MEX_H */
   47 # include <mex.h>
   48 #endif  /* HAVE_OCTAVE_MEX_H */
   49 /* our stuff */
   50 #include "tzraw.h"
   51 
   52 /* see tzconv.m for details */
   53 
   54 static zif_t
   55 find_zone(const mxArray *zstr)
   56 {
   57     char *zone = mxArrayToString(zstr);
   58     zif_t res;
   59 
   60     res = zif_open(zone);
   61     if (zone != NULL) {
   62         mxFree(zone);
   63     }
   64     return res;
   65 }
   66 
   67 
   68 void
   69 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
   70 {
   71     zif_t fromz;
   72     zif_t toz;
   73 
   74     if (nrhs != 3 || nlhs > 1) {
   75         mexErrMsgTxt("invalid usage, see `help tzconv'\n");
   76         return;
   77     }
   78 
   79     /* find zones */
   80     if ((fromz = find_zone(prhs[1])) == NULL) {
   81         mexErrMsgTxt("cannot open from zone\n");
   82     }
   83     if ((toz = find_zone(prhs[2])) == NULL) {
   84         zif_close(fromz);
   85         mexErrMsgTxt("cannot open target zone\n");
   86     }
   87 
   88 #define TO_UNIX(x)  ((x) - 719529.0) * 86400.0
   89 #define TO_MATL(x)  ((x) / 86400.0) + 719529.0
   90     {
   91         mwSize m = mxGetM(prhs[0]);
   92         mwSize n = mxGetN(prhs[0]);
   93         const double *src = mxGetPr(prhs[0]);
   94         double *tgt;
   95 
   96         plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
   97         tgt = mxGetPr(plhs[0]);
   98 
   99         for (mwSize i = 0; i < m * n; i++) {
  100             double x = TO_UNIX(src[i]);
  101 
  102             if (x < 2147483647.0 && x > -2147483648.0) {
  103                 double frac = modf(x, &x);
  104                 int32_t utc = zif_utc_time(fromz, (int32_t)x);
  105                 int32_t lcl = zif_local_time(toz, utc);
  106 
  107                 tgt[i] = TO_MATL((double)lcl) + frac / 86400.0;
  108             } else {
  109                 tgt[i] = NAN;
  110             }
  111         }
  112     }
  113 
  114     zif_close(fromz);
  115     zif_close(toz);
  116     return;
  117 }
  118 
  119 /* gand_get_series.c ends here */