tokei-C/main.c
2025-02-18 21:04:02 +00:00

56 lines
1.6 KiB
C

#include <time.h>
#include <unistd.h>
#include <string.h>
static const char template[] =
"📅 \x00\x00\x00\x00年\x00\x00月\x00\x00日 \x00\x00時\x00\x00分\x00\x00秒 \x00\x00\x00\x00\x00\x00\x00\x00\x00 \n";
static const char digits[] =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899";
static const char *const weekdays[] = {
"\xe6\x97\xa5\xe6\x9b\x9c\xe6\x97\xa5", // 日曜日
"\xe6\x9c\x88\xe6\x9b\x9c\xe6\x97\xa5", // 月曜日
"\xe7\x81\xab\xe6\x9b\x9c\xe6\x97\xa5", // 火曜日
"\xe6\xb0\xb4\xe6\x9b\x9c\xe6\x97\xa5", // 水曜日
"\xe6\x9c\xa8\xe6\x9b\x9c\xe6\x97\xa5", // 木曜日
"\xe9\x87\x91\xe6\x9b\x9c\xe6\x97\xa5", // 金曜日
"\xe5\x9c\x9f\xe6\x9b\x9c\xe6\x97\xa5" // 土曜日
};
int main() {
char buf[68];
memcpy(buf, template, sizeof(buf));
struct tm t;
time_t now = time(NULL);
localtime_r(&now, &t);
// Date components
int year = t.tm_year + 1900;
memcpy(buf + 7, digits + (year / 100) * 2, 2);
memcpy(buf + 9, digits + (year % 100) * 2, 2);
memcpy(buf + 14, digits + (t.tm_mon + 1) * 2, 2);
memcpy(buf + 19, digits + t.tm_mday * 2, 2);
// Time components - critical fix
memcpy(buf + 25, digits + t.tm_hour * 2, 2); // Was 26
memcpy(buf + 30, digits + t.tm_min * 2, 2); // Was 31
memcpy(buf + 35, digits + t.tm_sec * 2, 2); // Was 36
// Weekday
memcpy(buf + 41, weekdays[t.tm_wday], 9);
write(STDOUT_FILENO, buf, 60);
return 0;
}