diff options
author | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-05-29 12:54:47 +0200 |
---|---|---|
committer | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-05-29 13:18:40 +0200 |
commit | a041d9898e6d699bd8c0c25482ec574feb03c547 (patch) | |
tree | 7f094e33fb530152c3ab6238ce7300750b47addb /src/timing.c | |
download | jwm-a041d9898e6d699bd8c0c25482ec574feb03c547.tar.gz |
First commit
This is the original state of the released tarball for JWM 1.8,
which will serve as my starting point for further modifications.
Diffstat (limited to 'src/timing.c')
-rw-r--r-- | src/timing.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/timing.c b/src/timing.c new file mode 100644 index 0000000..047d919 --- /dev/null +++ b/src/timing.c @@ -0,0 +1,71 @@ +/**************************************************************************** + * Timing functions. + * Copyright (C) 2004 Joe Wingbermuehle + ****************************************************************************/ + +#include "jwm.h" +#include "timing.h" + +static const unsigned long MAX_TIME_SECONDS = 60; + +/**************************************************************************** + * Get the current time in milliseconds since midnight 1970-01-01 UTC. + ****************************************************************************/ +void GetCurrentTime(TimeType *t) { + struct timeval val; + gettimeofday(&val, NULL); + t->seconds = val.tv_sec; + t->ms = val.tv_usec / 1000; +} + +/**************************************************************************** + * Get the absolute difference between two times in milliseconds. + * If the difference is larger than a MAX_TIME_SECONDS, then + * MAX_TIME_SECONDS will be returned. + * Note that the times must be normalized. + ****************************************************************************/ +unsigned long GetTimeDifference(const TimeType *t1, const TimeType *t2) { + unsigned long deltaSeconds; + int deltaMs; + + if(t1->seconds > t2->seconds) { + deltaSeconds = t1->seconds - t2->seconds; + deltaMs = t1->ms - t2->ms; + } else if(t1->seconds < t2->seconds) { + deltaSeconds = t2->seconds - t1->seconds; + deltaMs = t2->ms - t1->ms; + } else if(t1->ms > t2->ms) { + deltaSeconds = 0; + deltaMs = t1->ms - t2->ms; + } else { + deltaSeconds = 0; + deltaMs = t2->ms - t1->ms; + } + + if(deltaSeconds > MAX_TIME_SECONDS) { + return MAX_TIME_SECONDS * 1000; + } else { + return deltaSeconds * 1000 + deltaMs; + } + +} + +/**************************************************************************** + * Get the current time. + * Not reenterent. + ****************************************************************************/ +const char *GetTimeString(const char *format) { + + static char str[80]; + time_t t; + + Assert(format); + + time(&t); + strftime(str, sizeof(str), format, localtime(&t)); + + return str; + +} + + |