src/glpenv06.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glpenv06.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* glpenv06.c (standard time) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#ifdef HAVE_CONFIG_H
    1.29 +#include <config.h>
    1.30 +#endif
    1.31 +
    1.32 +#include "glpapi.h"
    1.33 +
    1.34 +/***********************************************************************
    1.35 +*  NAME
    1.36 +*
    1.37 +*  glp_time - determine current universal time
    1.38 +*
    1.39 +*  SYNOPSIS
    1.40 +*
    1.41 +*  glp_long glp_time(void);
    1.42 +*
    1.43 +*  RETURNS
    1.44 +*
    1.45 +*  The routine glp_time returns the current universal time (UTC), in
    1.46 +*  milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
    1.47 +
    1.48 +static const int epoch = 2440588; /* = jday(1, 1, 1970) */
    1.49 +
    1.50 +/* POSIX version ******************************************************/
    1.51 +
    1.52 +#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
    1.53 +
    1.54 +#include <sys/time.h>
    1.55 +#include <time.h>
    1.56 +
    1.57 +glp_long glp_time(void)
    1.58 +{     struct timeval tv;
    1.59 +      struct tm *tm;
    1.60 +      glp_long t;
    1.61 +      int j;
    1.62 +      gettimeofday(&tv, NULL);
    1.63 +      tm = gmtime(&tv.tv_sec);
    1.64 +      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
    1.65 +      xassert(j >= 0);
    1.66 +      t = xlset(j - epoch);
    1.67 +      t = xlmul(t, xlset(24));
    1.68 +      t = xladd(t, xlset(tm->tm_hour));
    1.69 +      t = xlmul(t, xlset(60));
    1.70 +      t = xladd(t, xlset(tm->tm_min));
    1.71 +      t = xlmul(t, xlset(60));
    1.72 +      t = xladd(t, xlset(tm->tm_sec));
    1.73 +      t = xlmul(t, xlset(1000));
    1.74 +      t = xladd(t, xlset(tv.tv_usec / 1000));
    1.75 +      return t;
    1.76 +}
    1.77 +
    1.78 +/* Windows version ****************************************************/
    1.79 +
    1.80 +#elif defined(__WOE__)
    1.81 +
    1.82 +#include <windows.h>
    1.83 +
    1.84 +glp_long glp_time(void)
    1.85 +{     SYSTEMTIME st;
    1.86 +      glp_long t;
    1.87 +      int j;
    1.88 +      GetSystemTime(&st);
    1.89 +      j = jday(st.wDay, st.wMonth, st.wYear);
    1.90 +      xassert(j >= 0);
    1.91 +      t = xlset(j - epoch);
    1.92 +      t = xlmul(t, xlset(24));
    1.93 +      t = xladd(t, xlset(st.wHour));
    1.94 +      t = xlmul(t, xlset(60));
    1.95 +      t = xladd(t, xlset(st.wMinute));
    1.96 +      t = xlmul(t, xlset(60));
    1.97 +      t = xladd(t, xlset(st.wSecond));
    1.98 +      t = xlmul(t, xlset(1000));
    1.99 +      t = xladd(t, xlset(st.wMilliseconds));
   1.100 +      return t;
   1.101 +}
   1.102 +
   1.103 +/* portable ISO C version *********************************************/
   1.104 +
   1.105 +#else
   1.106 +
   1.107 +#include <time.h>
   1.108 +
   1.109 +glp_long glp_time(void)
   1.110 +{     time_t timer;
   1.111 +      struct tm *tm;
   1.112 +      glp_long t;
   1.113 +      int j;
   1.114 +      timer = time(NULL);
   1.115 +      tm = gmtime(&timer);
   1.116 +      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
   1.117 +      xassert(j >= 0);
   1.118 +      t = xlset(j - epoch);
   1.119 +      t = xlmul(t, xlset(24));
   1.120 +      t = xladd(t, xlset(tm->tm_hour));
   1.121 +      t = xlmul(t, xlset(60));
   1.122 +      t = xladd(t, xlset(tm->tm_min));
   1.123 +      t = xlmul(t, xlset(60));
   1.124 +      t = xladd(t, xlset(tm->tm_sec));
   1.125 +      t = xlmul(t, xlset(1000));
   1.126 +      return t;
   1.127 +}
   1.128 +
   1.129 +#endif
   1.130 +
   1.131 +/***********************************************************************
   1.132 +*  NAME
   1.133 +*
   1.134 +*  glp_difftime - compute difference between two time values
   1.135 +*
   1.136 +*  SYNOPSIS
   1.137 +*
   1.138 +*  double glp_difftime(glp_long t1, glp_long t0);
   1.139 +*
   1.140 +*  RETURNS
   1.141 +*
   1.142 +*  The routine glp_difftime returns the difference between two time
   1.143 +*  values t1 and t0, expressed in seconds. */
   1.144 +
   1.145 +double glp_difftime(glp_long t1, glp_long t0)
   1.146 +{     return
   1.147 +         xltod(xlsub(t1, t0)) / 1000.0;
   1.148 +}
   1.149 +
   1.150 +/**********************************************************************/
   1.151 +
   1.152 +#if 0
   1.153 +int main(void)
   1.154 +{     glp_long t;
   1.155 +      glp_ldiv d;
   1.156 +      int ttt, ss, mm, hh, day, month, year;
   1.157 +      char s[50];
   1.158 +      t = glp_time();
   1.159 +      xprintf("t = %s\n", xltoa(t, s));
   1.160 +      d = xldiv(t, xlset(1000));
   1.161 +      ttt = d.rem.lo, t = d.quot;
   1.162 +      d = xldiv(t, xlset(60));
   1.163 +      ss = d.rem.lo, t = d.quot;
   1.164 +      d = xldiv(t, xlset(60));
   1.165 +      mm = d.rem.lo, t = d.quot;
   1.166 +      d = xldiv(t, xlset(24));
   1.167 +      hh = d.rem.lo, t = d.quot;
   1.168 +      xassert(jdate(t.lo + epoch, &day, &month, &year) == 0);
   1.169 +      xprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d\n", year, month, day,
   1.170 +         hh, mm, ss, ttt);
   1.171 +      return 0;
   1.172 +}
   1.173 +#endif
   1.174 +
   1.175 +/* eof */