diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-23 11:59:53 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-29 16:13:03 -0700 |
commit | 0f4154bacccd0884ffbd5ae5bfa1fa16991675dc (patch) | |
tree | 2fd756947d9cba9a7c59d552b95933ddb54f2d06 | |
parent | efdc875aa3174ba53a819490e498a59ba0399dc6 (diff) | |
download | subsurface-0f4154bacccd0884ffbd5ae5bfa1fa16991675dc.tar.gz |
core: add functions to core/time.c
To support the new filter code, add helper functions that turn timestamps
into year and day-of-week to core/time.c.
Internally, these functions simply call utc_mktime() to break down the
timestamp and then extract the wanted value. This may appear inefficient,
but testing shows that modern compilers are quite effective in throwing
away the unneeded calculations. FWIW in this respect clang10 outperformed
gcc10.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/subsurface-time.h | 2 | ||||
-rw-r--r-- | core/time.c | 30 |
2 files changed, 32 insertions, 0 deletions
diff --git a/core/subsurface-time.h b/core/subsurface-time.h index 38d6c10de..ce2821d07 100644 --- a/core/subsurface-time.h +++ b/core/subsurface-time.h @@ -11,6 +11,8 @@ extern "C" { extern timestamp_t utc_mktime(struct tm *tm); extern void utc_mkdate(timestamp_t, struct tm *tm); +extern int utc_year(timestamp_t timestamp); +extern int utc_weekday(timestamp_t timestamp); #ifdef __cplusplus } diff --git a/core/time.c b/core/time.c index d339ffa30..94149342a 100644 --- a/core/time.c +++ b/core/time.c @@ -133,3 +133,33 @@ timestamp_t utc_mktime(struct tm *tm) return when - EPOCH_OFFSET; } + +/* + * Extract year from 64-bit timestamp. + * + * This looks inefficient, since it breaks down into a full + * struct tm. However, modern compilers are effective at throwing + * out unused calculations. If it turns out to be a bottle neck + * we will have to cache a struct tm per dive. + */ +int utc_year(timestamp_t timestamp) +{ + struct tm tm; + utc_mkdate(timestamp, &tm); + return tm.tm_year; +} + +/* + * Extract day of week from 64-bit timestamp. + * Returns 0-6, whereby 0 is Sunday and 6 is Saturday. + * + * Same comment as for utc_year(): Modern compilers are good + * at throwing out unused calculations, so this is more efficient + * than it looks. + */ +int utc_weekday(timestamp_t timestamp) +{ + struct tm tm; + utc_mkdate(timestamp, &tm); + return tm.tm_wday; +} |