/// <summary> /// /// </summary> /// <param name="sourceDatetime"></param> /// <param name="sourceTimeZoneId"></param> /// <param name="targetTimeZoneId"></param> /// <returns></returns> public static DateTime TimeZoneConvert(DateTime sourceDatetime,string sourceTimeZoneId,string targetTimeZoneId) { //DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00); //TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"); //Console.WriteLine("{0} {1} is {2} local time.", // hwTime, // hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName, // TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local)); try { TimeZoneInfo sourceTimezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(sourceTimeZoneId); TimeZoneInfo targetTimezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(targetTimeZoneId); return TimeZoneInfo.ConvertTime(sourceDatetime, sourceTimezoneInfo, targetTimezoneInfo); } catch (TimeZoneNotFoundException) { return DateTime.UtcNow; //return new Exception ("The registry does not define the Hawaiian Standard Time zone."); } catch (InvalidTimeZoneException) { return DateTime.UtcNow; //Console.WriteLine("Registry data on the Hawaiian STandard Time zone has been corrupted."); } }
算费用:
View Code
/// <summary> /// 计算设备使用费用 /// </summary> /// <param name="dtRate"></param> /// <param name="dr"></param> /// <param name="sourceDomainID"></param> /// <returns></returns> public double CountMoneyByRate(DataTable dtRate, DataRow dr, string sourceDomainID) { //source timezoneinfo DomainBusiness domainBus = new DomainBusiness(); string sourceTimeZoneKey= domainBus.ReadFullById(new Guid(sourceDomainID)).TimeZoneEnName; //target timezoneinfo string targetTimezoneId = dr["timezone"].ToString(); TSysTimeZoneDal tzDal = new TSysTimeZoneDal(); string targetTimezoneKey = tzDal.GetList(new Guid(targetTimezoneId)).First().ENName; double money = 0.0; //收集开始时间信息 DateTime startTime = Convert.ToDateTime(dr["startDate"]); //开始时间 DateTime startTimeToTarget = CommonUtility.TimeZoneConvert(startTime, sourceTimeZoneKey, targetTimezoneKey); int dayOfStartTimeToTarget=startTimeToTarget.Day; int hourOfStartTimeToTarget = startTimeToTarget.Hour; //int minuteOfStartTimeToTarget = startTimeToTarget.Minute; double totalHourOfStartTimeToTarget = hourOfStartTimeToTarget + double.Parse(startTimeToTarget.Minute.ToString()) / 60; int hourStartInterval = this.GetIntervalByHour(totalHourOfStartTimeToTarget); //收集结束时间信息 DateTime endTime = Convert.ToDateTime(dr["endDate"]); //结束时间 DateTime endTimeToTarget = CommonUtility.TimeZoneConvert(endTime, sourceTimeZoneKey, targetTimezoneKey);//.ToString("yyyy-MM-dd hh:mm:ss");//24小时制 int dayOfEndTimeToTarget = endTimeToTarget.Day; int hourOfEndTimeToTarget = endTimeToTarget.Hour == 0 ? 24 : endTimeToTarget.Hour; int minuteOfEndTimeToTarget = endTimeToTarget.Minute; double totalHourOfEndTimeToTarget = (hourOfEndTimeToTarget==24&&minuteOfEndTimeToTarget>0)?(0 + double.Parse(minuteOfEndTimeToTarget.ToString()) / 60):(hourOfEndTimeToTarget + double.Parse(minuteOfEndTimeToTarget.ToString()) / 60); int hourEndInterval = this.GetIntervalByHour(totalHourOfEndTimeToTarget); //计算费用,分以下N种情况: double unitPrice = double.Parse(dr["unitPrice"].ToString()); //单价 int number = int.Parse(dr["number"].ToString());//数量 int intervalOffset = Math.Abs(hourEndInterval - hourStartInterval); int dayOffset = (endTimeToTarget-startTimeToTarget).Days ;//Math.Abs(dayOfEndTimeToTarget - dayOfStartTimeToTarget); int hourOff = (endTimeToTarget - startTimeToTarget).Hours; double hourOffset = Math.Abs(hourOfEndTimeToTarget - totalHourOfStartTimeToTarget); //int minuteOffset = Math.Abs(minuteOfEndTimeToTarget - minuteOfStartTimeToTarget); //double minuteOffsetToHour=Convert.ToDouble(minuteOffset); //情况一:开始和结束时间都是同一区间 double rate = 0.0; if (intervalOffset == 0) { if (endTimeToTarget==startTimeToTarget) { money = 0.0; } else { rate = dtRate.Rows.Count > 0 ? double.Parse(dtRate.Rows[hourStartInterval - 1]["DiscountRate"].ToString()) : 0; //折扣率 //费用=天数*(四个区间共24+小时费用之和) money = dayOffset * (unitPrice * number * rate * (6 * 4)) + (unitPrice * number * rate * hourOffset); } } else { //情况二:开始和结束时间不在同一区间 //int flagOffset = hourOfEndTimeToTarget - hourOfStartTimeToTarget;//值大于0说明结束时间所在的区间大于开始时间所在的区间 //开始部分费用 rate = dtRate.Rows.Count > 0 ? double.Parse(dtRate.Rows[hourStartInterval - 1]["DiscountRate"].ToString()) : 0; //折扣率 money = dayOffset * (unitPrice * number * rate * (6 * 4)) + (unitPrice * number * rate * (hourStartInterval * 6 - totalHourOfStartTimeToTarget)); //加上结束部分费用 rate = dtRate.Rows.Count > 0 ? double.Parse(dtRate.Rows[hourEndInterval - 1]["DiscountRate"].ToString()) : 0; //折扣率 money += (unitPrice * number * rate * (totalHourOfEndTimeToTarget - hourEndInterval * 6)); //再加上中间相间隔的那部分区间费用,间隔可能是2,1个区间 switch (hourEndInterval - hourStartInterval) { case 1: rate = dtRate.Rows.Count > 0 ? double.Parse(dtRate.Rows[hourStartInterval]["DiscountRate"].ToString()) : 0; //折扣率 money += unitPrice * number * rate * 1 * 6; break; case 2: rate = dtRate.Rows.Count > 0 ? double.Parse(dtRate.Rows[hourStartInterval+ 1]["DiscountRate"].ToString()) : 0; //折扣率 money += unitPrice * number * rate * 2 * 6; break; } } return money; } /// <summary> /// 根据小时计算区间 /// 作者:xucixiao /// 时间:2013-03-06 /// </summary> /// <param name="hour">24小时制小时数</param> /// <returns></returns> private int GetIntervalByHour(double hour) { //第一区间:00:00 -06:00: 折扣率:1.0000 //第二区间:06:00 -12:00: 折扣率:0.8000 //第三区间:12:00 -18:00: 折扣率:0.5000 //第四区间:18:00 -00:00: 折扣率:0.6000 //计算区间 int interval = 0; //第一区间 if (0<=hour && hour <= 6) { interval = 1; } //第二区间 if (6 < hour && hour <= 12) { interval = 2; } //第三区间 if (12 < hour && hour <= 18) { interval = 3; } //第四区间 if (18 < hour && hour <= 24) { interval = 4; } return interval; }