1

First of all, sorry about my ignorance and my awful english skills, i work to improve them.So here goes my question:

I want use DateTime.Ticks (instead Guid.NewGuid) in order to calculate an identifier and a question is being raised to me. In my current culture we have 2 days on the year when we change the official time: in octuber we add an hour and in april we remove it. how does it affect to the ticks value? how ticks value is calulated? As far as i understand based on https://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx it seems it is not able to be a repeat value because based on the text (...)It does not include the number of ticks that are attributable to leap seconds(...) . there could be repeated ticks?, (maybe other question would be how long a tick lasts, depends on the computer? ) If i'm not wrong it cant be repeated.

Moreover, Maybe there could be a lot of stuff i misunderstand so i'm really sorry again...

MirlvsMaximvs
  • 1,453
  • 1
  • 24
  • 34
  • 1
    If your motivation is to ensure sequential identifiers, you may want to consider using [`UuidCreateSequential`](http://stackoverflow.com/a/9538870/18192). – Brian Apr 08 '15 at 15:54

1 Answers1

7

Even without DST changes, you can observe DateTime.Now.Ticks returning the same value multiple times, due to the granularity of the system clock.

But with DST changing, if you use DateTime.Now, you will indeed see the same values repeating for one hour per year. The Ticks property is just "the number of ticks since the DateTime epoch, in whatever kind of value is represented". (A DateTime value can be one of three "kinds" - universal, local, or unspecified. It's all a bit of a mess.)

If you use DateTime.UtcNow you shouldn't see that if your system clock only moves forward... but it's entirely possible for system clocks to be changed, either manually or in an automated way, to correct them for drift. Basically, it's not a good source of uniqueness on its own.

(In terms of the length of a tick - the tick in DateTime is always 100ns. That's not true for Stopwatch, where you need to use the Frequency property to find out how long a tick is, or use the Elapsed property to just find the elapsed time as a TimeSpan.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So if i use utf no and if i use System.DateTime.Now.Ticks yes? – MirlvsMaximvs Apr 07 '15 at 10:18
  • @Mirlo: Yes and no to *what*, exactly? – Jon Skeet Apr 07 '15 at 10:23
  • If the system time was never changed var x = System.DateTime.UtcNow.Ticks; -->might not be repeated values because of DST AND var y = System.DateTime.Now.Ticks yes – MirlvsMaximvs Apr 07 '15 at 10:57
  • @Mirlo: That doesn't really clarify your question from the previous comment. As I've said in my answer, you can still get repeated `Ticks` values when using `UtcNow`, due to both clock granularity and clock adjustment. – Jon Skeet Apr 07 '15 at 10:59
  • If system clock never change and if we doesn't regard the granularity var x = System.DateTime.UtcNow.Ticks; -->can't be repeated values and, var y = System.DateTime.Now.Ticks -->can be repeated values because of the DST ? – MirlvsMaximvs Apr 07 '15 at 11:04
  • 1
    @Mirlo: Yes, that's right. But those assumptions don't seem like useful ones to rely on in the real world :) – Jon Skeet Apr 07 '15 at 12:00
  • Ok, i know it's very probably Ticks member It's not a good source of uniqueness on its own but I was curious about that. thank you very much, – MirlvsMaximvs Apr 07 '15 at 12:15