~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pith/osdep/debugtime.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: debugtime.c 165 2006-10-04 01:09:47Z jpf@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
9
 * Licensed under the Apache License, Version 2.0 (the "License");
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 */
 
17
 
 
18
#include <stdio.h>
 
19
 
 
20
#include <system.h>
 
21
#include "debugtime.h"
 
22
 
 
23
#ifdef DEBUG
 
24
 
 
25
/*
 
26
 * Returns a pointer to static string for a timestamp.
 
27
 *
 
28
 * If timestamp is set .subseconds are added if available.
 
29
 * If include_date is set the date is appended.
 
30
 */
 
31
char *
 
32
debug_time(int include_date, int include_subseconds)
 
33
{
 
34
    time_t          t;
 
35
    struct tm      *tm_now;
 
36
#if     HAVE_GETTIMEOFDAY
 
37
    struct timeval  tp;
 
38
    struct timezone tzp;
 
39
#else
 
40
    struct _timeb   timebuffer;
 
41
#endif
 
42
    static char     timestring[23];
 
43
    char            subsecond[8];
 
44
    char            datestr[7];
 
45
 
 
46
    timestring[0] = '\0';
 
47
 
 
48
#if     HAVE_GETTIMEOFDAY
 
49
    if(gettimeofday(&tp, &tzp) == 0){
 
50
        t = (time_t)tp.tv_sec;
 
51
        if(include_date){
 
52
            tm_now = localtime(&t);
 
53
            snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
 
54
        }
 
55
        else
 
56
          datestr[0] = '\0';
 
57
 
 
58
        if(include_subseconds)
 
59
          snprintf(subsecond, sizeof(subsecond), ".%06ld", tp.tv_usec);
 
60
        else
 
61
          subsecond[0] = '\0';
 
62
 
 
63
        snprintf(timestring, sizeof(timestring), "%.8s%.7s%.6s", ctime(&t)+11, subsecond, datestr);
 
64
    }
 
65
#else /* !HAVE_GETTIMEOFDAY */
 
66
    /* Should be _WINDOWS */
 
67
    t = time((time_t *)0);
 
68
    if(include_date){
 
69
        tm_now = localtime(&t);
 
70
        snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
 
71
    }
 
72
    else
 
73
      datestr[0] = '\0';
 
74
 
 
75
    if(include_subseconds){
 
76
        _ftime(&timebuffer);
 
77
        snprintf(subsecond, sizeof(subsecond), ".%03ld", timebuffer.millitm);
 
78
    }
 
79
    else
 
80
      subsecond[0] = '\0';
 
81
 
 
82
    snprintf(timestring, sizeof(timestring), "%.8s%.7s%.6s", ctime(&t)+11, subsecond, datestr);
 
83
#endif /* HAVE_GETTIMEOFDAY */
 
84
 
 
85
    return(timestring);
 
86
}
 
87
#endif /* DEBUG */
 
88
 
 
89
 
 
90
/*
 
91
 * Fills in the passed in structure with the current time.
 
92
 *
 
93
 * Returns 0 if ok
 
94
 *        -1 if can't do it
 
95
 */
 
96
int
 
97
get_time(TIMEVAL_S *our_time_val)
 
98
{
 
99
#if     HAVE_GETTIMEOFDAY
 
100
    struct timeval  tp;
 
101
    struct timezone tzp;
 
102
 
 
103
    if(gettimeofday(&tp, &tzp) == 0){
 
104
        our_time_val->sec  = tp.tv_sec;
 
105
        our_time_val->usec = tp.tv_usec;
 
106
        return 0;
 
107
    }
 
108
#endif
 
109
 
 
110
    return -1;
 
111
}
 
112
 
 
113
 
 
114
/*
 
115
 * Returns the difference between the two values, in microseconds.
 
116
 * Value returned is first - second.
 
117
 */
 
118
long
 
119
time_diff(TIMEVAL_S *first, TIMEVAL_S *second)
 
120
{
 
121
    return(1000000L*(first->sec - second->sec) + (first->usec - second->usec));
 
122
}
 
123
 
 
124