diff options
author | Andrey Nazarov <skuller@skuller.net> | 2010-11-05 23:45:39 +0300 |
---|---|---|
committer | Andrey Nazarov <skuller@skuller.net> | 2010-11-11 16:54:28 +0300 |
commit | 41681b1193cbb89bc926fa4d9a5b205ef20aafb9 (patch) | |
tree | 0c62f0807aa5723cfd5fee3c5d7d001a3a65ff44 /src/common.c | |
parent | ae182f82645db7c7d7c3c60384d9f1642d1d4dc3 (diff) |
Avoid calling localtime() too often since it is not that cheap.
Diffstat (limited to 'src/common.c')
-rw-r--r-- | src/common.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/common.c b/src/common.c index e6c803a..c8b1b10 100644 --- a/src/common.c +++ b/src/common.c @@ -213,7 +213,9 @@ static void logfile_param_changed( cvar_t *self ) { } static size_t format_local_time( char *buffer, size_t size, const char *fmt ) { - time_t t; + static struct tm cached_tm; + static time_t cached_time; + time_t now; struct tm *tm; if( !size ) { @@ -222,10 +224,17 @@ static size_t format_local_time( char *buffer, size_t size, const char *fmt ) { buffer[0] = 0; - t = time( NULL ); - tm = localtime( &t ); - if( !tm ) { - return 0; + now = time( NULL ); + if( now == cached_time ) { + // avoid calling localtime() too often since it is not that cheap + tm = &cached_tm; + } else { + tm = localtime( &now ); + if( !tm ) { + return 0; + } + cached_time = now; + cached_tm = *tm; } return strftime( buffer, size, fmt, tm ); |