.NET Compact Framework 上的 DateTime.Now 中的毫秒始终为零?

发布于 2024-08-28 05:36:37 字数 1443 浏览 3 评论 0原文

我想要 Windows Mobile 项目 上的日志有一个时间戳。精度必须至少在一百毫秒范围内。

但是,我对 DateTime.Now 的调用返回一个 DateTime 对象,其 Millisecond 属性设置为零。 Ticks 属性也相应地进行舍入。

如何获得更好的时间精度?
请记住,我的代码在 Compact Framework 3.5 版上运行。我使用 HTC touch Pro 2 设备。

根据 MusiGenesis 的回答,我创建了以下类来解决此问题:

/// <summary>
/// A more precisely implementation of some DateTime properties on mobile devices.
/// </summary>
/// <devdoc>Tested on a HTC Touch Pro2.</devdoc>
public static class DateTimePrecisely
{
    /// <summary>
    /// Remembers the start time when this model was created.
    /// </summary>
    private static DateTime _start = DateTime.Now;
    /// <summary>
    /// Remembers the system uptime ticks when this model was created. This
    /// serves as a more precise time provider as DateTime.Now can do.
    /// </summary>
    private static int _startTick = Environment.TickCount;

    /// <summary>
    /// Gets a DateTime object that is set exactly to the current date and time on this computer, expressed as the local time.
    /// </summary>
    /// <returns></returns>
    public static DateTime Now
    {
        get
        {
            return _start.AddMilliseconds(Environment.TickCount - _startTick);
        }
    }
}

i want to have a time stamp for logs on a Windows Mobile project. The accuracy must be in the range a hundred milliseconds at least.

However my call to DateTime.Now returns a DateTime object with the Millisecond property set to zero. Also the Ticks property is rounded accordingly.

How to get better time accuracy?
Remember, that my code runs on on the Compact Framework, version 3.5. I use a HTC touch Pro 2 device.

Based on the answer from MusiGenesis i have created the following class which solved this problem:

/// <summary>
/// A more precisely implementation of some DateTime properties on mobile devices.
/// </summary>
/// <devdoc>Tested on a HTC Touch Pro2.</devdoc>
public static class DateTimePrecisely
{
    /// <summary>
    /// Remembers the start time when this model was created.
    /// </summary>
    private static DateTime _start = DateTime.Now;
    /// <summary>
    /// Remembers the system uptime ticks when this model was created. This
    /// serves as a more precise time provider as DateTime.Now can do.
    /// </summary>
    private static int _startTick = Environment.TickCount;

    /// <summary>
    /// Gets a DateTime object that is set exactly to the current date and time on this computer, expressed as the local time.
    /// </summary>
    /// <returns></returns>
    public static DateTime Now
    {
        get
        {
            return _start.AddMilliseconds(Environment.TickCount - _startTick);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霊感 2024-09-04 05:36:37

Environment.TickCount 将返回 Windows(或 Windows Mobile)自上次重新启动以来运行的毫秒数。

要使用此功能,请将这两个表单级变量添加到您的代码中:

private DateTime _start;
private int _startTick;

在表单的 Load 事件中,执行以下操作:

private void Form1_Load(object sender, EventArgs e)
{
    _start = DateTime.Now;
    _startTick = Environment.TickCount;
}

每当您需要包含毫秒的 DateTime 对象时,执行以下操作:

DateTime timeStamp = 
    _start.AddMilliseconds(Environment.TickCount - _startTick);

Environment.TickCount 是一个 int 并且该值将在 25 天左右后“环绕”为 Int32.MinValue。如果您的设备要在不重新启动的情况下运行那么长时间,您需要添加一个检查 Environment.TickCount 值是否小于上次读取的值,并重置 _start_startTick 如果是的话。

Environment.TickCount will return the number of milliseconds that Windows (or Windows Mobile) has been running since the last reboot.

To use this, add these two form-level variables to your code:

private DateTime _start;
private int _startTick;

In your form's Load event, do this:

private void Form1_Load(object sender, EventArgs e)
{
    _start = DateTime.Now;
    _startTick = Environment.TickCount;
}

Whenever you need a DateTime object with milliseconds, do this:

DateTime timeStamp = 
    _start.AddMilliseconds(Environment.TickCount - _startTick);

Environment.TickCount is an int and this value will "wrap around" to Int32.MinValue after 25 days or so. If your device is going to be running that long without restarting, you'll want to add a check for an Environment.TickCount value that is less than the last value read, and reset both _start and _startTick if so.

楠木可依 2024-09-04 05:36:37

主要替代方案是 System.Diagnostics.Stopwatch 类。

它在 CE 中可用,但请注意 IsHighResolution 属性。在您的设备上它可能是 False,但请检查一下。

它与没有 P/Invoke 时一样准确。

The main alternative is the System.Diagnostics.Stopwatch class.

It is available in CE but note the IsHighResolution property. It probably is False on your device but do check.

It is as accurate as you're going to get without P/Invoke.

明媚殇 2024-09-04 05:36:37

在常规框架 v2.0 中,您可以使用 DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") 来获取毫秒。 f 越大意味着精度越高。

In regular framework v2.0 you may use DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") to get miliseconds. More f means more precision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文