none
How to get a timestamp with microsecond value. RRS feed

  • Question

  • In my C# project, while a UDP packet arriving, I need to log a timestamp with microsecond value.

    Below is a sample function to use microsecond timestamp:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace datetimetest
    {
        class Program
        {
            static void Main(string[] args)
            {
                for(int i=0;i<10;i++)
                {
                    long currentTick = DateTime.Now.Ticks;
                    Console.WriteLine("currentTick:{0};  timestamp:{1}", currentTick, new DateTime(currentTick).ToString("HH:mm:ss.ffffff"));
                }
                Console.ReadLine();
            }
        }
    }

    I got below result:

    currentTick:636844467783194613;timestamp:12:06:18.319461
    currentTick:636844467783204613;timestamp:12:06:18.320461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461
    currentTick:636844467783214613;timestamp:12:06:18.321461

    My questions are:

    1. On some PCs, the last 4 numbers would never change. In above case, the last 4 numbers are always "4613". Why?

    2. Is there any better way to get the timestamp with microsecond value?

    Any comments are highly appreciated. 


    • Edited by Denis xu Wednesday, January 30, 2019 4:16 AM
    Wednesday, January 30, 2019 4:14 AM

Answers

  • Probably there are some limitations. Try another class:

    Stopwatch sw = new Stopwatch();
    
    sw.Start();
     
    for( int i = 0; i < 10; i++ )
    {
        long ticks = sw.ElapsedTicks;
        long ms = sw.ElapsedMilliseconds;
        TimeSpan ts = sw.Elapsed;
     
        Console.WriteLine( "{0,10} {1,10} {2:G}", ticks, ms, ts );
    }



    or

    DateTime start = DateTime.Now;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    for( int i = 0; i < 10; i++ )
    {
        DateTime t = start.Add( sw.Elapsed );
    
        Console.WriteLine( "{0:HH:mm:ss.ffffff}", t );
    }


    • Edited by Viorel_MVP Wednesday, January 30, 2019 5:45 AM
    • Marked as answer by Denis xu Wednesday, January 30, 2019 5:59 AM
    Wednesday, January 30, 2019 5:39 AM

All replies

  • Probably there are some limitations. Try another class:

    Stopwatch sw = new Stopwatch();
    
    sw.Start();
     
    for( int i = 0; i < 10; i++ )
    {
        long ticks = sw.ElapsedTicks;
        long ms = sw.ElapsedMilliseconds;
        TimeSpan ts = sw.Elapsed;
     
        Console.WriteLine( "{0,10} {1,10} {2:G}", ticks, ms, ts );
    }



    or

    DateTime start = DateTime.Now;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    for( int i = 0; i < 10; i++ )
    {
        DateTime t = start.Add( sw.Elapsed );
    
        Console.WriteLine( "{0:HH:mm:ss.ffffff}", t );
    }


    • Edited by Viorel_MVP Wednesday, January 30, 2019 5:45 AM
    • Marked as answer by Denis xu Wednesday, January 30, 2019 5:59 AM
    Wednesday, January 30, 2019 5:39 AM
  • It DateTime.Now gets time from DateTime.UtcNow which uses GetSystemTimeAsFileTime() API which is not a precise time function. Note that there are 10 to 50ms difference to the actual time anyway.

    To get more precise time, since I don't remember any .NET APIs call precise time functions, you may want to P/Invoke call to GetSystemPreciseTimeAsFileTime() as suggested by the linked blog entry or the other 2 functions. (Note that those higher precision time APIs often need more time to return)

    Edit: When StopWatch.IsHighResolution is set to true, it'll use QueryPerformanceCounter() internally which is one of those higher precision time functions.

    Wednesday, January 30, 2019 5:56 AM
    Answerer
  • Thank you Viorel_. It works for me .
    Wednesday, January 30, 2019 5:59 AM