吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3634|回复: 11
收起左侧

[其他转载] 节假日API·用CodeIgniter框架·C#·撸一个·方便自己调用。

[复制链接]
zmolli775 发表于 2019-10-30 14:17
本帖最后由 zmolli775 于 2019-11-26 08:58 编辑

近日工作需要·节假日API调用·需要判断那天是·节假日·休息日·工作日,网上找的免费API要嘛调用有次数限制·要嘛就是各种注册收费。所以自己撸了一个!
国家法定节假日 =  3
周六、周日        =  2
上班                  =  1
错误                  =  0

JJRAPI.png
WH.png
后期维护·对应修改紫色框日期即可。
php小白·欢迎各位大佬·纠错指正`以便完善此代码...

------------------------------------------------
好用加分...
调用方法get
http://你的域名/index.php/控制器名/(方法名)JJR?d=20190103(参数)

---------------------------------------------------------
更新2020年C#类
[C#] 纯文本查看 复制代码
 
    /// <summary>
    /// 节假日检测类
    /// </summary>
    public class JJR
    {
        public List<string[]>
            // 2019年法定节假日
            y2019 = new List<string[]>(),
            // 2019年调休工作日
            w2019 = new List<string[]>(),
            // 2020年法定节假日
            y2020 = new List<string[]>(),
            // 2020年调休工作日
            w2020 = new List<string[]>();
        /// <summary>
        /// 节假日检测(仅支持判断2019年、2020年)
        /// </summary>
        public JJR()
        {
            /*2019年有效数据*/
            y2019.Add(new string[] { "20181230", "20190101" }); // 元旦
            y2019.Add(new string[] { "20190204", "20190210" }); // 春节
            y2019.Add(new string[] { "20190405", "20190407" }); // 清明
            y2019.Add(new string[] { "20190501", "20190504" }); // 劳动
            y2019.Add(new string[] { "20190607", "20190609" }); // 端午
            y2019.Add(new string[] { "20190913", "20190915" }); // 中秋
            y2019.Add(new string[] { "20191001", "20191007" }); // 国庆
            // 调整工作日
            w2019.Add(new string[] { "20181229", "20190202", "20190203", "20190428", "20190505", "20190929", "20191012" });

            /*2020年有效数据*/
            y2020.Add(new string[] { "20200101", "20200101" }); // 元旦
            y2020.Add(new string[] { "20200124", "20200130" }); // 春节
            y2020.Add(new string[] { "20200404", "20200406" }); // 清明
            y2020.Add(new string[] { "20200501", "20200505" }); // 劳动
            y2020.Add(new string[] { "20200625", "20200627" }); // 端午
            y2020.Add(new string[] { "20201001", "20201008" }); // 中秋
            y2020.Add(new string[] { "20201001", "20201008" }); // 国庆
            // 调整工作日
            w2020.Add(new string[] { "20200119", "20200201", "20200426", "20200509", "20200628", "20200927", "20201010" });
        }
        /// <summary>
        /// 日期计算临时变量
        /// </summary>
        private DateTime temp;
        /// <summary>
        /// 检测日期状态(3:节假日、2:休息日、1:工作日、0:错误)
        /// </summary>
        /// <param name="rq">日期(20191231)</param>
        /// <returns>返回状态码(3:节假日、2:休息日、1:工作日、0:错误)</returns>
        public int CheckForHolidays(string rq)
        {
            temp = Str2dt(rq);

            if ((temp.Year == 2019 || temp.Year == 2020) && temp != new DateTime())
            {
                return Determine();
            }
            else
            {
                /*Console.WriteLine("Error");*/
                return 0;
            }
        }
        /// <summary>
        /// 检查法定节假日和调休工作日
        /// </summary>
        /// <param name="y">法定节假日</param>
        /// <param name="w">调休工作日</param>
        /// <returns>返回状态码(3:节假日、1:工作日、0:错误)</returns>
        private int Check(List<string[]> y, List<string[]> w)
        {
            // 法定节假日检查
            foreach (var i in y)
            {
                if (temp >= Str2dt(i[0]) && temp <= Str2dt(i[1]))
                {
                    return 3;
                }
            }

            // 调休工作日检查
            foreach (string[] i in w)
            {
                foreach (var item in i)
                {
                    if (temp == Str2dt(item))
                    {
                        return 1;
                    }
                }
            }
            return 0;
        }
        private int Determine()
        {
            int type = 0;

            switch (temp.Year)
            {
                // 2019年
                case 2019:
                    type = Check(y2019, w2019); break;

                // 2020年
                case 2020:
                    type = Check(y2020, w2020); break;
            }

            if (type == 0)
            {
                int week = Convert.ToInt32(temp.DayOfWeek);

                if (6 == week || 0 == week) // 类型为0`检测是否为星期六、星期日
                {
                    type = 2; // 休息日
                }
                else
                {
                    type = 1; // 工作日
                }
            }
            /*
            switch (type)
            {
                case 3: Console.WriteLine("节假日"); break;
                case 2: Console.WriteLine("休息日"); break;
                case 1: Console.WriteLine("工作日"); break;
            }
            */
            return type;
        }
        /// <summary>
        /// string转换DateTime
        /// </summary>
        public static DateTime Str2dt(string d)
        {
            try
            {
                return DateTime.ParseExact(d, "yyyyMMdd", CultureInfo.CreateSpecificCulture("zh-CN"));
            }
            catch
            {
                return new DateTime();
            }
        }    }

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

叫我小王叔叔 发表于 2020-5-18 10:33
我复制了代码,贴到index.php中,设置虚拟主机,然后打开网址,出现乱码,http://你的域名/index.php/控制器名/(方法名)JJR?d=20190103(参数),/控制器名/方法名不知道怎么填。小白一个,请大佬指点
 楼主| zmolli775 发表于 2020-5-19 08:34
叫我小王叔叔 发表于 2020-5-18 10:33
我复制了代码,贴到index.php中,设置虚拟主机,然后打开网址,出现乱码,http://你的域名/index.php/控制 ...

你需要CodeIgniter3框架·如果用框架。
lovnie 发表于 2019-10-30 14:47
898601566 发表于 2019-10-30 14:55
代码不放上来,光贴个图?
 楼主| zmolli775 发表于 2019-10-30 15:43
898601566 发表于 2019-10-30 14:55
代码不放上来,光贴个图?

图上有全部代码·我放框架上来·不太好吧·还以为我推广框架呢!
yangliu1991515 发表于 2019-10-30 16:19
代码复制放上来
yangliu1991515 发表于 2019-10-30 16:53
最后那个elseif 可以优化一下
ctiger 发表于 2019-10-30 17:05
不明觉厉系列。
 楼主| zmolli775 发表于 2019-11-25 16:21
php没有2019年数据·请参照C#2020年数据添加
叫我小王叔叔 发表于 2020-5-18 10:30
控制器名和方法名是啥
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 09:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表