Implement strftime

This commit is contained in:
K. Lange 2018-03-01 10:57:16 +09:00 committed by Kevin Lange
parent b2a50cf325
commit eaa4bc0c4c
4 changed files with 197 additions and 25 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <stddef.h>
#include <sys/types.h>
struct tm {
@ -15,3 +16,4 @@ struct tm {
};
extern struct tm *localtime(const time_t *timep);
extern size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

195
libc/strftime.c Normal file
View File

@ -0,0 +1,195 @@
#include <stddef.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
static char * weekdays[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
static char * weekdays_short[] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
static char * months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
static char * months_short[] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
size_t strftime(char *s, size_t max, const char *fmt, const struct tm *tm) {
char * b = s;
size_t count = 0;
for (const char *f = fmt; *f; f++) {
if (*f != '%') {
count++;
*b++ = *f;
if (count == max) return b - s;
continue;
}
++f;
int _alte = 0;
int _alto = 0;
if (*f == 'E') {
_alte = 1;
++f;
} else if (*f == '0') {
_alto = 1;
++f;
}
switch (*f) {
case 'a':
b += sprintf(b, "%s", weekdays_short[tm->tm_wday]);
break;
case 'A':
b += sprintf(b, "%s", weekdays[tm->tm_wday]);
break;
case 'h':
case 'b':
b += sprintf(b, "%s", months_short[tm->tm_mon]);
break;
case 'B':
b += sprintf(b, "%s", months[tm->tm_mon]);
break;
case 'c':
b += sprintf(b, "%s %s %02d %02d:%02d:%02d %04d",
weekdays_short[tm->tm_wday],
months_short[tm->tm_mon],
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
tm->tm_year + 1900);
break;
case 'C':
b += sprintf(b, "%02d", (tm->tm_year + 1900) / 100);
break;
case 'd':
b += sprintf(b, "%02d", tm->tm_mday);
break;
case 'D':
b += sprintf(b, "%02d/%02d/%02d", tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
break;
case 'e':
b += sprintf(b, "%2d", tm->tm_mday);
break;
case 'F':
b += sprintf(b, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday);
break;
case 'H':
b += sprintf(b, "%02d", tm->tm_hour);
break;
case 'I':
b += sprintf(b, "%02d", tm->tm_hour == 0 ? 12 : (tm->tm_hour == 12 ? 12 : (tm->tm_hour % 12)));
break;
case 'j':
b += sprintf(b, "%03d", tm->tm_yday);
break;
case 'k':
b += sprintf(b, "%2d", tm->tm_hour);
break;
case 'l':
b += sprintf(b, "%2d", tm->tm_hour == 0 ? 12 : (tm->tm_hour == 12 ? 12 : (tm->tm_hour % 12)));
break;
case 'm':
b += sprintf(b, "%02d", tm->tm_mon+1);
break;
case 'M':
b += sprintf(b, "%02d", tm->tm_min);
break;
case 'n':
b += sprintf(b, "\n");
break;
case 'p':
b += sprintf(b, "%s", tm->tm_hour < 12 ? "AM" : "PM");
break;
case 'P':
b += sprintf(b, "%s", tm->tm_hour < 12 ? "am" : "pm");
break;
case 'r':
b += sprintf(b, "%02d:%02d:%02d %s",
tm->tm_hour == 0 ? 12 : (tm->tm_hour == 12 ? 12 : (tm->tm_hour % 12)),
tm->tm_min,
tm->tm_sec,
tm->tm_hour < 12 ? "AM" : "PM");
break;
case 'R':
b += sprintf(b, "%02d:%02d",
tm->tm_hour,
tm->tm_min);
break;
case 'S':
b += sprintf(b, "%02d", tm->tm_sec);
break;
case 't':
b += sprintf(b, "\t");
break;
case 'T':
b += sprintf(b, "%02d:%02d:%02d",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
break;
case 'u':
b += sprintf(b, "%d", tm->tm_wday == 0 ? 7 : tm->tm_wday);
break;
case 'w':
b += sprintf(b, "%d", tm->tm_wday);
break;
case 'x':
b += sprintf(b, "%02d/%02d/%02d", tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
break;
case 'X':
b += sprintf(b, "%02d:%02d:%02d",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
break;
case 'y':
b += sprintf(b, "%02d", tm->tm_year % 100);
break;
case 'Y':
b += sprintf(b, "%04d", tm->tm_year + 1900);
break;
case 'z':
b += sprintf(b, "+0000");
break;
case 'Z':
b += sprintf(b, "UTC");
break;
case '%':
b += sprintf(b, "%");
break;
case 'V':
case 'W':
case 's':
case 'U':
case 'G':
case 'g':
b += sprintf(b, "<%c unsupported>", *f);
break;
}
}
/* Ensure the buffer ends in a null */
*b = '\0';
return b - s;
}

18
ls.c
View File

@ -225,29 +225,11 @@ static void print_entry_long(int * widths, struct tfile * file) {
char time_buf[80];
struct tm * timeinfo = localtime(&file->statbuf.st_mtime);
#if 0
if (timeinfo->tm_year == this_year) {
strftime(time_buf, 80, "%b %d %H:%M", timeinfo);
} else {
strftime(time_buf, 80, "%b %d %Y", timeinfo);
}
#else
static char * months[] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
if (timeinfo->tm_year == this_year) {
sprintf(time_buf, "%s %02d %02d:%02d",
months[timeinfo->tm_mon],
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min);
} else {
sprintf(time_buf, "%s %02d %04d",
months[timeinfo->tm_mon],
timeinfo->tm_mday,
timeinfo->tm_year+1900);
}
#endif
printf("%s ", time_buf);
/* Print the file name */

7
sh.c
View File

@ -120,18 +120,11 @@ void draw_prompt(int ret) {
gettimeofday(&now, NULL); //time(NULL);
timeinfo = localtime((time_t *)&now.tv_sec);
#if 0
/* Format the date and time for prompt display */
char date_buffer[80];
strftime(date_buffer, 80, "%m/%d", timeinfo);
char time_buffer[80];
strftime(time_buffer, 80, "%H:%M:%S", timeinfo);
#else
char date_buffer[80];
sprintf(date_buffer, "%02d/%02d", timeinfo->tm_mon+1, timeinfo->tm_mday);
char time_buffer[80];
sprintf(time_buffer, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
#endif
/* Print the working directory in there, too */
getcwd(cwd, 512);