alpar@1: /* glpenv06.c (standard time) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #ifdef HAVE_CONFIG_H alpar@1: #include alpar@1: #endif alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_time - determine current universal time alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * glp_long glp_time(void); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_time returns the current universal time (UTC), in alpar@1: * milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */ alpar@1: alpar@1: static const int epoch = 2440588; /* = jday(1, 1, 1970) */ alpar@1: alpar@1: /* POSIX version ******************************************************/ alpar@1: alpar@1: #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY) alpar@1: alpar@1: #include alpar@1: #include alpar@1: alpar@1: glp_long glp_time(void) alpar@1: { struct timeval tv; alpar@1: struct tm *tm; alpar@1: glp_long t; alpar@1: int j; alpar@1: gettimeofday(&tv, NULL); alpar@1: tm = gmtime(&tv.tv_sec); alpar@1: j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year); alpar@1: xassert(j >= 0); alpar@1: t = xlset(j - epoch); alpar@1: t = xlmul(t, xlset(24)); alpar@1: t = xladd(t, xlset(tm->tm_hour)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(tm->tm_min)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(tm->tm_sec)); alpar@1: t = xlmul(t, xlset(1000)); alpar@1: t = xladd(t, xlset(tv.tv_usec / 1000)); alpar@1: return t; alpar@1: } alpar@1: alpar@1: /* Windows version ****************************************************/ alpar@1: alpar@1: #elif defined(__WOE__) alpar@1: alpar@1: #include alpar@1: alpar@1: glp_long glp_time(void) alpar@1: { SYSTEMTIME st; alpar@1: glp_long t; alpar@1: int j; alpar@1: GetSystemTime(&st); alpar@1: j = jday(st.wDay, st.wMonth, st.wYear); alpar@1: xassert(j >= 0); alpar@1: t = xlset(j - epoch); alpar@1: t = xlmul(t, xlset(24)); alpar@1: t = xladd(t, xlset(st.wHour)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(st.wMinute)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(st.wSecond)); alpar@1: t = xlmul(t, xlset(1000)); alpar@1: t = xladd(t, xlset(st.wMilliseconds)); alpar@1: return t; alpar@1: } alpar@1: alpar@1: /* portable ISO C version *********************************************/ alpar@1: alpar@1: #else alpar@1: alpar@1: #include alpar@1: alpar@1: glp_long glp_time(void) alpar@1: { time_t timer; alpar@1: struct tm *tm; alpar@1: glp_long t; alpar@1: int j; alpar@1: timer = time(NULL); alpar@1: tm = gmtime(&timer); alpar@1: j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year); alpar@1: xassert(j >= 0); alpar@1: t = xlset(j - epoch); alpar@1: t = xlmul(t, xlset(24)); alpar@1: t = xladd(t, xlset(tm->tm_hour)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(tm->tm_min)); alpar@1: t = xlmul(t, xlset(60)); alpar@1: t = xladd(t, xlset(tm->tm_sec)); alpar@1: t = xlmul(t, xlset(1000)); alpar@1: return t; alpar@1: } alpar@1: alpar@1: #endif alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_difftime - compute difference between two time values alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * double glp_difftime(glp_long t1, glp_long t0); alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_difftime returns the difference between two time alpar@1: * values t1 and t0, expressed in seconds. */ alpar@1: alpar@1: double glp_difftime(glp_long t1, glp_long t0) alpar@1: { return alpar@1: xltod(xlsub(t1, t0)) / 1000.0; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #if 0 alpar@1: int main(void) alpar@1: { glp_long t; alpar@1: glp_ldiv d; alpar@1: int ttt, ss, mm, hh, day, month, year; alpar@1: char s[50]; alpar@1: t = glp_time(); alpar@1: xprintf("t = %s\n", xltoa(t, s)); alpar@1: d = xldiv(t, xlset(1000)); alpar@1: ttt = d.rem.lo, t = d.quot; alpar@1: d = xldiv(t, xlset(60)); alpar@1: ss = d.rem.lo, t = d.quot; alpar@1: d = xldiv(t, xlset(60)); alpar@1: mm = d.rem.lo, t = d.quot; alpar@1: d = xldiv(t, xlset(24)); alpar@1: hh = d.rem.lo, t = d.quot; alpar@1: xassert(jdate(t.lo + epoch, &day, &month, &year) == 0); alpar@1: xprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d\n", year, month, day, alpar@1: hh, mm, ss, ttt); alpar@1: return 0; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /* eof */