用于显示从一个时间戳到另一个时间戳的特定数据的逻辑。
预期产出:
12:00:00 - 13:00:00 -> should display 1 (In the log, timestamp 12:00:00 has data as "1", All lines from 12:00:00 until 13:00:00 should display 1. But has to change at 13:00:00, as timestamp 13:00:00 has new data which is "5")
13:00:00 - 14:00:00 -> should display 5 (13:00:00 line has data as 5)
14:00:00 - 15:00:00 -> should display 7 (14:00:00 line has data as 7)
日志样本:
{"data":"1","Timestamp":"12:00:00"}
{"sample1":"sample","x":"12:30:00"}
{"sample1":"sample","x":"12:40:00"}
.....
{"data":"5","Timestamp":"13:00:00"}
{"sample1":"sample""x":"13:10:00"}
.....
{"data":"7","Timestamp":"14:00:00"}
...
目前使用的代码:
eachTBData.TimeStamp >= this.x
当前输出:
Correct data is not shown (Maps and displays in the previous set instead)
12:00:00 - 13:00:00 -> this set displays 5 (has to display 1)
13:00:00 - 14:00:00 -> this set displays 7 (has to display 5)
试过:
Thought using '<= this.x' instead of '>= this.x' as shown in below line, would solve the issue.
But does not work and displays the initial data "1" for all sets:
eachTBData.TimeStamp <= this.x
谢谢。
尝试使用正则表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication52
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
string pattern = @"""(?'key1'[^""]+)"":""(?'data'\d+)"",""(?'key2'[^""]+)"":""(?'time'[^""]+)";
string oldData = "";
string time = "";
string oldTime = "";
while ((line = reader.ReadLine()) != null)
{
Match match = Regex.Match(line, pattern);
if (match.Success)
{
string key1 = match.Groups["key1"].Value;
time = match.Groups["time"].Value;
if (key1 == "data")
{
string data = match.Groups["data"].Value;
if (data != oldData)
{
if (oldData.Length != 0)
{
Console.WriteLine("{0} - {1} -> {2}", oldTime, time, oldData);
}
oldData = data;
oldTime = time;
}
}
}
}
Console.WriteLine("{0} - {1} -> {2}", oldTime, time, oldData);
}
}
}