commit e4678a7b0058662953bd640823b26606e710564c
parent d9de09f797e53933d3e77323311ec3a89f0b965b
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 30 Apr 2026 11:56:35 +0200
libc/time: Optimize localtime()
When it is not a dst date we can avoid the additional call to gmtime()
because we already did a call with the correct offset.
Diffstat:
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/libc/time/localtime.c b/src/libc/time/localtime.c
@@ -1,4 +1,3 @@
-#include <string.h>
#include <time.h>
#include "../libc.h"
@@ -15,18 +14,20 @@ localtime(const time_t *timep)
_tzset();
t = *timep - _timezone;
- if (_isdst(gmtime(&t))) {
+ tm = gmtime(&t);
+
+ if (_isdst(tm)) {
dst = 1;
off = _dstzone;
name = _tzname[1];
+ t = *timep - off;
+ tm = gmtime(&t);
} else {
dst = 0;
off = _timezone;
name = _tzname[0];
}
- t = *timep - off;
- tm = gmtime(&t);
tm->tm_zone = name;
tm->tm_isdst = dst;
tm->tm_gmtoff = off;