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