summaryrefslogtreecommitdiff
path: root/corelib/ncbitime.c
blob: f8964054ce91e944588ee9889dcbd6d1ac7fe710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*  ncbitime.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name:  ncbitime.c
*
* Author:  Ostell, Kans
*
* Version Creation Date:  1/1/90
*
* $Revision: 6.3 $
*
* File Description:
*   misc portable routines for
*   dates, times, timers
*
* Modifications:
* --------------------------------------------------------------------------
* $Log: ncbitime.c,v $
* Revision 6.3  2006/10/19 16:58:26  lavr
* Cache clock rate instead of requesting it each time on OS_UNIX
*
* Revision 6.2  2001/01/19 20:26:12  kans
* support for OS_UNIX_DARWIN (contributed by William Van Etten)
*
* Revision 6.1  1997/10/29 02:45:09  vakatov
* Type castings to pass through the C++ compiler
*
* Revision 6.0  1997/08/25 18:17:21  madden
* Revision changed to 6.0
*
* Revision 5.7  1997/07/17 16:09:31  vakatov
* Encapsulated "NCBI_months" global variable into a {function + cpp macros}
*
* Revision 5.6  1996/12/03 21:48:33  vakatov
* Adopted for 32-bit MS-Windows DLLs
*
 * Revision 5.5  1996/11/25  19:05:52  vakatov
 * Made Nlm_GetDayTime() MT-safe
 *
 * Revision 5.3  1996/09/17  15:23:25  sad
 * Corrected compilation problem under SunOS 4 related with CLK_TCK constant
 *
 * Revision 5.2  1996/09/16  20:19:57  kans
 * structures cannot be empty, so needed to add #else condition in two places
 *
 * Revision 5.1  1996/09/16  19:37:19  sad
 * Added set of functions for measuring CPU time.
 * Added StopWatch set of functions.
 *
* 04-15-93 Schuler     Changed _cdecl to LIBCALL
*
* ==========================================================================
*/

#include <ncbi.h>
#include <ncbithr.h>
#include <ncbiwin.h>
#include <stdlib.h>

#ifdef OS_UNIX
#include <sys/times.h>
#include <unistd.h>
#endif

static char *STATIC__NCBI_months[12] = {
  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
NLM_EXTERN char **x_NCBI_months(void)  { return &STATIC__NCBI_months[0]; }

static char *ampm[2] = { "AM", "PM" };


/*****************************************************************************
*
*   Nlm_GetSecs()
*   	returns computer time in seconds - for timing purposes only
*
*****************************************************************************/
NLM_EXTERN time_t LIBCALL  Nlm_GetSecs (void)
{
  return time(NULL);
}

/*****************************************************************************
*
*   Nlm_GetDayTime(DayTimePtr)
*
*****************************************************************************/
NLM_EXTERN Nlm_Boolean LIBCALL  Nlm_GetDayTime (Nlm_DayTimePtr dtp)
{
  /* This function honors the SOURCE_DATE_EPOCH specificatin
     (https://reproducible-builds.org/specs/source-date-epoch/) */
  const char *sde = getenv("SOURCE_DATE_EPOCH");
#if (defined(SOLARIS_THREADS_AVAIL)  ||  defined(POSIX_THREADS_AVAIL)  ||  defined(WIN32)) && !defined(OS_UNIX_DARWIN)
  time_t t;
  if (sde) {
    t = strtoul(sde, NULL, 0);
    if (t == 0)
      return FALSE;
  } else {
    t = time( NULL );
  }
#ifdef WIN32
  static TNlmMutex localtime_lock;
  if (NlmMutexLockEx( &localtime_lock ) != 0)
    abort();
  *dtp = *localtime( &t );
  NlmMutexUnlock( localtime_lock );
#else
  localtime_r(&t, dtp);
#endif
  return TRUE;

#else
  time_t ltime;
  struct tm *dt;
  Nlm_MemFill ((Nlm_VoidPtr) dtp, 0, sizeof(Nlm_DayTime));
  if (sde) {
    ltime = strtoul(sde, NULL, 0);
    if (ltime == 0)
      return FALSE;
  } else {
    time(&ltime);
  }
  if ((dt = localtime (&ltime)) != NULL)
 	{
        Nlm_MemCopy ((Nlm_VoidPtr) dtp, (Nlm_VoidPtr) dt, sizeof (struct tm));
        return TRUE;
    }
    return FALSE;
#endif
}

/*****************************************************************************
*
*   Nlm_DayTimeStr(buf, date, time)
*   	fills buffer with formatted date and/or time
*   	date requires a 13 character buffer
*       time requeres a 10 character buffer
*   	data and time takes 23
*
*       eg.   "Jan 28, 1988 12:59 PM"
*
*****************************************************************************/
NLM_EXTERN Nlm_Boolean LIBCALL  Nlm_DayTimeStr (Nlm_CharPtr buf, Nlm_Boolean nlm_date, Nlm_Boolean nlm_time)

{
	Nlm_DayTime dt;
	char localbuf[30];
	int pm;

	buf[0] = '\0';

	if ( ! Nlm_GetDayTime(&dt))
		return FALSE;

	if (nlm_date)
	{
		sprintf (localbuf, "%s %d, %d", NCBI_months [dt.tm_mon],
                    dt.tm_mday, dt.tm_year + 1900);
		StringCpy (buf, localbuf);
	}
	if (nlm_time)
	{
		pm = 1;
		if (dt.tm_hour == 0)
		{
			dt.tm_hour = 12;
			pm = 0;
		}
		else if (dt.tm_hour < 12)
			pm = 0;
		else if (dt.tm_hour > 12)
			dt.tm_hour -= 12;

	    sprintf (localbuf, "%3d:%2d %s", dt.tm_hour, dt.tm_min, ampm[pm]);
		if (dt.tm_min < 10)
			localbuf[4] = '0';
		StringCat (buf, localbuf);
	}
	return TRUE;
}

struct _StopWatch {
#if defined(WIN32)
    FILETIME	start;
    FILETIME	stop;
#elif defined(OS_UNIX)
    clock_t	start;
    clock_t	stop;
    clock_t	rate;
#else
    int         dummy;
#endif
};
typedef struct _StopWatch Nlm_StopWatch;

NLM_EXTERN Nlm_StopWatchPtr Nlm_StopWatchNew(void)
{
#if defined(WIN32) || defined(OS_UNIX)
    Nlm_StopWatchPtr pSW = (Nlm_StopWatchPtr)Nlm_MemNew(sizeof(Nlm_StopWatch));
#  if defined(OS_UNIX)
    if (pSW != NULL)
      pSW->rate = sysconf(_SC_CLK_TCK);
#  endif
    return pSW;
#else
    ErrPostEx(SEV_WARNING, 0, 0,
	"StopWatch is not implemented for this platform");
    return NULL;
#endif
}

NLM_EXTERN Nlm_StopWatchPtr Nlm_StopWatchFree(Nlm_StopWatchPtr pStopWatch)
{
#if defined(WIN32) || defined(OS_UNIX)
    return (Nlm_StopWatchPtr) Nlm_MemFree(pStopWatch);
#else
    ErrPostEx(SEV_WARNING, 0, 0,
	"StopWatch is not implemented for this platform");
    return NULL;
#endif
}

NLM_EXTERN Nlm_StopWatchPtr Nlm_StopWatchStart(Nlm_StopWatchPtr pSW)
{
#if defined(WIN32)
    SYSTEMTIME	systime;

    GetSystemTime(&systime);
    SystemTimeToFileTime(&systime, &pSW->start);
#elif defined(OS_UNIX)
    struct tms	foo;

    pSW->start = times(&foo);
#else
    ErrPostEx(SEV_WARNING, 0, 0,
	"StopWatch is not implemented for this platform");
#endif
    return pSW;
}

NLM_EXTERN Nlm_StopWatchPtr Nlm_StopWatchStop(Nlm_StopWatchPtr pSW)
{
#if defined(WIN32)
    SYSTEMTIME	systime;

    GetSystemTime(&systime);
    SystemTimeToFileTime(&systime, &pSW->stop);
#elif defined(OS_UNIX)
    struct tms	foo;

    pSW->stop = times(&foo);
#else
    ErrPostEx(SEV_WARNING, 0, 0,
	"StopWatch is not implemented for this platform");
#endif
    return pSW;
}

NLM_EXTERN Nlm_FloatHi Nlm_GetElapsedTime(Nlm_StopWatchPtr pSW)
{
    Nlm_FloatHi	res = 0;
#if defined(WIN32)
    FILETIME	ft;

    if (pSW->start.dwLowDateTime > pSW->stop.dwLowDateTime) {
	ft.dwLowDateTime = (0xFFFFFFFFL - pSW->start.dwLowDateTime) + 1
	    + pSW->stop.dwLowDateTime;
	ft.dwHighDateTime =
	    pSW->stop.dwHighDateTime - pSW->start.dwHighDateTime - 1;
    } else {
	ft.dwLowDateTime =
	    pSW->stop.dwLowDateTime - pSW->start.dwLowDateTime;
	ft.dwHighDateTime =
	    pSW->stop.dwHighDateTime - pSW->start.dwHighDateTime;
    }

    return res = (Nlm_FloatHi)ft.dwHighDateTime/5000000L*0x80000000L +
	(Nlm_FloatHi)ft.dwLowDateTime/10000000L;
#elif defined(OS_UNIX)
    res = ((Nlm_FloatHi)pSW->stop - pSW->start) / pSW->rate;
#else
    ErrPostEx(SEV_WARNING, 0, 0,
	"StopWatch is not implemented for this platform");
#endif

    return res;
}


struct _CPUTime {
#if defined(WIN32)
	FILETIME	crtime;
	FILETIME	extime;
	FILETIME	systime;
	FILETIME	usrtime;
#elif defined(OS_UNIX)
    struct tms	times;
    clock_t    rate;
#else
    int         dummy;
#endif
};

typedef struct _CPUTime Nlm_CPUTime;

NLM_EXTERN Nlm_CPUTimePtr Nlm_CPUTimeMeasure(void)
{
#if defined(WIN32) || defined(OS_UNIX)
  Nlm_CPUTimePtr pTime = (Nlm_CPUTimePtr) Nlm_MemNew(sizeof(Nlm_CPUTime));
  if (pTime != NULL) {
#endif

#if defined(WIN32)
    GetProcessTimes(GetCurrentProcess(),
                    &pTime->crtime,
                    &pTime->extime,
                    &pTime->systime,
                    &pTime->usrtime
                    );
#elif defined(OS_UNIX)
    times(&pTime->times);
    pTime->rate = sysconf(_SC_CLK_TCK);
#else
    ErrPostEx(SEV_WARNING, 0, 0,
              "CPU time measuring is not implemented for this platform");
    return NULL;
#endif

#if defined(WIN32) || defined(OS_UNIX)
    }

    return pTime;
#endif
}

NLM_EXTERN Nlm_CPUTimePtr Nlm_CPUTimeFree(Nlm_CPUTimePtr pTime)
{
#if defined(WIN32) || defined(OS_UNIX)
  return (Nlm_CPUTimePtr) Nlm_MemFree(pTime);
#else
  ErrPostEx(SEV_WARNING, 0, 0,
            "CPU time measuring is not implemented for this platform");
  return NULL;
#endif
}

NLM_EXTERN Nlm_FloatHi Nlm_CPUTimeGetSys(Nlm_CPUTimePtr pTime)
{
#if defined(WIN32)
  return (Nlm_FloatHi)pTime->systime.dwHighDateTime * 0x80000000L/5000000L
		+ (Nlm_FloatHi)pTime->systime.dwLowDateTime / 10000000L;
#elif defined(OS_UNIX)
  return (Nlm_FloatHi)pTime->times.tms_stime / pTime->rate;
#else
  ErrPostEx(SEV_WARNING, 0, 0,
            "CPU time measuring is not implemented for this platform");
  return 0;
#endif
}

NLM_EXTERN Nlm_FloatHi Nlm_CPUTimeGetUser(Nlm_CPUTimePtr pTime)
{
#if defined(WIN32)
  return (Nlm_FloatHi)pTime->usrtime.dwHighDateTime * 0x80000000L/5000000L
    + (Nlm_FloatHi)pTime->usrtime.dwLowDateTime / 10000000L;
#elif defined(OS_UNIX)
  return (Nlm_FloatHi)pTime->times.tms_utime / pTime->rate;
#else
  ErrPostEx(SEV_WARNING, 0, 0,
            "CPU time measuring is not implemented for this platform");
  return 0;
#endif
}