comparison src/memline.c @ 19396:a961efb326e5 v8.2.0256

patch 8.2.0256: time and timer related code is spread out Commit: https://github.com/vim/vim/commit/0a8fed6231c84e4e1b3a7dd6c0d95d3f98207fe0 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Feb 14 13:22:17 2020 +0100 patch 8.2.0256: time and timer related code is spread out Problem: Time and timer related code is spread out. Solution: Move time and timer related code to a new file. (Yegappan Lakshmanan, closes #5604)
author Bram Moolenaar <Bram@vim.org>
date Fri, 14 Feb 2020 13:30:05 +0100
parents 2ef19eed524a
children e529690f27bc
comparison
equal deleted inserted replaced
19395:eb5ef6f5f58b 19396:a961efb326e5
2077 } 2077 }
2078 else 2078 else
2079 dict_add_string(d, "error", (char_u *)"Cannot open file"); 2079 dict_add_string(d, "error", (char_u *)"Cannot open file");
2080 } 2080 }
2081 #endif 2081 #endif
2082
2083 /*
2084 * Cache of the current timezone name as retrieved from TZ, or an empty string
2085 * where unset, up to 64 octets long including trailing null byte.
2086 */
2087 #if defined(HAVE_LOCALTIME_R) && defined(HAVE_TZSET)
2088 static char tz_cache[64];
2089 #endif
2090
2091 /*
2092 * Call either localtime(3) or localtime_r(3) from POSIX libc time.h, with the
2093 * latter version preferred for reentrancy.
2094 *
2095 * If we use localtime_r(3) and we have tzset(3) available, check to see if the
2096 * environment variable TZ has changed since the last run, and call tzset(3) to
2097 * update the global timezone variables if it has. This is because the POSIX
2098 * standard doesn't require localtime_r(3) implementations to do that as it
2099 * does with localtime(3), and we don't want to call tzset(3) every time.
2100 */
2101 struct tm *
2102 vim_localtime(
2103 const time_t *timep, // timestamp for local representation
2104 struct tm *result UNUSED) // pointer to caller return buffer
2105 {
2106 #ifdef HAVE_LOCALTIME_R
2107 # ifdef HAVE_TZSET
2108 char *tz; // pointer for TZ environment var
2109
2110 tz = (char *)mch_getenv((char_u *)"TZ");
2111 if (tz == NULL)
2112 tz = "";
2113 if (STRNCMP(tz_cache, tz, sizeof(tz_cache) - 1) != 0)
2114 {
2115 tzset();
2116 vim_strncpy((char_u *)tz_cache, (char_u *)tz, sizeof(tz_cache) - 1);
2117 }
2118 # endif // HAVE_TZSET
2119 return localtime_r(timep, result);
2120 #else
2121 return localtime(timep);
2122 #endif // HAVE_LOCALTIME_R
2123 }
2124
2125 /*
2126 * Replacement for ctime(), which is not safe to use.
2127 * Requires strftime(), otherwise returns "(unknown)".
2128 * If "thetime" is invalid returns "(invalid)". Never returns NULL.
2129 * When "add_newline" is TRUE add a newline like ctime() does.
2130 * Uses a static buffer.
2131 */
2132 char *
2133 get_ctime(time_t thetime, int add_newline)
2134 {
2135 static char buf[50];
2136 #ifdef HAVE_STRFTIME
2137 struct tm tmval;
2138 struct tm *curtime;
2139
2140 curtime = vim_localtime(&thetime, &tmval);
2141 // MSVC returns NULL for an invalid value of seconds.
2142 if (curtime == NULL)
2143 vim_strncpy((char_u *)buf, (char_u *)_("(Invalid)"), sizeof(buf) - 1);
2144 else
2145 {
2146 (void)strftime(buf, sizeof(buf) - 1, _("%a %b %d %H:%M:%S %Y"),
2147 curtime);
2148 # ifdef MSWIN
2149 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2150 {
2151 char_u *to_free = NULL;
2152 int len;
2153
2154 acp_to_enc((char_u *)buf, (int)strlen(buf), &to_free, &len);
2155 if (to_free != NULL)
2156 {
2157 STRCPY(buf, to_free);
2158 vim_free(to_free);
2159 }
2160 }
2161 # endif
2162 }
2163 #else
2164 STRCPY(buf, "(unknown)");
2165 #endif
2166 if (add_newline)
2167 STRCAT(buf, "\n");
2168 return buf;
2169 }
2170 2082
2171 /* 2083 /*
2172 * Give information about an existing swap file. 2084 * Give information about an existing swap file.
2173 * Returns timestamp (0 when unknown). 2085 * Returns timestamp (0 when unknown).
2174 */ 2086 */