none
设备运行时间 RRS feed

  • 问题

  • 有这么几条设备运行记录

    设备1 : 开机2015-01-01 08:00:00  关机2015-01-01 12:00:00
    设备1:开机2015-01-01 14:00:00 关机2015-01-01 19:00:00

    设备1: 开机2015-01-02 08:00:00 关机2015-01-02  20:00:00

    设备1:开机2015-01-03 08:00:00 关机2015-01-06 12:00:00

    求设备1在2015-01-01日的运行时长?
    求设备1在2015-01-02日的运行时长?
    求设备1在2015-01-05日的运行时长?

    上三个问题分别代表的是设备运行的不同种情况,可能一天开关多次,可能一天开关一次,可能连续运行几天,等等

    怎么写一个函数方法把这些情况都分析清楚,准确的把设备在一天内的运行时长计算出来??

    小弟想了很久都没什么好办法,各路大神求救
    2015年1月29日 2:41

答案

  •  写了一个处理流程,供你参考。实际应用中还可以进行一些优化,以提高效率。      

    private void btnStart_Click(object sender, EventArgs e)
            {
                List<string> listRecord=new List<string>();
                listRecord.Add("设备1:开机2015-01-01 08:00:00  关机2015-01-01 12:00:00");
                listRecord.Add("设备1:开机2015-01-01 14:00:00  关机2015-01-01 19:00:00");
                listRecord.Add("设备1:开机2015-01-02 08:00:00  关机2015-01-02 20:00:00");
                listRecord.Add("设备1:开机2015-01-03 08:00:00  关机2015-01-06 12:00:00");
                List<string> listNewRecord=new List<string>();
                Regex regex=new Regex(@"(?<设备名称>\w*):开机(?<开机时间>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})  关机(?<关机时间>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})");
                while (listRecord.Count>0)
                {
                    Match match = regex.Match(listRecord[0]);
                    string device = match.Groups["设备名称"].Value;
                    DateTime start = DateTime.Parse(match.Groups["开机时间"].Value);
                    DateTime end = DateTime.Parse(match.Groups["关机时间"].Value);
                    if (start.Date != end.Date)
                    {
                        string _tmpEnd = start.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
                        string _tmpStart = start.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
                        listNewRecord.Add(string.Format("{0}:开机{1}  关机{2}",device, start.ToString("yyyy-MM-dd HH:mm:ss"), _tmpEnd));
                        listRecord.Add(string.Format("{0}:开机{1}  关机{2}", device, _tmpStart, end.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    else
                    {
                        listNewRecord.Add(listRecord[0]);
                    }
                    listRecord.RemoveAt(0);
                }
                Dictionary<string, double> dicWork = new Dictionary<string, double>();
                foreach (string record in listNewRecord)
                {
                    Match match = regex.Match(record);
                    string device = match.Groups["设备名称"].Value;
                    DateTime start = DateTime.Parse(match.Groups["开机时间"].Value);
                    DateTime end = DateTime.Parse(match.Groups["关机时间"].Value);
                    double workHour = (end - start).TotalHours;
                    string key = device + ":" + start.ToString("yyyy-MM-dd");
                    if (dicWork.ContainsKey(key))
                    {
                        dicWork[key] = dicWork[key] + workHour;
                    }
                    else
                    {
                        dicWork.Add(key,workHour);
                    }
                }
                foreach (string key in dicWork.Keys)
                {
                    Console.WriteLine(key + "工作时间为" + dicWork[key].ToString() + "小时");
                }
            }

    2015年1月30日 2:54