src/glpenv06.c
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/* glpenv06.c (standard time) */
alpar@1
     2
alpar@1
     3
/***********************************************************************
alpar@1
     4
*  This code is part of GLPK (GNU Linear Programming Kit).
alpar@1
     5
*
alpar@1
     6
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@1
     7
*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
alpar@1
     8
*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@1
     9
*  E-mail: <mao@gnu.org>.
alpar@1
    10
*
alpar@1
    11
*  GLPK is free software: you can redistribute it and/or modify it
alpar@1
    12
*  under the terms of the GNU General Public License as published by
alpar@1
    13
*  the Free Software Foundation, either version 3 of the License, or
alpar@1
    14
*  (at your option) any later version.
alpar@1
    15
*
alpar@1
    16
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@1
    17
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@1
    18
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@1
    19
*  License for more details.
alpar@1
    20
*
alpar@1
    21
*  You should have received a copy of the GNU General Public License
alpar@1
    22
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@1
    23
***********************************************************************/
alpar@1
    24
alpar@1
    25
#ifdef HAVE_CONFIG_H
alpar@1
    26
#include <config.h>
alpar@1
    27
#endif
alpar@1
    28
alpar@1
    29
#include "glpapi.h"
alpar@1
    30
alpar@1
    31
/***********************************************************************
alpar@1
    32
*  NAME
alpar@1
    33
*
alpar@1
    34
*  glp_time - determine current universal time
alpar@1
    35
*
alpar@1
    36
*  SYNOPSIS
alpar@1
    37
*
alpar@1
    38
*  glp_long glp_time(void);
alpar@1
    39
*
alpar@1
    40
*  RETURNS
alpar@1
    41
*
alpar@1
    42
*  The routine glp_time returns the current universal time (UTC), in
alpar@1
    43
*  milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
alpar@1
    44
alpar@1
    45
static const int epoch = 2440588; /* = jday(1, 1, 1970) */
alpar@1
    46
alpar@1
    47
/* POSIX version ******************************************************/
alpar@1
    48
alpar@1
    49
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
alpar@1
    50
alpar@1
    51
#include <sys/time.h>
alpar@1
    52
#include <time.h>
alpar@1
    53
alpar@1
    54
glp_long glp_time(void)
alpar@1
    55
{     struct timeval tv;
alpar@1
    56
      struct tm *tm;
alpar@1
    57
      glp_long t;
alpar@1
    58
      int j;
alpar@1
    59
      gettimeofday(&tv, NULL);
alpar@1
    60
      tm = gmtime(&tv.tv_sec);
alpar@1
    61
      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
alpar@1
    62
      xassert(j >= 0);
alpar@1
    63
      t = xlset(j - epoch);
alpar@1
    64
      t = xlmul(t, xlset(24));
alpar@1
    65
      t = xladd(t, xlset(tm->tm_hour));
alpar@1
    66
      t = xlmul(t, xlset(60));
alpar@1
    67
      t = xladd(t, xlset(tm->tm_min));
alpar@1
    68
      t = xlmul(t, xlset(60));
alpar@1
    69
      t = xladd(t, xlset(tm->tm_sec));
alpar@1
    70
      t = xlmul(t, xlset(1000));
alpar@1
    71
      t = xladd(t, xlset(tv.tv_usec / 1000));
alpar@1
    72
      return t;
alpar@1
    73
}
alpar@1
    74
alpar@1
    75
/* Windows version ****************************************************/
alpar@1
    76
alpar@1
    77
#elif defined(__WOE__)
alpar@1
    78
alpar@1
    79
#include <windows.h>
alpar@1
    80
alpar@1
    81
glp_long glp_time(void)
alpar@1
    82
{     SYSTEMTIME st;
alpar@1
    83
      glp_long t;
alpar@1
    84
      int j;
alpar@1
    85
      GetSystemTime(&st);
alpar@1
    86
      j = jday(st.wDay, st.wMonth, st.wYear);
alpar@1
    87
      xassert(j >= 0);
alpar@1
    88
      t = xlset(j - epoch);
alpar@1
    89
      t = xlmul(t, xlset(24));
alpar@1
    90
      t = xladd(t, xlset(st.wHour));
alpar@1
    91
      t = xlmul(t, xlset(60));
alpar@1
    92
      t = xladd(t, xlset(st.wMinute));
alpar@1
    93
      t = xlmul(t, xlset(60));
alpar@1
    94
      t = xladd(t, xlset(st.wSecond));
alpar@1
    95
      t = xlmul(t, xlset(1000));
alpar@1
    96
      t = xladd(t, xlset(st.wMilliseconds));
alpar@1
    97
      return t;
alpar@1
    98
}
alpar@1
    99
alpar@1
   100
/* portable ISO C version *********************************************/
alpar@1
   101
alpar@1
   102
#else
alpar@1
   103
alpar@1
   104
#include <time.h>
alpar@1
   105
alpar@1
   106
glp_long glp_time(void)
alpar@1
   107
{     time_t timer;
alpar@1
   108
      struct tm *tm;
alpar@1
   109
      glp_long t;
alpar@1
   110
      int j;
alpar@1
   111
      timer = time(NULL);
alpar@1
   112
      tm = gmtime(&timer);
alpar@1
   113
      j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
alpar@1
   114
      xassert(j >= 0);
alpar@1
   115
      t = xlset(j - epoch);
alpar@1
   116
      t = xlmul(t, xlset(24));
alpar@1
   117
      t = xladd(t, xlset(tm->tm_hour));
alpar@1
   118
      t = xlmul(t, xlset(60));
alpar@1
   119
      t = xladd(t, xlset(tm->tm_min));
alpar@1
   120
      t = xlmul(t, xlset(60));
alpar@1
   121
      t = xladd(t, xlset(tm->tm_sec));
alpar@1
   122
      t = xlmul(t, xlset(1000));
alpar@1
   123
      return t;
alpar@1
   124
}
alpar@1
   125
alpar@1
   126
#endif
alpar@1
   127
alpar@1
   128
/***********************************************************************
alpar@1
   129
*  NAME
alpar@1
   130
*
alpar@1
   131
*  glp_difftime - compute difference between two time values
alpar@1
   132
*
alpar@1
   133
*  SYNOPSIS
alpar@1
   134
*
alpar@1
   135
*  double glp_difftime(glp_long t1, glp_long t0);
alpar@1
   136
*
alpar@1
   137
*  RETURNS
alpar@1
   138
*
alpar@1
   139
*  The routine glp_difftime returns the difference between two time
alpar@1
   140
*  values t1 and t0, expressed in seconds. */
alpar@1
   141
alpar@1
   142
double glp_difftime(glp_long t1, glp_long t0)
alpar@1
   143
{     return
alpar@1
   144
         xltod(xlsub(t1, t0)) / 1000.0;
alpar@1
   145
}
alpar@1
   146
alpar@1
   147
/**********************************************************************/
alpar@1
   148
alpar@1
   149
#if 0
alpar@1
   150
int main(void)
alpar@1
   151
{     glp_long t;
alpar@1
   152
      glp_ldiv d;
alpar@1
   153
      int ttt, ss, mm, hh, day, month, year;
alpar@1
   154
      char s[50];
alpar@1
   155
      t = glp_time();
alpar@1
   156
      xprintf("t = %s\n", xltoa(t, s));
alpar@1
   157
      d = xldiv(t, xlset(1000));
alpar@1
   158
      ttt = d.rem.lo, t = d.quot;
alpar@1
   159
      d = xldiv(t, xlset(60));
alpar@1
   160
      ss = d.rem.lo, t = d.quot;
alpar@1
   161
      d = xldiv(t, xlset(60));
alpar@1
   162
      mm = d.rem.lo, t = d.quot;
alpar@1
   163
      d = xldiv(t, xlset(24));
alpar@1
   164
      hh = d.rem.lo, t = d.quot;
alpar@1
   165
      xassert(jdate(t.lo + epoch, &day, &month, &year) == 0);
alpar@1
   166
      xprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d\n", year, month, day,
alpar@1
   167
         hh, mm, ss, ttt);
alpar@1
   168
      return 0;
alpar@1
   169
}
alpar@1
   170
#endif
alpar@1
   171
alpar@1
   172
/* eof */