public static DateOffsetInfo GetTimeOffset(DateTime startDate, DateTime endDate) { // DateTime nowDate = DateTime.Now.ToLocalTime(); DateTime nowDate = TimeZoneInfo.ConvertTime(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time")); TimeSpan startSpan = startDate.Subtract(nowDate); TimeSpan endSpan = endDate.Subtract(nowDate); int start = (int)startSpan.TotalHours; if (startSpan.TotalHours > (double)start) { start += 1; } int end = (int)endSpan.TotalHours; if (endSpan.TotalHours > (double)end) { end += 1; } int len = end - start; DateOffsetInfo dateOffset = new DateOffsetInfo(); dateOffset.start = start; dateOffset.len = len; return(dateOffset); }
public string GetPredictedSmogHourly(string location, DateTime startDate, DateTime endDate) { DateOffsetInfo dateoffset = FileUtils.GetDateOffset(startDate, endDate); int start = dateoffset.start; int len = dateoffset.len; string hourlyweatherurl = rootURL + "/hourly.json?key={0}&language=zh-Hans&location={1}&days={2}"; string[] FieldsToDisplay = { "time", "quality", "aqi", "pm25", "pm10" }; string[] ResponseTemplate = { "{0}", "空气质量{0}", "空气质量指数 {0}", "PM2.5 {0}", "PM10 {0}" }; int days = start + len; if (days > 7) { return("仅提供7天之内的空气质量预报"); } string completedURL = string.Format(hourlyweatherurl, key, location, days); string responseMessage = client.Query(completedURL); dynamic stuff = JObject.Parse(responseMessage); dynamic jsonArray = stuff.results[0].hourly; string respText = ""; List <string> respList = new List <string>(); foreach (dynamic jsonObj in jsonArray) { DateTime date = DateTime.Parse(jsonObj.time.ToString()); if (date < startDate || date >= endDate) { continue; } string resp = GetResponseText(jsonObj.ToString(), FieldsToDisplay, ResponseTemplate); respList.Add(resp); } if (respList.Count == 0) { respText = "我们仅提供七天之内的空气质量预报"; } else { respText = string.Join("\r\n", respList.ToArray()); } return(location + ":\r\n" + respText); }
public static DateOffsetInfo GetDateOffset(DateTime startDate, DateTime endDate) { DateTime today = DateTime.Now.Date; int start = startDate.Subtract(today).Days; int end = endDate.Subtract(today).Days; int len = end - start + 1; DateOffsetInfo dateOffset = new DateOffsetInfo(); dateOffset.start = start; dateOffset.len = len; return(dateOffset); }
public string GetPredictedSmogDaily(string location, DateTime startDate, DateTime endDate) { DateOffsetInfo dateoffset = FileUtils.GetDateOffset(startDate, endDate); int start = dateoffset.start; int len = dateoffset.len; string dailyweatherurl = rootURL + "/daily.json?key={0}&language=zh-Hans&location={1}&days={2}"; string[] FieldsToDisplay = { "date", "quality", "aqi", "pm25", "pm10" }; string[] ResponseTemplate = { "{0}", "空气质量{0}", "空气质量指数 {0}", "PM2.5 {0}", "PM10 {0}" }; int days = start + len; if (days > 7) { return("仅提供7天之内的空气质量预报"); } string completedURL = string.Format(dailyweatherurl, key, location, days); string responseMessage = client.Query(completedURL); dynamic stuff = JObject.Parse(responseMessage); dynamic jsonArray = stuff.results[0].daily; string respText = ""; int index = 0; foreach (dynamic jsonObj in jsonArray) { if (index < start) { index++; continue; } else if (index >= start + len) { break; } string resp = GetResponseText(jsonObj.ToString(), FieldsToDisplay, ResponseTemplate); respText += resp + "\r\n"; index++; } return(location + ":\r\n" + respText); }
public string GetPredictedWeatherHourly(string location, DateTime startDate, DateTime endDate) { DateOffsetInfo dateoffset = FileUtils.GetTimeOffset(startDate, endDate); int start = dateoffset.start; int len = dateoffset.len; if (start + len > 24) { return("我们仅预报24小时之内的天气情况"); } string hourlyweatherurl = rootURL + "/hourly.json?key={0}&language=zh-Hans&location={1}&start={2}&hours={3}"; //{"time":"2016-12-27T06:00:00+08:00","text":"多云","code":"4","temperature":"-6","humidity":"70","wind_direction":"北","wind_speed":"3.6"} string[] FieldsToDisplay = { "time", "text", "temperature", "wind_direction" }; string[] ResponseTemplate = { "{0}", "{0}", "气温{0}度", "风向{0}" }; string completedURL = string.Format(hourlyweatherurl, key, location, start, len); string responseMessage = client.Query(completedURL); dynamic stuff = JObject.Parse(responseMessage); dynamic jsonArray = stuff.results[0].hourly; string respText = ""; int index = 0; foreach (dynamic jsonObj in jsonArray) { string timeStr = jsonObj.time; DateTime time = DateTime.Parse(timeStr); string resp = GetResponseText(jsonObj.ToString(), FieldsToDisplay, ResponseTemplate); respText += resp + "\r\n"; index++; } return(location + ":\r\n" + respText); }
public string GetPredictedWeatherDaily(string location, DateTime startDate, DateTime endDate)//int start, int len) { DateOffsetInfo dateoffset = FileUtils.GetDateOffset(startDate, endDate); string dailyweatherurl = rootURL + "/daily.json?key={0}&location={1}&language=zh-Hans&unit=c&start={2}&days={3}"; string[] FieldsToDisplay = { "date", "text_day", "high", "text_night", "low", "wind_direction", "wind_scale" }; string[] ResponseTemplate = { "{0}", "白天{0}", "最高气温{0}度", "夜间{0}", "最低气温{0}度", "风向{0}", "风力{0}级" }; if (dateoffset.start + dateoffset.len > 15) { return("我们仅预报未来15天的天气"); } if (dateoffset.start < -1) { dateoffset.len -= -1 - dateoffset.len; dateoffset.start = -1; } string completedURL = string.Format(dailyweatherurl, key, location, dateoffset.start, dateoffset.len); string responseMessage = client.Query(completedURL); dynamic stuff = JObject.Parse(responseMessage); dynamic jsonArray = stuff.results[0].daily; string respText = ""; foreach (dynamic jsonObj in jsonArray) { string resp = GetResponseText(jsonObj.ToString(), FieldsToDisplay, ResponseTemplate); respText += resp + "\r\n"; } return(location + ":\r\n" + respText); }