glpenv06.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* glpenv06.c (standard time) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  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. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include "glpapi.h"
  27. /***********************************************************************
  28. * NAME
  29. *
  30. * glp_time - determine current universal time
  31. *
  32. * SYNOPSIS
  33. *
  34. * glp_long glp_time(void);
  35. *
  36. * RETURNS
  37. *
  38. * The routine glp_time returns the current universal time (UTC), in
  39. * milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
  40. static const int epoch = 2440588; /* = jday(1, 1, 1970) */
  41. /* POSIX version ******************************************************/
  42. #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
  43. #include <sys/time.h>
  44. #include <time.h>
  45. glp_long glp_time(void)
  46. { struct timeval tv;
  47. struct tm *tm;
  48. glp_long t;
  49. int j;
  50. gettimeofday(&tv, NULL);
  51. tm = gmtime(&tv.tv_sec);
  52. j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
  53. xassert(j >= 0);
  54. t = xlset(j - epoch);
  55. t = xlmul(t, xlset(24));
  56. t = xladd(t, xlset(tm->tm_hour));
  57. t = xlmul(t, xlset(60));
  58. t = xladd(t, xlset(tm->tm_min));
  59. t = xlmul(t, xlset(60));
  60. t = xladd(t, xlset(tm->tm_sec));
  61. t = xlmul(t, xlset(1000));
  62. t = xladd(t, xlset(tv.tv_usec / 1000));
  63. return t;
  64. }
  65. /* Windows version ****************************************************/
  66. #elif defined(__WOE__)
  67. #include <windows.h>
  68. glp_long glp_time(void)
  69. { SYSTEMTIME st;
  70. glp_long t;
  71. int j;
  72. GetSystemTime(&st);
  73. j = jday(st.wDay, st.wMonth, st.wYear);
  74. xassert(j >= 0);
  75. t = xlset(j - epoch);
  76. t = xlmul(t, xlset(24));
  77. t = xladd(t, xlset(st.wHour));
  78. t = xlmul(t, xlset(60));
  79. t = xladd(t, xlset(st.wMinute));
  80. t = xlmul(t, xlset(60));
  81. t = xladd(t, xlset(st.wSecond));
  82. t = xlmul(t, xlset(1000));
  83. t = xladd(t, xlset(st.wMilliseconds));
  84. return t;
  85. }
  86. /* portable ISO C version *********************************************/
  87. #else
  88. #include <time.h>
  89. glp_long glp_time(void)
  90. { time_t timer;
  91. struct tm *tm;
  92. glp_long t;
  93. int j;
  94. timer = time(NULL);
  95. tm = gmtime(&timer);
  96. j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
  97. xassert(j >= 0);
  98. t = xlset(j - epoch);
  99. t = xlmul(t, xlset(24));
  100. t = xladd(t, xlset(tm->tm_hour));
  101. t = xlmul(t, xlset(60));
  102. t = xladd(t, xlset(tm->tm_min));
  103. t = xlmul(t, xlset(60));
  104. t = xladd(t, xlset(tm->tm_sec));
  105. t = xlmul(t, xlset(1000));
  106. return t;
  107. }
  108. #endif
  109. /***********************************************************************
  110. * NAME
  111. *
  112. * glp_difftime - compute difference between two time values
  113. *
  114. * SYNOPSIS
  115. *
  116. * double glp_difftime(glp_long t1, glp_long t0);
  117. *
  118. * RETURNS
  119. *
  120. * The routine glp_difftime returns the difference between two time
  121. * values t1 and t0, expressed in seconds. */
  122. double glp_difftime(glp_long t1, glp_long t0)
  123. { return
  124. xltod(xlsub(t1, t0)) / 1000.0;
  125. }
  126. /**********************************************************************/
  127. #if 0
  128. int main(void)
  129. { glp_long t;
  130. glp_ldiv d;
  131. int ttt, ss, mm, hh, day, month, year;
  132. char s[50];
  133. t = glp_time();
  134. xprintf("t = %s\n", xltoa(t, s));
  135. d = xldiv(t, xlset(1000));
  136. ttt = d.rem.lo, t = d.quot;
  137. d = xldiv(t, xlset(60));
  138. ss = d.rem.lo, t = d.quot;
  139. d = xldiv(t, xlset(60));
  140. mm = d.rem.lo, t = d.quot;
  141. d = xldiv(t, xlset(24));
  142. hh = d.rem.lo, t = d.quot;
  143. xassert(jdate(t.lo + epoch, &day, &month, &year) == 0);
  144. xprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d\n", year, month, day,
  145. hh, mm, ss, ttt);
  146. return 0;
  147. }
  148. #endif
  149. /* eof */