由第一张图可以看出,这里用到了struct tm数据结构,进而贯穿整个操作。
关键点在于:
- 闰年下2月的计算
- 上月末,本月,下月初等日期的输出。
Main.c
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include "function.h"
#include "constraints.h"
int get_year_month_days(int year, int month) {
int days; // 获取当前月对应的天数
if(month == 2) {
if((year % 400 == 0) || (year%4 == 0 && year % 100 != 0)) {
days = 29;
}else{
days = 28;
}
}else if(month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
}else {
days = 31;
}
return days;
}
void print_lastmonth(struct tm *tm_now) {
int last_month_cols, this_month_cols;
int index; // 用于控制输出上个月的日期
int lastmonthlastday = get_year_month_days(1900+tm_now->tm_year, tm_now->tm_mon);
//printf("Last month days:%d\n", lastmonthlastday);
this_month_cols = (tm_now->tm_mday - tm_now->tm_wday)%7;
last_month_cols = WEEKDAY - this_month_cols;
//printf("%d:%d", last_month_cols, this_month_cols);
// 打印上个月剩余的日期
for(index=0; index < last_month_cols; index++) {
printf("\33[33m%.2d \33[0m", lastmonthlastday-last_month_cols + index + 1);
}
// 打印本月开始的日期
for(index=1; index <= this_month_cols; index++) {
printf("\33[32m%.2d \33[0m", index);
}
// 上个月混合输出结束
printf("\n");
}
void print_thismonth(struct tm *tm_now) {
// 本月总是占据中间4行,所以只需要找出第二行开始的第一个日期即可
int this_month_begin_day, last_month_cols, this_month_cols,next_month_cols;
int thismonthdays = get_year_month_days(tm_now->tm_year + 1900, tm_now->tm_mon + 1);
int index = (tm_now->tm_mday - tm_now->tm_wday) % WEEKDAY + 1;
int rowindex, colindex;
int tempdate;
int lastmonthlastday = get_year_month_days(1900+tm_now->tm_year, tm_now->tm_mon);
//printf("Last month days:%d\n", lastmonthlastday);
this_month_cols = (tm_now->tm_mday - tm_now->tm_wday)%7;
last_month_cols = WEEKDAY - this_month_cols;
// 打印上个月剩余的日期
for(index=0; index < last_month_cols; index++) {
printf("\33[33m%.2d \33[0m", lastmonthlastday-last_month_cols + index + 1);
}
// 打印本月开始的日期
for(index=1; index <= this_month_cols; index++) {
printf("\33[32m%.2d \33[0m", index);
}
// 上个月混合输出结束
printf("\n");
// 打印本月四行的数据;需要注意的是本月总天数可能会小于4行,因此要提前退出
for(rowindex=0; rowindex < ROW_NUM; rowindex++) {
// 打印每一列的数据
for(colindex=0; colindex < WEEKDAY; colindex++) {
tempdate = index + rowindex * WEEKDAY + colindex;
if(tempdate == tm_now->tm_mday) {
// 高亮显示当天日期
printf("\33[36m\33[1m\33[8m%.2d \33[0m", tempdate);
}else{
if(rowindex*WEEKDAY + colindex + index <= thismonthdays) {
printf("\33[32m%.2d \33[0m", tempdate);
}else{
goto NEXTMONTH;
}
}
}
// 每行结束记得换行
printf("\n");
}
NEXTMONTH: printf("");
printf("[INDEX]%d\n", tempdate);
// 下个月内容输出
this_month_cols = (thismonthdays - last_month_cols) % WEEKDAY;
next_month_cols = WEEKDAY - this_month_cols;
// 打印本月剩余日期内容
for(index=0; index < this_month_cols; index++) {
// 判断当前首个日期是否大于本月天数,大于的话需要提前终止输出
printf("\33[32m%.2d \33[0m", thismonthdays - this_month_cols + index + 1);
}
// 打印下月初始日期内容
for(index=1; index <= next_month_cols; index++) {
printf("\33[33m%.2d \33[0m", index);
}
// 尾行打印结束
printf("\n");
}
void print_month(struct tm *tm_now) {
int last_month_cols, this_month_head_cols, this_month_tail_cols, next_month_cols;
int lastmonthdays = get_year_month_days(1990 + tm_now->tm_year, tm_now->tm_mon);
int thismonthdays = get_year_month_days(1990 + tm_now->tm_year, tm_now->tm_mon + 1);
//printf("Lastmonthdays: %d, thismonthdays: %d\n", lastmonthdays, thismonthdays);
int tempdate;// 用于记录本月日期输出到了哪一天
int index, changeline=0;
int current_weekday;
if(tm_now->tm_wday == 0) {
current_weekday = WEEKDAY;
}else{
current_weekday = tm_now->tm_wday;
}
//printf("CURRENT WEEKDAY: %d\n", current_weekday);
this_month_head_cols = (tm_now->tm_mday - current_weekday) % WEEKDAY;
last_month_cols = WEEKDAY - this_month_head_cols;
this_month_tail_cols = (thismonthdays - this_month_head_cols) % WEEKDAY;
next_month_cols = WEEKDAY - this_month_tail_cols;
//printf("[%d-%d-%d-%d]\n", last_month_cols, this_month_head_cols, this_month_tail_cols, next_month_cols);
//printf("%2.d-%.2d-%.2d\n", tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday);
printf("%s\n", "一 二 三 四 五 六 日");
// 打印首行:上个月日期内容 + 本月月初日期内容
for(index=1; index <= last_month_cols; index++) {
printf("\33[33m%.2d \33[0m", lastmonthdays - last_month_cols + index);
}
for(index=1; index <= this_month_head_cols; index++) {
tempdate = index;
printf("\33[32m%.2d \33[0m", index);
}
// 首行输出完毕,记得回车换行
//printf("\t[tempdate=>%d, currentdate: %d]\n", tempdate, tm_now->tm_mday);
printf("\n");
// 输出本月中间几行的日期数据
for(index=tempdate+1, changeline=1; index <= thismonthdays; index++, changeline++, tempdate++) {
if(index == tm_now->tm_mday) {
// 高亮当天日期
printf("\33[35m\33[4m%.2d\33[0m", index);printf(" ");// 后面这俩空格是为了防止下划线溢出
}else{
printf("\33[32m%.2d \33[0m", index);
}
if(changeline == WEEKDAY) {
changeline = 0;
printf("\n");
}
}
// 输出下个月的日期数据
for(index=1; index <= next_month_cols; index++) {
printf("\33[33m%.2d \33[0m", index);
}
printf("\n");
}
int main() {
struct tm *tm_now;
//while(1) {
tm_now = get_curdate();
// tm_now->tm_year = 118;
// tm_now->tm_mon = 4;
// tm_now->tm_mday = 28;
//printf("\t%d-%d-%d -%d\n", tm_now->tm_year+1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_wday);
printf("| %d-%.2d-%.2d %.2d:%.2d:%.2d |\n", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
printf("============================\n");
print_month(tm_now);
printf("============================\n");
// 休眠一秒后刷新整个屏幕实现,动态更改时间的效果
//sleep(1);
//system("clear");
//}
return 0;
}
function.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct tm* get_curdate();
void sleep(int seconds);
function.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "function.h"
struct tm* get_curdate() {
time_t now;
struct tm *tm_now;
//char curdate[10];
time(&now);
tm_now = localtime(&now);
return tm_now;
}
void sleep(int seconds) {
time_t tm = time(NULL);
time_t tm2 = tm;
while(difftime(tm2, tm) < seconds) {
tm2 = time(NULL);
}
}
constraints.h
#define WEEKDAY 7
#define ROW_NUM 4
编译命令:
gcc *.c -o calender
测试命令:
./calender