如何获得在C#中的DateTime精确 [英] How to get precise DateTime in C#

查看:162
本文介绍了如何获得在C#中的DateTime精确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 DateTime.UtcNow 不具有相对较高的精确度(10-15毫秒分辨率)。

I know that DateTime.UtcNow does not have a relatively high degree of precision (10-15 ms resolution).

这对我来说是一个问题,因为在我的日志文件,我想解决竞争条件的目的,更好的分辨率

This is a problem for me because in my log files, I want better resolution for the purposes of resolving race conditions.

现在的问题是:我怎么能获取当前的日期/时间比 DateTime.UtcNow 优惠?

The question is: how can I get the current date/time with a better precision than what DateTime.UtcNow offers?

唯一的解决方案,更好的精度我见过到现在一直是这个: http://stackoverflow.com/a/15008836/270348 。那是一个合适的方式来解决这个问题?

The only solution which I've seen up till now has been this: http://stackoverflow.com/a/15008836/270348. Is that a suitable way to solve the problem?

推荐答案

您可以使用秒表的时间更精确的测量。然后,您可以从第一个动作开始时有每个日志条目记录。如果是很重要的,你可以记录的DateTime 第一个操作,因此计算出其余的时间,但它听起来好像只是有自的开始蜱/纳秒第一次手术是你已经足够了。

You can use Stopwatch for more precise measurements of time. You can then have each log entry record the time from the start of the first operation. If it's important, you can record the DateTime of the first operation and therefore calculate the times of the rest, but it sounds like just having the ticks/nanoseconds since the start of the first operation is good enough for your purposes.

这篇关于如何获得在C#中的DateTime精确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆