Fix some greedy sed changes in imported code. Also provide a sys/types.h for compatib...
[kugel-rb.git] / apps / plugins / lua / strftime.c
blob681d4be1cf3a4e432c70d35be95e59c822b62115
1 #include <sys/types.h>
2 #include <time.h>
3 #include "plugin.h"
5 static const char sweekdays [7] [4] = {
6 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
7 };
8 static const char weekdays [7] [10] = {
9 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
11 static const char smonths [12] [4] = {
12 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
13 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
15 static const char* months [12] = {
16 "January", "February", "March", "April", smonths[5-1], "June",
17 "July", "August", "September", "October", "November", "December"
19 static const char ampm [4] [3] = {
20 "am", "pm",
21 "AM", "PM"
24 static void i2a ( char* dest,unsigned long x )
26 int div = 10;
27 *dest++ = x/div + '0';
28 *dest++ = x%div + '0';
29 *dest++ = '\0';
32 size_t strftime ( char* dst, size_t max, const char* format, const struct tm* tm )
34 char* p = dst;
35 const char* src;
36 unsigned long no;
37 char buf [5];
39 if (!max) return 0;
40 for ( ; *format != '\0'; format++ ) {
41 if (*format == '%') {
42 if (*++format == '%') {
43 *p++ = '%';
45 else
46 again:
47 switch (*format) {
48 // case '%': *p++ = '%'; break; // reduce size of jump table
49 case 'n': *p++ = '\n'; break;
50 case 't': *p++ = '\t'; break;
51 case 'O': case 'E': ++format; goto again;
52 case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf;
53 case 'r': src = "%I:%M:%S %p"; goto _strf;
54 case 'R': src = "%H:%M"; goto _strf;
55 case 'x': src = "%b %a %d"; goto _strf;
56 case 'X': src = "%k:%M:%S"; goto _strf;
57 case 'D': src = "%m/%d/%y"; goto _strf;
58 case 'T': src = "%H:%M:%S";
59 _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 = weekdays [tm->tm_wday]; goto _str;
62 case 'h':
63 case 'b': src = smonths [tm->tm_mon]; goto _str;
64 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 ? 1 : 0]; goto _str;
67 case 'C': no = tm->tm_year/100 + 19; goto _no;
68 case 'd': no = tm->tm_mday; goto _no;
69 case 'e': no = tm->tm_mday; goto _nos;
70 case 'H': no = tm->tm_hour; goto _no;
71 case 'I': no = tm->tm_hour % 12; goto _no;
72 case 'j': no = tm->tm_yday; goto _no;
73 case 'k': no = tm->tm_hour; goto _nos;
74 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_min; goto _no;
77 case 'S': no = tm->tm_sec; goto _no;
78 case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no;
79 case 'w': no = tm->tm_wday; goto _no;
80 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 's': {
83 time_t t =
84 #if CONFIG_RTC
85 rb->mktime((struct tm*)tm)
86 #else
88 #endif
90 char buf[101];
91 char* c;
92 buf[100]=0;
93 for (c=buf+99; c>buf; --c) {
94 *c=(t%10)+'0';
95 t/=10;
96 if (!t) break;
98 src=c;
99 goto _str;
101 case 'Z':
102 #ifdef WANT_TZFILE_PARSER
103 tzset(); src = tzname[0];
104 #else
105 src = "[unknown timezone]";
106 #endif
107 goto _str;
108 case 'Y': i2a ( buf+0, (unsigned int)(tm->tm_year / 100 + 19) );
109 i2a ( buf+2, (unsigned int)(tm->tm_year % 100) );
110 src = buf;
111 goto _str;
112 case 'y': no = tm->tm_year % 100; goto _no;
113 _no: i2a ( buf, no ); /* append number 'no' */
114 src = buf;
115 goto _str;
116 _nos: i2a ( buf, no ); /* the same, but '0'->' ' */
117 if (buf[0] == '0')
118 buf[0] = ' ';
119 src = buf;
120 _str: while (*src && p < dst+max) /* append string */
121 *p++ = *src++;
122 break;
124 } else {
125 *p++ = *format;
128 if (p >= dst+max)
129 break;
132 *p = '\0';
133 return p - dst;