/farmR/src/glplib10.c

https://code.google.com/p/javawfm/ · C · 120 lines · 74 code · 8 blank · 38 comment · 4 complexity · 9399b0cfd2df1ac7edace22637ad7da3 MD5 · raw file

  1. /* glplib10.c (standard time) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000,01,02,03,04,05,06,07,08,2009 Andrew Makhorin,
  6. * Department for Applied Informatics, Moscow Aviation Institute,
  7. * Moscow, Russia. All rights reserved. E-mail: <mao@mai2.rcnet.ru>.
  8. *
  9. * GLPK is free software: you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  21. ***********************************************************************/
  22. #define _GLPSTD_TIME
  23. #include "glplib.h"
  24. /* platform-independent ISO C version */
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * xtime - determine the current universal time
  29. *
  30. * SYNOPSIS
  31. *
  32. * #include "glplib.h"
  33. * xlong_t xtime(void);
  34. *
  35. * RETURNS
  36. *
  37. * The routine xtime returns the current universal time (UTC), in
  38. * milliseconds, elapsed since 00:00:00 GMT January 1, 1970. */
  39. #if 0
  40. xlong_t xtime(void)
  41. { static const int epoch = 2440588; /* jday(1, 1, 1970) */
  42. time_t timer;
  43. struct tm *tm;
  44. xlong_t t;
  45. int j;
  46. timer = time(NULL);
  47. tm = gmtime(&timer);
  48. j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
  49. xassert(j >= 0);
  50. t = xlset(j - epoch);
  51. t = xlmul(t, xlset(24));
  52. t = xladd(t, xlset(tm->tm_hour));
  53. t = xlmul(t, xlset(60));
  54. t = xladd(t, xlset(tm->tm_min));
  55. t = xlmul(t, xlset(60));
  56. t = xladd(t, xlset(tm->tm_sec));
  57. t = xlmul(t, xlset(1000));
  58. return t;
  59. }
  60. #else
  61. static xlong_t zeit(void)
  62. { static const int epoch = 2440588; /* jday(1, 1, 1970) */
  63. time_t timer;
  64. struct tm *tm;
  65. xlong_t t;
  66. int j;
  67. timer = time(NULL);
  68. tm = gmtime(&timer);
  69. j = jday(tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
  70. xassert(j >= 0);
  71. t = xlset(j - epoch);
  72. t = xlmul(t, xlset(24));
  73. t = xladd(t, xlset(tm->tm_hour));
  74. t = xlmul(t, xlset(60));
  75. t = xladd(t, xlset(tm->tm_min));
  76. t = xlmul(t, xlset(60));
  77. t = xladd(t, xlset(tm->tm_sec));
  78. t = xlmul(t, xlset(1000));
  79. return t;
  80. }
  81. xlong_t xtime(void)
  82. { LIBENV *env = lib_link_env();
  83. xlong_t t;
  84. clock_t c;
  85. double secs;
  86. xassert(sizeof(clock_t) <= sizeof(env->c_init));
  87. t = zeit();
  88. if (xlcmp(xlsub(t, env->t_init), xlset(600 * 1000)) <= 0)
  89. { /* not more than ten minutes since the last call */
  90. memcpy(&c, env->c_init, sizeof(clock_t));
  91. secs = (double)(clock() - c) / (double)CLOCKS_PER_SEC;
  92. if (0.0 <= secs && secs <= 1000.0)
  93. { /* looks like correct value */
  94. t = xladd(env->t_init, xlset((int)(1000.0 * secs + 0.5)));
  95. goto done;
  96. }
  97. }
  98. /* re-initialize */
  99. if (xlcmp(t, env->t_last) < 0) t = env->t_last;
  100. env->t_init = t;
  101. c = clock();
  102. memcpy(env->c_init, &c, sizeof(clock_t));
  103. done: xassert(xlcmp(env->t_last, t) <= 0);
  104. env->t_last = t;
  105. return t;
  106. }
  107. #endif
  108. double xdifftime(xlong_t t1, xlong_t t0)
  109. { /* compute the difference between two time values, in seconds */
  110. return xltod(xlsub(t1, t0)) / 1000.0;
  111. }
  112. /* eof */