C日历

C日历实现效果图
模仿自windows10自带的日期控件

由第一张图可以看出,这里用到了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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容