备注:
TYPE 类型(正常工作日:0, 法定节假日:1, 节假日调休补班:2,休息日: 3)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Test.Common
{
//节假日
public class Holiday
{
public void main(string yearNow)
{
string[] arrTear = new string[] { yearNow };
for (int i = 0; i < arrTear.Length; i++)
{
DataTable table = new DataTable();
table.Columns.Add("ShortDay");
table.Columns.Add("Code");
string year = arrTear[i];
string startDay = year + "-01-01";
string endDay = year + "-12-31";
//获取相差几天
int days = DateDiff(Convert.ToDateTime(startDay), Convert.ToDateTime(endDay)) + 1;
//添加整一年的数据并区分出工作日与休息日
for (int j = 0; j < days; j++)
{
DataRow dr = table.NewRow();
dr["ShortDay"] = Convert.ToDateTime(startDay).AddDays(j).ToString("yyyyMMdd");
//判断是否未工作日
string temp = GetWeekDes((int)Convert.ToDateTime(startDay).AddDays(j).DayOfWeek);
if (temp == "星期日" || temp == "星期六")
{
dr["Code"] = 3;
}
else
{
dr["Code"] = 0;
}
table.Rows.Add(dr);
}
//获取改年的节假日信息
string result = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://timor.tech/api/holiday/year/" + year + "/");
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream streamNr = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(streamNr, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
catch (Exception e)
{
}
if (result != "")
{
JObject jo = (JObject)JsonConvert.DeserializeObject(result);
JObject jo_T = (JObject)JsonConvert.DeserializeObject(jo["holiday"].ToString());
foreach (var item in jo_T)
{
string key = item.Key;
JObject jo_T2 = (JObject)JsonConvert.DeserializeObject(item.Value.ToString());
string holiday = jo_T2["holiday"].ToString();
string dateT = jo_T2["date"].ToString();
string date = Convert.ToDateTime(dateT).ToString("yyyyMMdd");
for (int k = 0; k < table.Rows.Count; k++)
{
if (table.Rows[k]["ShortDay"].ToString() == date)
{
//法定节假日
if (holiday == "True" || holiday == "true")
{
table.Rows[k]["Code"] = 1;
}
//节假日调休补班
else
{
table.Rows[k]["Code"] = 2;
}
break;
}
}
}
}
string txt = "";
//添加记录
for (int j = 0; j < table.Rows.Count; j++)
{
string sql = "INSERT INTO HOLIDAY(SHORT_DATE,TYPE) values('{0}','{1}');";
sql = string.Format(sql, table.Rows[j]["ShortDay"], table.Rows[j]["Code"]);
txt += sql;
}
}
}
/// <summary>
/// 获取日期天数差
/// </summary>
/// <param name="dateStart"></param>
/// <param name="dateEnd"></param>
/// <returns></returns>
private int DateDiff(DateTime dateStart, DateTime dateEnd)
{
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
TimeSpan sp = end.Subtract(start);
return sp.Days;
}
private string GetWeekDes(int index)
{
switch (index)
{
case 0: return "星期日";
case 1: return "星期一";
case 2: return "星期二";
case 3: return "星期三";
case 4: return "星期四";
case 5: return "星期五";
case 6: return "星期六";
}
return "";
}
}
}