PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/farmR/src/glplib10.c

https://code.google.com/p/javawfm/
C | 120 lines | 74 code | 8 blank | 38 comment | 4 complexity | 9399b0cfd2df1ac7edace22637ad7da3 MD5 | raw file
  1/* glplib10.c (standard time) */
  2
  3/***********************************************************************
  4*  This code is part of GLPK (GNU Linear Programming Kit).
  5*
  6*  Copyright (C) 2000,01,02,03,04,05,06,07,08,2009 Andrew Makhorin,
  7*  Department for Applied Informatics, Moscow Aviation Institute,
  8*  Moscow, Russia. All rights reserved. E-mail: <mao@mai2.rcnet.ru>.
  9*
 10*  GLPK is free software: you can redistribute it and/or modify it
 11*  under the terms of the GNU General Public License as published by
 12*  the Free Software Foundation, either version 3 of the License, or
 13*  (at your option) any later version.
 14*
 15*  GLPK is distributed in the hope that it will be useful, but WITHOUT
 16*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 17*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 18*  License for more details.
 19*
 20*  You should have received a copy of the GNU General Public License
 21*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
 22***********************************************************************/
 23
 24#define _GLPSTD_TIME
 25#include "glplib.h"
 26
 27/* platform-independent ISO C version */
 28
 29/***********************************************************************
 30*  NAME
 31*
 32*  xtime - determine the current universal time
 33*
 34*  SYNOPSIS
 35*
 36*  #include "glplib.h"
 37*  xlong_t xtime(void);
 38*
 39*  RETURNS
 40*
 41*  The routine xtime returns the current universal time (UTC), in
 42*  milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
 43
 44#if 0
 45xlong_t xtime(void)
 46{     static const int epoch = 2440588; /* jday(1, 1, 1970) */
 47      time_t timer;
 48      struct tm *tm;
 49      xlong_t t;
 50      int j;
 51      timer = time(NULL);
 52      tm = gmtime(&timer);
 53      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
 54      xassert(j >= 0);
 55      t = xlset(j - epoch);
 56      t = xlmul(t, xlset(24));
 57      t = xladd(t, xlset(tm->tm_hour));
 58      t = xlmul(t, xlset(60));
 59      t = xladd(t, xlset(tm->tm_min));
 60      t = xlmul(t, xlset(60));
 61      t = xladd(t, xlset(tm->tm_sec));
 62      t = xlmul(t, xlset(1000));
 63      return t;
 64}
 65#else
 66static xlong_t zeit(void)
 67{     static const int epoch = 2440588; /* jday(1, 1, 1970) */
 68      time_t timer;
 69      struct tm *tm;
 70      xlong_t t;
 71      int j;
 72      timer = time(NULL);
 73      tm = gmtime(&timer);
 74      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
 75      xassert(j >= 0);
 76      t = xlset(j - epoch);
 77      t = xlmul(t, xlset(24));
 78      t = xladd(t, xlset(tm->tm_hour));
 79      t = xlmul(t, xlset(60));
 80      t = xladd(t, xlset(tm->tm_min));
 81      t = xlmul(t, xlset(60));
 82      t = xladd(t, xlset(tm->tm_sec));
 83      t = xlmul(t, xlset(1000));
 84      return t;
 85}
 86
 87xlong_t xtime(void)
 88{     LIBENV *env = lib_link_env();
 89      xlong_t t;
 90      clock_t c;
 91      double secs;
 92      xassert(sizeof(clock_t) <= sizeof(env->c_init));
 93      t = zeit();
 94      if (xlcmp(xlsub(t, env->t_init), xlset(600 * 1000)) <= 0)
 95      {  /* not more than ten minutes since the last call */
 96         memcpy(&c, env->c_init, sizeof(clock_t));
 97         secs = (double)(clock() - c) / (double)CLOCKS_PER_SEC;
 98         if (0.0 <= secs && secs <= 1000.0)
 99         {  /* looks like correct value */
100            t = xladd(env->t_init, xlset((int)(1000.0 * secs + 0.5)));
101            goto done;
102         }
103      }
104      /* re-initialize */
105      if (xlcmp(t, env->t_last) < 0) t = env->t_last;
106      env->t_init = t;
107      c = clock();
108      memcpy(env->c_init, &c, sizeof(clock_t));
109done: xassert(xlcmp(env->t_last, t) <= 0);
110      env->t_last = t;
111      return t;
112}
113#endif
114
115double xdifftime(xlong_t t1, xlong_t t0)
116{     /* compute the difference between two time values, in seconds */
117      return xltod(xlsub(t1, t0)) / 1000.0;
118}
119
120/* eof */