245
243
char * ascii_day(char *, int);
246
int center_str(const char* src, char* dest, size_t dest_size, int width);
247
void center(const char *, int, int);
244
int center_str(const char* src, char* dest, size_t dest_size, size_t width);
245
void center(const char *, size_t, int);
248
246
void day_array(int, int, int, int *);
249
247
int day_in_week(int, int, int);
250
248
int day_in_year(int, int, int);
256
254
void trim_trailing_spaces(char *);
257
255
void usage(void);
258
256
void headers_init(void);
259
extern char *__progname;
262
259
main(int argc, char **argv) {
263
260
struct tm *local_time;
265
262
int ch, day, month, year, yflag;
267
263
int num_months = NUM_MONTHS;
270
if ((p = strrchr(progname, '/')) != NULL)
272
__progname = progname;
274
265
setlocale(LC_ALL, "");
275
266
bindtextdomain(PACKAGE, LOCALEDIR);
276
267
textdomain(PACKAGE);
312
303
POSIX: 19971201 + 7 -1 = 0
315
int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
307
union { unsigned int word; char *string; } val;
308
val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
316
311
wfd = day_in_week(wfd % 100, (wfd / 100) % 100, wfd / (100 * 100));
317
312
weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
424
414
space_left = sizeof(day_headings) - (cur_dh - day_headings);
425
415
if(space_left <= 2)
427
cur_dh += center_str(weekday(wd), cur_dh, space_left, 2);
417
cur_dh += center_str(nl_langinfo(ABDAY_1+wd), cur_dh, space_left, 2);
430
420
strcat(cur_j_dh++, " ");
431
421
space_left = sizeof(j_day_headings) - (cur_j_dh - j_day_headings);
432
422
if(space_left <= 3)
434
cur_j_dh += center_str(weekday(wd), cur_j_dh, space_left, 3);
424
cur_j_dh += center_str(nl_langinfo(ABDAY_1+wd), cur_j_dh, space_left, 3);
439
for (i = 0; i < 12; i++) {
440
#ifdef HAVE_LANGINFO_H
427
for (i = 0; i < 12; i++)
441
428
full_month[i] = nl_langinfo(MON_1+i);
443
full_month[i] = _time_info->full_month[i];
753
/* replace non printable chars.
754
* return 1 if replacement made, 0 otherwise */
755
int wc_ensure_printable(wchar_t* wchars)
758
wchar_t* wc = wchars;
760
if (!iswprint((wint_t) *wc)) {
769
/* truncate wchar string to width cells.
770
* returns number of cells used. */
771
size_t wc_truncate(wchar_t* wchars, size_t width, size_t minchars)
775
while (*(wchars+wc)) {
776
cells = wcswidth(wchars, wc+1);
778
if (wc >= minchars) {
790
736
* Center string, handling multibyte characters appropriately.
791
737
* In addition if the string is too large for the width it's truncated.
792
738
* The number of trailing spaces may be 1 less than the number of leading spaces.
795
center_str(const char* src, char* dest, size_t dest_size, int width)
741
center_str(const char* src, char* dest, size_t dest_size, size_t width)
798
wchar_t str_wc[FMT_ST_CHARS];
800
char str[FMT_ST_CHARS];
801
const char* str_to_print=src;
802
int used, spaces, wc_conversion=0, wc_enabled=0;
805
if (mbstowcs(str_wc, src, ARRAY_SIZE(str_wc)) > 0) {
806
str_wc[ARRAY_SIZE(str_wc)-1]=L'\0';
808
wc_conversion = wc_ensure_printable(str_wc);
809
used = wcswidth(str_wc, ARRAY_SIZE(str_wc));
815
if (wc_conversion || used > width) {
819
used = wc_truncate(str_wc, width, 1);
820
wcstombs(str, str_wc, ARRAY_SIZE(str));
823
memcpy(str, src, width);
828
spaces = width - used;
829
spaces = ( spaces < 0 ? 0 : spaces );
831
return snprintf(dest, dest_size, "%*s%s%*s",
832
spaces / 2 + spaces % 2, "",
743
return mbsalign(src, dest, dest_size, &width,
744
MBS_ALIGN_CENTER, MBA_UNIBYTE_FALLBACK);
838
748
center(str, len, separate)
843
753
char lineout[FMT_ST_CHARS];