C语言---判断输入的日期是否合法_判断日期是否合法c语言-程序员宅基地

技术标签: C语言  c语言  linux  

前提:本例中规定日期的格式为YYYY-MM-DD

1、判断单个日期是否合法

int check_date(int year, int month, int day)
{
        int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if ( year < 0 )
        {
                printf("the entered year[%d] is invalid\n", year);
                return -1;
        }

        if ( month < 1 || month > 12 )
        {
                printf("The entered month[%d] is invalid\n", month);
                return -1;
        }

        if ( month == 2 )
        {
                // 判断如果是闰年,则修改二月的monthDays[1]值为29
                if ( (year % 400 == 0) || \
                     (year % 100 != 0 && year % 4 == 0)
                   )
                {
                        monthDays[1] = 29;
                }
        }

        if ( day < 1 || day > monthDays[month-1] )
        {
                printf("The entered day[%d] is invalid\n", day);
                return -1;
        }

        return 0;
}

2、解析输入日期字符串YYYY-MM-DD,并判断日期范围是否合法

int is_valid_date(const char *startDate, const char *endDate)
{
        int start_year = 0;
        int start_month = 0;
        int start_day = 0;
        int end_year = 0;
        int end_month = 0;
        int end_day = 0;

        if ( strlen(startDate) > 0 )
        {
                int count = 0;
                printf("\n check start date... \n");
                count = sscanf(startDate, "%4d-%2d-%2d", &start_year, &start_month, &start_day);
                if ( count != 3 )
                {
                        printf("Input parameters invalid.\n");
                        return -1;
                }

                if ( 0 != check_date(start_year, start_month, start_day) )
                {
                        return -1;
                }

                printf("\n check start date OK!\n");
        }

        if ( strlen(endDate) > 0 )
        {
                int count = 0;
                printf("\n check end date... \n");

                count = sscanf(endDate, "%4d-%2d-%2d", &end_year, &end_month, &end_day);
                if ( count != 3 )
                {
                        printf("Input parameters invalid.\n");
                        return -1;
                }

                if ( 0 != check_date(end_year, end_month, end_day) )
                {
                        return -1;
                }
                printf("\n check end date OK!\n");
        }

        printf("\n check date range...\n");
        if ( strlen(startDate) > 0 && strlen(endDate) > 0 )
        {
                if ( (end_year < start_year) || \
                     (end_year == start_year && end_month < start_month) || \
                     (end_year == start_year && end_month == start_month && end_day < start_day)
                   )
                {
                        printf("The end date is earlier than the start date!\n");
                        return -1;
                }
        }
        printf("\n check date range OK!\n");

        return 0;
}

3、测试代码

#include <stdio.h>
#include <string.h>

…… // is_valid_date()函数实现

int main(int argc, char **argv)
{
        if ( argc != 3)
        {
                printf("Usage: %s <start-date> <end-date>\n", argv[0]);
                return 0;
        }

        if ( 0 != is_valid_date(argv[1], argv[2]) )
        {
                printf("\n Input date is invalid!\n\n");
                return -1;
        }
        printf("\n start-date[%s], end-date[%s] are valid!\n\n", argv[1], argv[2]);

        return 0;
}

4、编译及测试

# gcc check_date.c -o check_date
#
#
#  ./check_date 2022-2-3 2022-4-5

 check start date...

 check start date OK!

 check end date...

 check end date OK!

 check date range...

 check date range OK!

 start-date[2022-2-3], end-date[2022-4-5] are valid!

#
#
#

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wcc15256945095/article/details/123852026

智能推荐

关于动态链接库DLL文件的一般常识及常见问题解析-程序员宅基地

DLL是Dynamic Link Library的缩写,意为动态链接库。在Windows中,许多应用程序并不是一个完整的可执行文件,它们被分割成一些相对独立的动态链接库,即DLL 文件,放置于系统中。当我们执行某一个程序时,相应的DLL文件就会被调用。一个应用程序可有多个DLL文件,一个DLL文件也可能被几个应用程序所共 用,这样的DLL文件被称为共享D...

Android用户协议与隐私政策实现-程序员宅基地

一、概述根据法规,现在上架的应用都要在App启动的时候弹出用户协议和隐私政策相关的内容,以便告知用户在使用App过程中的一些注意事项之类的东西,如果App没有添加这个功能,则不允许上架。...

S3C2416裸机开发系列十五_GCC下uCOS的移植(1)-程序员宅基地

操作系统是用来管理系统硬件、软件及数据资源,控制程序运行,并为其它应用软件提供支持的一种系统软件。根据不同的种类,又可分为实时操作系统、桌面操作系统、服务器操作系统等。对于一些小型的应用,对系统实时性要求高,硬件资源有限等的情况下,应尽量避免使用复杂庞大的操作系统(如Linux),使用小型的实时操作系统(如uCOS)更能满足应用的需求。笔者此处就uCOS-II的移植作一个简单的介绍。_s3c2416裸机开发系列

MATLAB绘制动画/动图-VideoWriter_matlab videowriter-程序员宅基地

在MATLAB中,有时候需要把多张图绘制一个动图,以表示,那么如何绘制呢?http://www.voidcn.com/article/p-wfxerpjf-bmk.html先创建一个文件,扩展名avi,这里以 videos.aviset(gca,‘nextplot’,‘replacechildren’);video_w= VideoWriter(‘videos.avi’);videos_w.FrameRate=10;%设置帧率open(video_w);for i=1:M%plot…绘图代码_matlab videowriter

linux eclipse动态链接,linux下Eclipse进行C编程时动态链接库的生成和使用_曾秋雷的博客-程序员宅基地

引用http://linux.chinaitlab.com/soft/864157.html一、创建动态链接库1、创建工程new->project->c++ project选择Shared Library->Empty Project.输入工程名a,点击finish,完成工程的创建。2、编写代码在windows下封装动态链接库时对要封的函数要用__declspec(dllexpo...

C语言用‘%20‘替换字符串中的所有空格的算法(附完整源码)_源代码大师的博客-程序员宅基地

C语言用'%20'替换字符串中的所有空格的算法C语言用'%20'替换字符串中的所有空格的算法完整源码(定义,实现,main函数测试)C语言用’%20’替换字符串中的所有空格的算法完整源码(定义,实现,main函数测试)#include <iostream>#include <cstring>void urlify(char *str, int len){ int numOfSpaces = 0; int i = 0, j = 0; for ( i =

随便推点

word插入Endnote参考文献报错{***, 1995 #307}解决方案_endnote插入文献参数错误-程序员宅基地

2016-10-21 14:33:12存在问题: word中插入endnote参考文献时,出现如下错误: 1)插入部分错误显示为{*, 1995 #307} 2)文档尾部并未有任何参考文献列出解决方案: 1)将Instant Formatting 由Off设置为on,如图: 在此过程中会出现如..._endnote插入文献参数错误

python 作图小工具 1. 产生随机数 2. 写入数据库shelve 3. 读取数据库shelve 4. 调用 numpy 和 mathplotlib 画图-程序员宅基地

-- coding: UTF-8 --#结合shelve和mathplotlib画图#Step1,先生成随机数import shelveimport random#Step1.1 先把Python小王子的数据生成出来name=“我是Python小王子”data_dict={}for key in range(1,100):data_dict[key]=random.randint...

滴滴开源的项目比腾讯还多?一起来看看滴滴开源的项目!-程序员宅基地

你知道的越多,不知道的就越多,业余的像一棵小草!你来,我们一起精进!你不来,我和你的竞争对手一起精进!编辑:业余草来源:https://www.xttblog.com/?p=5112在中..._滴滴开源

_ZN10tensorflow8internal21CheckOpMessageBuilder9NewStringB5cxx11Ev__zn18cpmcommoneidrecord10setclrflagep17cpmcommonob_哪都通临时员工的博客-程序员宅基地

在makefile上添加 -D_GLIBCXX_USE_CXX11_ABI=0如果已经设为0,还是这样的话,很可能是gcc版本和tf编译的版本不是同一个。可以尝试把gcc切换到4.9版本__zn18cpmcommoneidrecord10setclrflagep17cpmcommonobjqueuehh

我的博客判断素数-程序员宅基地

判断素数#include <stdio.h>int main(){ int a=0; // 素数的个数 int num=0; // 输入的整数 printf("输入一个整数:"); scanf("%d",&num); for(int i=2;i<num;i++){ if(nu...

【技术类】【ArcGIS对国产卫星的支持2:高分一号卫星】篇6、影像专题产品生产-程序员宅基地

对于多数行业用户而言,仅从数据分发商手中获取标准的影像产品远不能满足业务需求,当获得原始影像后,为了方便进行定量研究,需要进行额外的产品生产,比如植被指数产品,分类产品等等。 利用传统方式生产上述产品,要较长的处理等待时间,ArcGIS可以帮助用户实时得到影像结果。我们可以使用影像分析窗口内置的栅格处理工具,也可以通过多类工具栏进行产品生产。1、NDVI产品生产 利