diff options
Diffstat (limited to 'apps/plugins/lua')
-rw-r--r-- | apps/plugins/lua/strftime.c | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/apps/plugins/lua/strftime.c b/apps/plugins/lua/strftime.c index 681d4be1cf..c6152bf492 100644 --- a/apps/plugins/lua/strftime.c +++ b/apps/plugins/lua/strftime.c | |||
@@ -45,16 +45,17 @@ size_t strftime ( char* dst, size_t max, const char* format, const struct tm* t | |||
45 | else | 45 | else |
46 | again: | 46 | again: |
47 | switch (*format) { | 47 | switch (*format) { |
48 | // case '%': *p++ = '%'; break; // reduce size of jump table | 48 | // case '%': *p++ = '%'; break; // reduce size of jump table |
49 | case 'n': *p++ = '\n'; break; | 49 | case 'n': *p++ = '\n'; break; |
50 | case 't': *p++ = '\t'; break; | 50 | case 't': *p++ = '\t'; break; |
51 | case 'O': case 'E': ++format; goto again; | 51 | case 'O': case 'E': ++format; goto again; |
52 | case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf; | 52 | case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf; |
53 | case 'r': src = "%I:%M:%S %p"; goto _strf; | 53 | case 'r': src = "%I:%M:%S %p"; goto _strf; |
54 | case 'R': src = "%H:%M"; goto _strf; | 54 | case 'R': src = "%H:%M"; goto _strf; |
55 | case 'x': src = "%b %a %d"; goto _strf; | 55 | case 'x': src = "%b %a %d"; goto _strf; |
56 | case 'X': src = "%k:%M:%S"; goto _strf; | 56 | case 'X': src = "%k:%M:%S"; goto _strf; |
57 | case 'D': src = "%m/%d/%y"; goto _strf; | 57 | case 'D': src = "%m/%d/%y"; goto _strf; |
58 | case 'F': src = "%Y-%m-%d"; goto _strf; | ||
58 | case 'T': src = "%H:%M:%S"; | 59 | case 'T': src = "%H:%M:%S"; |
59 | _strf: p += strftime (p, (size_t)(dst+max-p), src, tm); break; | 60 | _strf: p += strftime (p, (size_t)(dst+max-p), src, tm); break; |
60 | case 'a': src = sweekdays [tm->tm_wday]; goto _str; | 61 | case 'a': src = sweekdays [tm->tm_wday]; goto _str; |
@@ -64,37 +65,35 @@ again: | |||
64 | case 'B': src = months [tm->tm_mon]; goto _str; | 65 | case 'B': src = months [tm->tm_mon]; goto _str; |
65 | case 'p': src = ampm [tm->tm_hour > 12 ? 3 : 2]; goto _str; | 66 | case 'p': src = ampm [tm->tm_hour > 12 ? 3 : 2]; goto _str; |
66 | case 'P': src = ampm [tm->tm_hour > 12 ? 1 : 0]; goto _str; | 67 | case 'P': src = ampm [tm->tm_hour > 12 ? 1 : 0]; goto _str; |
67 | case 'C': no = tm->tm_year/100 + 19; goto _no; | 68 | case 'C': no = tm->tm_year/100 + 19; goto _no; |
68 | case 'd': no = tm->tm_mday; goto _no; | 69 | case 'd': no = tm->tm_mday; goto _no; |
69 | case 'e': no = tm->tm_mday; goto _nos; | 70 | case 'e': no = tm->tm_mday; goto _nos; |
70 | case 'H': no = tm->tm_hour; goto _no; | 71 | case 'H': no = tm->tm_hour; goto _no; |
71 | case 'I': no = tm->tm_hour % 12; goto _no; | 72 | case 'I': no = tm->tm_hour % 12; goto _no; |
72 | case 'j': no = tm->tm_yday; goto _no; | 73 | case 'j': no = tm->tm_yday; goto _no; |
73 | case 'k': no = tm->tm_hour; goto _nos; | 74 | case 'k': no = tm->tm_hour; goto _nos; |
74 | case 'l': no = tm->tm_hour % 12; goto _nos; | 75 | case 'l': no = tm->tm_hour % 12; goto _nos; |
75 | case 'm': no = tm->tm_mon + 1; goto _no; | 76 | case 'm': no = tm->tm_mon + 1; goto _no; |
76 | case 'M': no = tm->tm_min; goto _no; | 77 | case 'M': no = tm->tm_min; goto _no; |
77 | case 'S': no = tm->tm_sec; goto _no; | 78 | case 'S': no = tm->tm_sec; goto _no; |
78 | case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no; | 79 | case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no; |
79 | case 'w': no = tm->tm_wday; goto _no; | 80 | case 'w': no = tm->tm_wday; goto _no; |
80 | case 'U': no = (tm->tm_yday - tm->tm_wday + 7) / 7; goto _no; | 81 | case 'U': no = (tm->tm_yday - tm->tm_wday + 7) / 7; goto _no; |
81 | case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no; | 82 | case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no; |
82 | case 's': { | 83 | case 's': { |
83 | time_t t = | ||
84 | #if CONFIG_RTC | 84 | #if CONFIG_RTC |
85 | rb->mktime((struct tm*)tm) | 85 | time_t t = rb->mktime((struct tm*)tm); |
86 | #else | 86 | char sbuf[101]; |
87 | 0 | ||
88 | #endif | ||
89 | ; | ||
90 | char buf[101]; | ||
91 | char* c; | 87 | char* c; |
92 | buf[100]=0; | 88 | sbuf[100]=0; |
93 | for (c=buf+99; c>buf; --c) { | 89 | for (c=sbuf+99; c>sbuf; --c) { |
94 | *c=(t%10)+'0'; | 90 | *c=(t%10)+'0'; |
95 | t/=10; | 91 | t/=10; |
96 | if (!t) break; | 92 | if (!t) break; |
97 | } | 93 | } |
94 | #else | ||
95 | char* c = "0"; | ||
96 | #endif | ||
98 | src=c; | 97 | src=c; |
99 | goto _str; | 98 | goto _str; |
100 | } | 99 | } |
@@ -109,15 +108,15 @@ again: | |||
109 | i2a ( buf+2, (unsigned int)(tm->tm_year % 100) ); | 108 | i2a ( buf+2, (unsigned int)(tm->tm_year % 100) ); |
110 | src = buf; | 109 | src = buf; |
111 | goto _str; | 110 | goto _str; |
112 | case 'y': no = tm->tm_year % 100; goto _no; | 111 | case 'y': no = tm->tm_year % 100; goto _no; |
113 | _no: i2a ( buf, no ); /* append number 'no' */ | 112 | _no: i2a ( buf, no ); /* append number 'no' */ |
114 | src = buf; | 113 | src = buf; |
115 | goto _str; | 114 | goto _str; |
116 | _nos: i2a ( buf, no ); /* the same, but '0'->' ' */ | 115 | _nos: i2a ( buf, no ); /* the same, but '0'->' ' */ |
117 | if (buf[0] == '0') | 116 | if (buf[0] == '0') |
118 | buf[0] = ' '; | 117 | buf[0] = ' '; |
119 | src = buf; | 118 | src = buf; |
120 | _str: while (*src && p < dst+max) /* append string */ | 119 | _str: while (*src && p < dst+max) /* append string */ |
121 | *p++ = *src++; | 120 | *p++ = *src++; |
122 | break; | 121 | break; |
123 | }; | 122 | }; |
@@ -129,7 +128,10 @@ again: | |||
129 | break; | 128 | break; |
130 | } | 129 | } |
131 | 130 | ||
132 | *p = '\0'; | 131 | if ((size_t)(p-dst)>=max) { |
132 | if (max) p[-1]=0; | ||
133 | } else | ||
134 | *p = '\0'; | ||
133 | return p - dst; | 135 | return p - dst; |
134 | } | 136 | } |
135 | 137 | ||