C# 从时间列表中查找最近的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the closest time from a list of times
提问by Nick DeMayo
So, here's the scenario. I have a file with a created time, and I want to choose a time from a list of times that that file's created time is closest or equal too...what would be the best way to accomplish this?
所以,这是场景。我有一个具有创建时间的文件,我想从该文件的创建时间最接近或相等的时间列表中选择一个时间……完成此任务的最佳方法是什么?
采纳答案by luvieere
Something like this:
像这样的东西:
DateTime fileDate, closestDate;
ArrayList theDates;
long min = long.MaxValue;
foreach (DateTime date in theDates)
if (Math.Abs(date.Ticks - fileDate.Ticks) < min)
{
min = Math.Abs(date.Ticks - fileDate.Ticks);
closestDate = date;
}
回答by ps.
get the difference of your file creatime and every time in your list and sort the absolute value of each time difference. the first one should be the answer you are looking for.
获取您的文件创建时间和列表中每次的差异,并对每个时间差异的绝对值进行排序。第一个应该是您正在寻找的答案。
回答by Adriaan Stander
Use the minimum absolute time difference between the file time and the time in the list. You might get 2 entries being the same, and then you would need a different method to differ between these.
使用文件时间和列表中时间之间的最小绝对时间差。您可能会得到 2 个相同的条目,然后您需要使用不同的方法来区分这些条目。
回答by Yuriy Faktorovich
var creationTimes = new [] {DateTime.Now.AddDays(-1), DateTime.Now.AddDays(-2)};
FileInfo fi = new FileInfo("C:/test.xml");
var closestTime = creationTimes
.OrderBy(c => Math.Abs(c.Subtract(fi.CreationTime).Days))
.First();
回答by leppie
var min = listoftimes.Select(
x => new { diff = Math.Abs((x - timeoffile).Ticks), time = x}).
OrderBy(x => x.diff).
First().time;
Note: Assumes at least 1 entry in listoftimes
.
注意:假设 中至少有 1 个条目listoftimes
。
回答by Jerry Coffin
How often will you be doing this with the same list of times? If you're only doing it once, the fastest way is probably to just scan through the list and keep track of the closest time you've seen yet. When/if you encounter a time that's closer, replace the "closest" with that closer time.
您多久会使用相同的时间列表执行此操作?如果您只执行一次,最快的方法可能是浏览列表并跟踪您所看到的最近时间。当/如果您遇到更近的时间,请将“最近”替换为更近的时间。
If you're doing it very often, you'd probably want to sort the list, then use a binary search.
如果您经常这样做,您可能希望对列表进行排序,然后使用二进制搜索。
回答by LukeH
var closestTime = listOfTimes.OrderBy(t => Math.Abs((t - fileCreateTime).Ticks))
.First();
If you don't want the performance overhead of the OrderBy
call then you could use something like the MinBy
extension method from MoreLINQ
instead:
如果你不想要OrderBy
调用的性能开销,那么你可以使用类似MinBy
扩展方法的东西MoreLINQ
:
var closestTime = listOfTimes.MinBy(t => Math.Abs((t - fileCreateTime).Ticks));
回答by Thomas Levesque
var closestTime = (from t in listOfTimes
orderby (t - fileInfo.CreationTime).Duration()
select t).First();
回答by cdkMoose
Not an answer, but a question regarding the various LINQ solutions proposed above. How efficient is LINQ? I have not written any "real" programs with LINQ yet, so I'm not sure on the performance.
不是答案,而是关于上面提出的各种 LINQ 解决方案的问题。LINQ 的效率如何?我还没有用 LINQ 编写任何“真正的”程序,所以我不确定性能。
In this example, the "listOfTimes" collection implies that we have already iterated over some file system based objects to gather the times. Would it have been more efficient to do the analysis during the iteration instead of later in LINQ? I recognize that these solutions may be more "elegant" or nicely abstract the "collection as database" idea, but I tend to choose efficiency (must be readable though) over elagance in my programming. Just wondering if the cost of LINQ might outweigh the elegance here?
在这个例子中,“listOfTimes”集合意味着我们已经迭代了一些基于文件系统的对象来收集时间。在迭代期间而不是在 LINQ 中进行分析会更有效吗?我认识到这些解决方案可能更“优雅”或很好地抽象了“作为数据库的集合”的想法,但我倾向于在我的编程中选择效率(尽管必须可读)而不是优雅。只是想知道 LINQ 的成本是否可能超过这里的优雅?
回答by Kevin
The accepted answer is completely wrong. What you want is something like this:
接受的答案是完全错误的。你想要的是这样的:
DateTime fileDate, closestDate;
List<DateTime> theDates;
fileDate = DateTime.Today; //set to the file date
theDates = new List<DateTime>(); //load the date list, obviously
long min = Math.Abs(fileDate.Ticks - theDates[0].Ticks);
long diff;
foreach (DateTime date in theDates)
{
diff = Math.Abs(fileDate.Ticks - date.Ticks);
if (diff < min)
{
min = diff;
closestDate = date;
}
}