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