JWM Source Documentation
timing.c
Go to the documentation of this file.
1 
10 #include "jwm.h"
11 #include "timing.h"
12 
13 static const unsigned long MAX_TIME_SECONDS = 60;
14 
17 {
18  struct timeval val;
19  gettimeofday(&val, NULL);
20  t->seconds = val.tv_sec;
21  t->ms = val.tv_usec / 1000;
22 }
23 
29 unsigned long GetTimeDifference(const TimeType *t1, const TimeType *t2)
30 {
31  unsigned long deltaSeconds;
32  int deltaMs;
33 
34  if(t1->seconds > t2->seconds) {
35  deltaSeconds = t1->seconds - t2->seconds;
36  deltaMs = t1->ms - t2->ms;
37  } else if(t1->seconds < t2->seconds) {
38  deltaSeconds = t2->seconds - t1->seconds;
39  deltaMs = t2->ms - t1->ms;
40  } else if(t1->ms > t2->ms) {
41  deltaSeconds = 0;
42  deltaMs = t1->ms - t2->ms;
43  } else {
44  deltaSeconds = 0;
45  deltaMs = t2->ms - t1->ms;
46  }
47 
48  if(JUNLIKELY(deltaSeconds > MAX_TIME_SECONDS)) {
49  return MAX_TIME_SECONDS * 1000;
50  } else {
51  return deltaSeconds * 1000 + deltaMs;
52  }
53 
54 }
55 
57 const char *GetTimeString(const char *format, const char *zone)
58 {
59  static char saveTZ[256];
60  static char newTZ[256];
61  static char str[80];
62  time_t t;
63 
64  time(&t);
65  if(zone) {
66  const char *oldTZ = getenv("TZ");
67  if(oldTZ) {
68  snprintf(saveTZ, sizeof(saveTZ), "TZ=%s", oldTZ);
69 #ifndef HAVE_UNSETENV
70  } else {
71  strcpy(saveTZ, "TZ=");
72 #endif
73  }
74  snprintf(newTZ, sizeof(newTZ), "TZ=%s", zone);
75  putenv(newTZ);
76  tzset();
77  strftime(str, sizeof(str), format, localtime(&t));
78 #ifdef HAVE_UNSETENV
79  if(oldTZ) {
80  putenv(saveTZ);
81  } else {
82  unsetenv("TZ");
83  }
84 #else
85  putenv(saveTZ);
86 #endif
87  } else {
88  strftime(str, sizeof(str), format, localtime(&t));
89  }
90 
91  return str;
92 }

joewing.net / Projects / JWM