diff options
author | Ron Yorston <rmy@pobox.com> | 2018-12-16 15:14:39 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-12-16 15:14:39 +0000 |
commit | 6b6c55e16f121976334b857a646d82c2fec0a07b (patch) | |
tree | 217422ddb4689f77a7575366d61589130720c99a | |
parent | 65189dacb44708743313c81ec1ccaff87a68be8d (diff) | |
download | busybox-w32-6b6c55e16f121976334b857a646d82c2fec0a07b.tar.gz busybox-w32-6b6c55e16f121976334b857a646d82c2fec0a07b.tar.bz2 busybox-w32-6b6c55e16f121976334b857a646d82c2fec0a07b.zip |
win32: add support for %T to strftime
Add the %T format specifier (same as %H:%M:%S) to our emulation
of strftime.
Rewrite so that common code to replace a format specifier with a
string is shared.
-rw-r--r-- | win32/mingw.c | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index d1da4ee84..15f94d86c 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -1039,45 +1039,42 @@ int mingw_unlink(const char *pathname) | |||
1039 | size_t mingw_strftime(char *buf, size_t max, const char *format, const struct tm *tm) | 1039 | size_t mingw_strftime(char *buf, size_t max, const char *format, const struct tm *tm) |
1040 | { | 1040 | { |
1041 | size_t ret; | 1041 | size_t ret; |
1042 | char day[3]; | 1042 | char buffer[64]; |
1043 | const char *replace; | ||
1043 | char *t; | 1044 | char *t; |
1044 | char *fmt, *newfmt; | 1045 | char *fmt; |
1045 | struct tm tm2; | 1046 | struct tm tm2; |
1046 | int m; | ||
1047 | 1047 | ||
1048 | /* | 1048 | /* |
1049 | * Emulate the some formats that Windows' strftime lacks. | 1049 | * Emulate some formats that Windows' strftime lacks. |
1050 | * - '%e' day of the month with space padding | 1050 | * - '%e' day of the month with space padding |
1051 | * - '%s' number of seconds since the Unix epoch | 1051 | * - '%s' number of seconds since the Unix epoch |
1052 | * - '%T' same as %H:%M:%S | ||
1052 | * - '%z' timezone offset | 1053 | * - '%z' timezone offset |
1053 | * Also, permit the '-' modifier to omit padding. Windows uses '#'. | 1054 | * Also, permit the '-' modifier to omit padding. Windows uses '#'. |
1054 | */ | 1055 | */ |
1055 | fmt = xstrdup(format); | 1056 | fmt = xstrdup(format); |
1056 | for ( t=fmt; *t; ++t ) { | 1057 | for ( t=fmt; *t; ++t ) { |
1057 | if ( *t == '%' ) { | 1058 | if ( *t == '%' ) { |
1059 | replace = NULL; | ||
1058 | if ( t[1] == 'e' ) { | 1060 | if ( t[1] == 'e' ) { |
1059 | if ( tm->tm_mday >= 0 && tm->tm_mday <= 99 ) { | 1061 | if ( tm->tm_mday >= 0 && tm->tm_mday <= 99 ) { |
1060 | sprintf(day, "%2d", tm->tm_mday); | 1062 | sprintf(buffer, "%2d", tm->tm_mday); |
1061 | } | 1063 | } |
1062 | else { | 1064 | else { |
1063 | strcpy(day, " "); | 1065 | strcpy(buffer, " "); |
1064 | } | 1066 | } |
1065 | memcpy(t++, day, 2); | 1067 | replace = buffer; |
1066 | } | 1068 | } |
1067 | else if ( t[1] == 's' ) { | 1069 | else if ( t[1] == 's' ) { |
1068 | *t = '\0'; | ||
1069 | m = t - fmt; | ||
1070 | tm2 = *tm; | 1070 | tm2 = *tm; |
1071 | newfmt = xasprintf("%s%d%s", fmt, (int)mktime(&tm2), t+2); | 1071 | sprintf(buffer, "%d", (int)mktime(&tm2)); |
1072 | free(fmt); | 1072 | replace = buffer; |
1073 | t = newfmt + m + 1; | 1073 | } |
1074 | fmt = newfmt; | 1074 | else if ( t[1] == 'T' ) { |
1075 | replace = "%H:%M:%S"; | ||
1075 | } | 1076 | } |
1076 | else if ( t[1] == 'z' ) { | 1077 | else if ( t[1] == 'z' ) { |
1077 | char buffer[16] = ""; | ||
1078 | |||
1079 | *t = '\0'; | ||
1080 | m = t - fmt; | ||
1081 | _tzset(); | 1078 | _tzset(); |
1082 | if ( tm->tm_isdst >= 0 ) { | 1079 | if ( tm->tm_isdst >= 0 ) { |
1083 | int offset = (int)_timezone - (tm->tm_isdst > 0 ? 3600 : 0); | 1080 | int offset = (int)_timezone - (tm->tm_isdst > 0 ? 3600 : 0); |
@@ -1095,10 +1092,10 @@ size_t mingw_strftime(char *buf, size_t max, const char *format, const struct tm | |||
1095 | min = (offset % 3600) / 60; | 1092 | min = (offset % 3600) / 60; |
1096 | sprintf(buffer+1, "%04d", hr*100 + min); | 1093 | sprintf(buffer+1, "%04d", hr*100 + min); |
1097 | } | 1094 | } |
1098 | newfmt = xasprintf("%s%s%s", fmt, buffer, t+2); | 1095 | else { |
1099 | free(fmt); | 1096 | buffer[0] = '\0'; |
1100 | t = newfmt + m + 1; | 1097 | } |
1101 | fmt = newfmt; | 1098 | replace = buffer; |
1102 | } | 1099 | } |
1103 | else if ( t[1] == '-' && t[2] != '\0' && | 1100 | else if ( t[1] == '-' && t[2] != '\0' && |
1104 | strchr("dHIjmMSUwWyY", t[2]) ) { | 1101 | strchr("dHIjmMSUwWyY", t[2]) ) { |
@@ -1108,6 +1105,18 @@ size_t mingw_strftime(char *buf, size_t max, const char *format, const struct tm | |||
1108 | else if ( t[1] != '\0' ) { | 1105 | else if ( t[1] != '\0' ) { |
1109 | ++t; | 1106 | ++t; |
1110 | } | 1107 | } |
1108 | |||
1109 | if (replace) { | ||
1110 | int m; | ||
1111 | char *newfmt; | ||
1112 | |||
1113 | *t = '\0'; | ||
1114 | m = t - fmt; | ||
1115 | newfmt = xasprintf("%s%s%s", fmt, replace, t+2); | ||
1116 | free(fmt); | ||
1117 | t = newfmt + m + strlen(replace); | ||
1118 | fmt = newfmt; | ||
1119 | } | ||
1111 | } | 1120 | } |
1112 | } | 1121 | } |
1113 | 1122 | ||