A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1#include <sys/types.h>
2#include <time.h>
3#include "plugin.h"
4
5static const char sweekdays [7] [4] = {
6 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
7};
8static const char weekdays [7] [10] = {
9 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
10};
11static const char smonths [12] [4] = {
12 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
13 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
14};
15static const char* months [12] = {
16 "January", "February", "March", "April", smonths[5-1], "June",
17 "July", "August", "September", "October", "November", "December"
18};
19static const char ampm [4] [3] = {
20 "am", "pm",
21 "AM", "PM"
22};
23
24static void i2a ( char* dest,unsigned long x )
25{
26 int div = 10;
27 *dest++ = x/div + '0';
28 *dest++ = x%div + '0';
29 *dest++ = '\0';
30}
31
32size_t strftime ( char* dst, size_t max, const char* format, const struct tm* tm )
33{
34 char* p = dst;
35 const char* src;
36 unsigned long no;
37 char buf [5];
38#if CONFIG_RTC
39 char sbuf[101];
40#endif
41
42 if (!max) return 0;
43 for ( ; *format != '\0'; format++ ) {
44 if (*format == '%') {
45 if (*++format == '%') {
46 *p++ = '%';
47 }
48 else
49again:
50 switch (*format) {
51// case '%': *p++ = '%'; break; // reduce size of jump table
52 case 'n': *p++ = '\n'; break;
53 case 't': *p++ = '\t'; break;
54 case 'O': case 'E': ++format; goto again;
55 case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf;
56 case 'r': src = "%I:%M:%S %p"; goto _strf;
57 case 'R': src = "%H:%M"; goto _strf;
58 case 'x': src = "%b %a %d"; goto _strf;
59 case 'X': src = "%k:%M:%S"; goto _strf;
60 case 'D': src = "%m/%d/%y"; goto _strf;
61 case 'F': src = "%Y-%m-%d"; goto _strf;
62 case 'T': src = "%H:%M:%S";
63 _strf: p += strftime (p, (size_t)(dst+max-p), src, tm); break;
64 case 'a': src = sweekdays [tm->tm_wday]; goto _str;
65 case 'A': src = weekdays [tm->tm_wday]; goto _str;
66 case 'h':
67 case 'b': src = smonths [tm->tm_mon]; goto _str;
68 case 'B': src = months [tm->tm_mon]; goto _str;
69 case 'p': src = ampm [tm->tm_hour > 12 ? 3 : 2]; goto _str;
70 case 'P': src = ampm [tm->tm_hour > 12 ? 1 : 0]; goto _str;
71 case 'C': no = tm->tm_year/100 + 19; goto _no;
72 case 'd': no = tm->tm_mday; goto _no;
73 case 'e': no = tm->tm_mday; goto _nos;
74 case 'H': no = tm->tm_hour; goto _no;
75 case 'I': no = tm->tm_hour % 12; goto _no;
76 case 'j': no = tm->tm_yday; goto _no;
77 case 'k': no = tm->tm_hour; goto _nos;
78 case 'l': no = tm->tm_hour % 12; goto _nos;
79 case 'm': no = tm->tm_mon + 1; goto _no;
80 case 'M': no = tm->tm_min; goto _no;
81 case 'S': no = tm->tm_sec; goto _no;
82 case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no;
83 case 'w': no = tm->tm_wday; goto _no;
84 case 'U': no = (tm->tm_yday - tm->tm_wday + 7) / 7; goto _no;
85 case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no;
86 case 's': {
87#if CONFIG_RTC
88 time_t t = rb->mktime((struct tm*)tm);
89 char* c;
90 sbuf[100]=0;
91 for (c=sbuf+99; c>sbuf; --c) {
92 *c=(t%10)+'0';
93 t/=10;
94 if (!t) break;
95 }
96#else
97 char* c = "0";
98#endif
99 src=c;
100 goto _str;
101 }
102 case 'Z':
103#ifdef WANT_TZFILE_PARSER
104 tzset(); src = tzname[0];
105#else
106 src = "[unknown timezone]";
107#endif
108 goto _str;
109 case 'Y': i2a ( buf+0, (unsigned int)(tm->tm_year / 100 + 19) );
110 i2a ( buf+2, (unsigned int)(tm->tm_year % 100) );
111 src = buf;
112 goto _str;
113 case 'y': no = tm->tm_year % 100; goto _no;
114 _no: i2a ( buf, no ); /* append number 'no' */
115 src = buf;
116 goto _str;
117 _nos: i2a ( buf, no ); /* the same, but '0'->' ' */
118 if (buf[0] == '0')
119 buf[0] = ' ';
120 src = buf;
121 _str: while (*src && p < dst+max) /* append string */
122 *p++ = *src++;
123 break;
124 };
125 } else {
126 *p++ = *format;
127 }
128
129 if (p >= dst+max)
130 break;
131 }
132
133 if ((size_t)(p-dst)>=max) {
134 if (max) p[-1]=0;
135 } else
136 *p = '\0';
137 return p - dst;
138}
139
140