• 计算某一天是星期几


    计算某一天是星期几的公式如下:

     W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D.
    其中Y是年份,D是改天是Y年的第几天。具体的解释见这儿

     具体代码如下:

    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;

    const int MONTH[12]={31,28,31,30,31,30,
                         31,31,30,31,30,31,
    };
    const string WEEK[]={"SUN","MON","TUE","WEN","THU","FRI","SAT"};
    int get_day_count(int year,int month,int day)
    {
        bool isleap=((year%4==0&&year%100!=0)||(year%400==0));
        int count;
        count=0;
        if(isleap&&month>2)
            count++;
        for(int i=1;i<month;i++)
            count+=MONTH[i-1];
        count+=day;

        return count;
    }
    int str2int(string str)
    {
        istringstream is(str);
        int n;
        is>>n;
        return n;

    }
    void parse(string str,int &year,int &month,int &day)
    {
        size_t p1,p2;
        p1=str.find('-');
        if(p1==string::npos)
        {
            cout<<"error"<<endl;
        }
        string subs=str.substr(0,p1);
        year=str2int(subs);

        p2=str.find_last_of('-');
        if(p1==string::npos)
        {
            cout<<"error"<<endl;
        }
        subs=str.substr(p1+1,p2-p1);
        month=str2int(subs);

        subs=str.substr(p2+1);
        day=str2int(subs);
    }

    int main(int argc,char**argv)
    {
        if(argc<2)
        {
            cout<<"usage:"<<argv[0]<<" <year>-<month>-<day>"<<endl;
            return 1;
        }
        string str(argv[1]);
        int year,month,day;
        parse(str,year,month,day);

        cout<<"year="<<year<<endl;
        cout<<"month="<<month<<endl;
        cout<<"day="<<day<<endl;
        int count;
        count=get_day_count(year,month,day);
        cout<<"count="<<count<<endl;

        int week=((year-1)+(year-1)/4-(year-1)/100+(year-1)/400+count)%7;
        
        cout<<WEEK[week]<<endl;
    }


    还可以利用mktime函数,参见

    具体代码如下:

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <ctime>
    using namespace std;

    const string WEEK[]={"SUN","MON","TUE","WEN","THU","FRI","SAT"};
    int str2int(string str)
    {
        istringstream is(str);
        int n;
        is>>n;
        return n;

    }
    void parse(string str,int &year,int &month,int &day)
    {
        size_t p1,p2;
        p1=str.find('-');
        if(p1==string::npos)
        {
            cout<<"error"<<endl;
        }
        string subs=str.substr(0,p1);
        year=str2int(subs);

        p2=str.find_last_of('-');
        if(p1==string::npos)
        {
            cout<<"error"<<endl;
        }
        subs=str.substr(p1+1,p2-p1);
        month=str2int(subs);

        subs=str.substr(p2+1);
        day=str2int(subs);
    }

    int main(int argc,char**argv)
    {
        if(argc<2)
        {
            cout<<"usage:"<<argv[0]<<" <year>-<month>-<day>"<<endl;
            return 1;
        }
        string str(argv[1]);
        int year,month,day;
        parse(str,year,month,day);

        cout<<"year="<<year<<endl;
        cout<<"month="<<month<<endl;
        cout<<"day="<<day<<endl;
        struct tm tm1,*tm2;
        time_t timep;
        int week;
        tm1.tm_year= year-1900;  
        tm1.tm_mon=month-1;  
        tm1.tm_mday=day;  
        tm1.tm_hour=12;  
        tm1.tm_min=0;  
        tm1.tm_sec=0;
        timep=mktime(&tm1);
        tm2=localtime(&timep);
        cout<<asctime(tm2)<<endl;

        week=(int) (tm2->tm_wday);

        cout<<WEEK[week]<<endl;
    }
  • 相关阅读:
    SpringBoot项目中遇到的BUG
    关于Unsupported major.minor version 52.0报错问题解决方案
    spring官网上下载历史版本的spring插件,springsource-tool-suite
    构建微服务:Spring boot 入门篇
    Spring Cloud 入门教程(一): 服务注册
    SpringCloud是什么?
    ubuntu下查看windows的 txt 文件乱码
    Ubuntu 14.04 LTS中怎样安装fcitx中文输入法
    eclipse调用jni
    Ubuntu 12.04 分区方案(仅供参考)
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2505591.html
Copyright © 2020-2023  润新知