mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-29 18:37:41 +08:00
add acl_localtime.c from redis to replace localtime_r()
This commit is contained in:
parent
7cd0450bd1
commit
211b62d2ef
@ -104,56 +104,39 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../include">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/pgsql" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/sqlite" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/mysql" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/pgsql" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/sqlite" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/mysql" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../include/mysql">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/mysql" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/mysql" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../include/pgsql">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/pgsql" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/pgsql" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../include/sqlite">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/sqlite" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../include/sqlite" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_acl">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/include" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/src" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/include" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_acl/include">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/include" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/include" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_acl/src">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl/src" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_acl_cpp/include">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl_cpp/include" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl_cpp/include" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_acl_cpp/src">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl_cpp/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_acl_cpp/src" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_fiber">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_fiber" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_fiber" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_protocol/include">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_protocol/include" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_protocol/include" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../lib_protocol/src">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_protocol/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../lib_protocol/src" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
|
||||
|
@ -242,6 +242,7 @@
|
||||
0779D5EF1DBE073000449438 /* acl_xml3_util.c in Sources */ = {isa = PBXBuildFile; fileRef = 0779D4B11DBE070E00449438 /* acl_xml3_util.c */; };
|
||||
0779D5F01DBE073000449438 /* acl_xml_parse.c in Sources */ = {isa = PBXBuildFile; fileRef = 0779D4B21DBE070E00449438 /* acl_xml_parse.c */; };
|
||||
0779D5F11DBE073000449438 /* acl_xml_util.c in Sources */ = {isa = PBXBuildFile; fileRef = 0779D4B31DBE070E00449438 /* acl_xml_util.c */; };
|
||||
70AA1BF228A383FB007E4B54 /* acl_localtime.c in Sources */ = {isa = PBXBuildFile; fileRef = 70AA1BF128A383FB007E4B54 /* acl_localtime.c */; };
|
||||
70BD6B8B232B931700E4C798 /* acl_sane_bind.c in Sources */ = {isa = PBXBuildFile; fileRef = 0779D3F91DBE070E00449438 /* acl_sane_bind.c */; };
|
||||
70BD6CA8233520C600E4C798 /* acl_sane_accept.c in Sources */ = {isa = PBXBuildFile; fileRef = 70BD6CA7233520C600E4C798 /* acl_sane_accept.c */; };
|
||||
70C98417253D4AC0003A9689 /* acl_rfc1035.c in Sources */ = {isa = PBXBuildFile; fileRef = 70C98416253D4AC0003A9689 /* acl_rfc1035.c */; };
|
||||
@ -650,6 +651,7 @@
|
||||
07A469F61E6FD4FA002C9B3C /* acl_xml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = acl_xml.h; sourceTree = "<group>"; };
|
||||
07A469F71E6FD4FA002C9B3C /* acl_xml2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = acl_xml2.h; sourceTree = "<group>"; };
|
||||
07A469F81E6FD4FA002C9B3C /* acl_xml3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = acl_xml3.h; sourceTree = "<group>"; };
|
||||
70AA1BF128A383FB007E4B54 /* acl_localtime.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = acl_localtime.c; sourceTree = "<group>"; };
|
||||
70BD6CA7233520C600E4C798 /* acl_sane_accept.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = acl_sane_accept.c; sourceTree = "<group>"; };
|
||||
70C98416253D4AC0003A9689 /* acl_rfc1035.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = acl_rfc1035.c; sourceTree = "<group>"; };
|
||||
70C98418253D4AD9003A9689 /* acl_rfc1035.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = acl_rfc1035.h; sourceTree = "<group>"; };
|
||||
@ -1102,6 +1104,7 @@
|
||||
0779D4701DBE070E00449438 /* string */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
70AA1BF128A383FB007E4B54 /* acl_localtime.c */,
|
||||
0779D4711DBE070E00449438 /* acl_alldig.c */,
|
||||
0779D4721DBE070E00449438 /* acl_basename.c */,
|
||||
0779D4731DBE070E00449438 /* acl_concatenate.c */,
|
||||
@ -1742,6 +1745,7 @@
|
||||
0779D5BB1DBE073000449438 /* acl_str2time.c in Sources */,
|
||||
0779D5BC1DBE073000449438 /* acl_strcasecmp.c in Sources */,
|
||||
0779D5BD1DBE073000449438 /* acl_strcasestr.c in Sources */,
|
||||
70AA1BF228A383FB007E4B54 /* acl_localtime.c in Sources */,
|
||||
0779D5BF1DBE073000449438 /* acl_dll.c in Sources */,
|
||||
0779D5C01DBE073000449438 /* acl_env.c in Sources */,
|
||||
0779D5C11DBE073000449438 /* acl_exec_command.c in Sources */,
|
||||
|
@ -11,6 +11,7 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "acl_define.h"
|
||||
#include <time.h>
|
||||
|
||||
@ -22,6 +23,17 @@ extern "C" {
|
||||
*/
|
||||
ACL_API time_t acl_str2time_t(const char *str);
|
||||
|
||||
/* acl_localtime.c */
|
||||
/**
|
||||
* 将整型的时间转换为指定的时间结构对象,类似于glibc中的 localtime_r(), 而 glibc
|
||||
* 实现的 localtime_r() 内部有锁, 会影响效率, 同时可能会造成fork()后的死锁问题,
|
||||
* 所以将 Redis 的代码中的实现放在此处.
|
||||
* @param t {const time_t*} 1970.1.1以来的时间值(秒级)
|
||||
* @param result {struct tm*} 存放转换结果对象
|
||||
* @return {struct tm *} 返回值非NULL表示转换成功,否则表示失败
|
||||
*/
|
||||
ACL_API struct tm *acl_localtime_r(const time_t *t, struct tm *result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -575,6 +575,9 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<File
|
||||
RelativePath=".\src\stdlib\string\acl_hex_code.c">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stdlib\string\acl_localtime.c">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stdlib\string\acl_mystring.c">
|
||||
</File>
|
||||
|
@ -775,6 +775,10 @@
|
||||
RelativePath=".\src\stdlib\string\acl_hex_code.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stdlib\string\acl_localtime.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stdlib\string\acl_mystring.c"
|
||||
>
|
||||
|
@ -305,6 +305,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
|
@ -425,6 +425,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
|
@ -511,6 +511,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
|
@ -419,6 +419,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
|
@ -511,6 +511,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
|
@ -422,6 +422,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
|
@ -515,6 +515,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
|
@ -431,6 +431,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
|
@ -515,6 +515,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
|
@ -431,6 +431,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
|
@ -511,6 +511,7 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_basename.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_concatenate.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_at.c" />
|
||||
<ClCompile Include=".\src\stdlib\string\acl_split_nameval.c" />
|
||||
@ -868,4 +869,4 @@
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
@ -431,6 +431,9 @@
|
||||
<ClCompile Include=".\src\stdlib\string\acl_hex_code.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_localtime.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include=".\src\stdlib\string\acl_mystring.c">
|
||||
<Filter>Source Files\stdlib\string</Filter>
|
||||
</ClCompile>
|
||||
@ -1494,4 +1497,4 @@
|
||||
<Filter>Source Files</Filter>
|
||||
</ResourceCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
@ -2,6 +2,7 @@
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "stdlib/acl_define.h"
|
||||
#include "stdlib/acl_mymalloc.h"
|
||||
#include "stdlib/acl_timeops.h"
|
||||
|
||||
/* #include <time.h> */
|
||||
#include <stdio.h>
|
||||
@ -670,21 +671,11 @@ int acl_open_log(const char *recipients, const char *logpre)
|
||||
void acl_logtime_fmt(char *buf, size_t size)
|
||||
{
|
||||
time_t now;
|
||||
#ifdef ACL_UNIX
|
||||
struct tm local_time;
|
||||
|
||||
(void) time (&now);
|
||||
(void) localtime_r(&now, &local_time);
|
||||
(void) acl_localtime_r(&now, &local_time);
|
||||
strftime(buf, size, "%Y/%m/%d %H:%M:%S", &local_time);
|
||||
#elif defined(ACL_WINDOWS)
|
||||
struct tm *local_time;
|
||||
|
||||
(void) time (&now);
|
||||
local_time = localtime(&now);
|
||||
strftime(buf, size, "%Y/%m/%d %H:%M:%S", local_time);
|
||||
#else
|
||||
# error "unknown OS type"
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ACL_UNIX
|
||||
|
185
lib_acl/src/stdlib/string/acl_localtime.c
Normal file
185
lib_acl/src/stdlib/string/acl_localtime.c
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Redis nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "StdAfx.h"
|
||||
#include <time.h>
|
||||
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "stdlib/acl_define.h"
|
||||
#include "thread/acl_pthread.h"
|
||||
#include "stdlib/acl_timeops.h"
|
||||
#endif
|
||||
|
||||
/* This is a safe version of localtime() which contains no locks and is
|
||||
* fork() friendly. Even the _r version of localtime() cannot be used safely
|
||||
* in Redis. Another thread may be calling localtime() while the main thread
|
||||
* forks(). Later when the child process calls localtime() again, for instance
|
||||
* in order to log something to the Redis log, it may deadlock: in the copy
|
||||
* of the address space of the forked process the lock will never be released.
|
||||
*
|
||||
* This function takes the timezone 'tz' as argument, and the 'dst' flag is
|
||||
* used to check if daylight saving time is currently in effect. The caller
|
||||
* of this function should obtain such information calling tzset() ASAP in the
|
||||
* main() function to obtain the timezone offset from the 'timezone' global
|
||||
* variable. To obtain the daylight information, if it is currently active or not,
|
||||
* one trick is to call localtime() in main() ASAP as well, and get the
|
||||
* information from the tm_isdst field of the tm structure. However the daylight
|
||||
* time may switch in the future for long running processes, so this information
|
||||
* should be refreshed at safe times.
|
||||
*
|
||||
* Note that this function does not work for dates < 1/1/1970, it is solely
|
||||
* designed to work with what time(NULL) may return, and to support Redis
|
||||
* logging of the dates, it's not really a complete implementation.
|
||||
*/
|
||||
|
||||
#if defined(ACL_UNIX)
|
||||
|
||||
static int is_leap_year(time_t year) {
|
||||
if (year % 4) {
|
||||
return 0; /* A year not divisible by 4 is not leap. */
|
||||
} else if (year % 100) {
|
||||
return 1; /* If div by 4 and not 100 is surely leap. */
|
||||
} else if (year % 400) {
|
||||
return 0; /* If div by 100 *and* not by 400 is not leap. */
|
||||
} else {
|
||||
return 1; /* If div by 100 and 400 is leap. */
|
||||
}
|
||||
}
|
||||
|
||||
static void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst)
|
||||
{
|
||||
const time_t secs_min = 60;
|
||||
const time_t secs_hour = 3600;
|
||||
const time_t secs_day = 3600*24;
|
||||
|
||||
t -= tz; /* Adjust for timezone. */
|
||||
t += 3600 * dst; /* Adjust for daylight time. */
|
||||
time_t days = t / secs_day; /* Days passed since epoch. */
|
||||
time_t seconds = t % secs_day; /* Remaining seconds. */
|
||||
|
||||
tmp->tm_isdst = dst;
|
||||
tmp->tm_hour = seconds / secs_hour;
|
||||
tmp->tm_min = (seconds % secs_hour) / secs_min;
|
||||
tmp->tm_sec = (seconds % secs_hour) % secs_min;
|
||||
|
||||
/* 1/1/1970 was a Thursday, that is, day 4 from the POV of the tm
|
||||
* structure where sunday = 0, so to calculate the day of the week
|
||||
* we have to add 4
|
||||
* and take the modulo by 7.
|
||||
*/
|
||||
tmp->tm_wday = (days + 4) % 7;
|
||||
|
||||
/* Calculate the current year. */
|
||||
tmp->tm_year = 1970;
|
||||
while(1) {
|
||||
/* Leap years have one day more. */
|
||||
time_t days_this_year = 365 + is_leap_year(tmp->tm_year);
|
||||
if (days_this_year > days) {
|
||||
break;
|
||||
}
|
||||
days -= days_this_year;
|
||||
tmp->tm_year++;
|
||||
}
|
||||
|
||||
tmp->tm_yday = days; /* Number of day of the current year. */
|
||||
|
||||
/* We need to calculate in which month and day of the month we are.
|
||||
* To do so we need to skip days according to how many days there are
|
||||
* in each month, and adjust for the leap year that has one more day
|
||||
* in February.
|
||||
*/
|
||||
int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
mdays[1] += is_leap_year(tmp->tm_year);
|
||||
|
||||
tmp->tm_mon = 0;
|
||||
while(days >= mdays[tmp->tm_mon]) {
|
||||
days -= mdays[tmp->tm_mon];
|
||||
tmp->tm_mon++;
|
||||
}
|
||||
|
||||
tmp->tm_mday = days + 1; /* Add 1 since our 'days' is zero-based. */
|
||||
tmp->tm_year -= 1900; /* Surprisingly tm_year is year-1900. */
|
||||
}
|
||||
|
||||
static int daylight_active;
|
||||
|
||||
static void timezone_init(void)
|
||||
{
|
||||
struct tm *aux;
|
||||
time_t t;
|
||||
|
||||
/* Obtain timezone and daylight info. */
|
||||
|
||||
tzset(); /* Now 'timezome' global is populated. */
|
||||
t = time(NULL);
|
||||
aux = localtime(&t);
|
||||
daylight_active = aux->tm_isdst;
|
||||
}
|
||||
|
||||
static acl_pthread_once_t once_control = ACL_PTHREAD_ONCE_INIT;
|
||||
|
||||
struct tm *acl_localtime_r(const time_t *t, struct tm *result)
|
||||
{
|
||||
if (acl_pthread_once(&once_control, timezone_init) != 0) {
|
||||
abort();
|
||||
}
|
||||
nolocks_localtime(result, *t, timezone, daylight_active);
|
||||
return result;
|
||||
}
|
||||
|
||||
#elif defined(ACL_WINDOWS)
|
||||
|
||||
struct tm *acl_localtime_r(const time_t *t, struct tm *result)
|
||||
{
|
||||
# if _MSC_VER >= 1500
|
||||
if (localtime_s(result, t) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
return result;
|
||||
# else
|
||||
struct tm *p = localtime(t);
|
||||
if (p == NULL) {
|
||||
return NULL;
|
||||
} else if (result) {
|
||||
memcpy(result, p, sizeof(struct tm));
|
||||
return result;
|
||||
} else {
|
||||
return p;
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct tm *acl_localtime_r(const time_t *t, struct tm *result)
|
||||
{
|
||||
return localtime_r(t, result);
|
||||
}
|
||||
|
||||
#endif
|
@ -386,36 +386,14 @@ const char* query::to_date(time_t t, string& out,
|
||||
fmt = "%Y-%m-%d %H:%M:%S";
|
||||
}
|
||||
|
||||
|
||||
struct tm* local_ptr;
|
||||
|
||||
#ifdef ACL_WINDOWS
|
||||
# ifdef __STDC_WANT_SECURE_LIB__
|
||||
|
||||
struct tm local;
|
||||
|
||||
if (localtime_s(&local, &t) != 0) {
|
||||
if (acl_localtime_r(&t, &local) == NULL) {
|
||||
logger_error("localtime_s failed, t: %ld", (long) t);
|
||||
return NULL;
|
||||
}
|
||||
local_ptr = &local;
|
||||
# else
|
||||
local_ptr = localtime(&t);
|
||||
if (local_ptr == NULL) {
|
||||
logger_error("localtime failed, t: %ld", (long) t);
|
||||
return NULL;
|
||||
}
|
||||
# endif
|
||||
#else
|
||||
|
||||
struct tm local;
|
||||
|
||||
if ((local_ptr = localtime_r(&t, &local)) == NULL) {
|
||||
logger_error("localtime_r failed, t: %ld", (long) t);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
if (strftime(buf, sizeof(buf), fmt, local_ptr) == 0) {
|
||||
if (strftime(buf, sizeof(buf), fmt, &local) == 0) {
|
||||
logger_error("strftime failed, t: %ld, fmt: %s",
|
||||
(long) t, fmt);
|
||||
return NULL;
|
||||
|
@ -315,26 +315,12 @@ void rfc822::mkdate_gmt(time_t t, char *buf, size_t size)
|
||||
|
||||
void rfc822::mkdate_cst(time_t t, char *buf, size_t size)
|
||||
{
|
||||
struct tm *p;
|
||||
long offset = 0;
|
||||
|
||||
#ifdef ACL_WINDOWS
|
||||
# if _MSC_VER >= 1500
|
||||
struct tm tm_buf;
|
||||
long s;
|
||||
p = &tm_buf;
|
||||
if (localtime_s(p, &t) != 0) {
|
||||
p = NULL;
|
||||
}
|
||||
# else
|
||||
p = localtime(&t);
|
||||
# endif
|
||||
#else
|
||||
struct tm tm_buf;
|
||||
p = localtime_r(&t, &tm_buf);
|
||||
#endif
|
||||
struct tm *p = acl_localtime_r(&t, &tm_buf);
|
||||
|
||||
buf[0] = 0;
|
||||
|
||||
if (p == NULL) {
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user