Function that creates a timestamp in c#
Asked Answered
D

6

102

I was wondering, is there a way to create a timestamp in c# from a datetime? I need a millisecond precision value that also works in Compact Framework(saying that since DateTime.ToBinary() does not exist in CF).

My problem is that i want to store this value in a database agnostic way so i can sortby it later and find out which value is greater from another etc.

Dost answered 21/5, 2009 at 9:28 Comment(1)
The accepted answer here gives a nice solution, but if you want a real timestamp, check out this Q/A: #9814560Astrict
P
207

I always use something like the following:

public static String GetTimestamp(this DateTime value)
{
    return value.ToString("yyyyMMddHHmmssfff");
}

This will give you a string like 200905211035131468, as the string goes from highest order bits of the timestamp to lowest order simple string sorting in your SQL queries can be used to order by date if you're sticking values in a database

Platitudinize answered 21/5, 2009 at 9:37 Comment(4)
How come you get 21 months and only get 12? :)Phippen
The token for year should be lowercase here: return value.ToString("yyyyMMddHHmmssffff");Altostratus
@Platitudinize The question asks for millisecond precision, so you need 3 'f's at the end. Your 4 'f's give 100 microsend precision.Palgrave
Be aware, that Compact Framework does not transfer the milliseconds. They will always be 0. You have to mod the code and add something like this: int tick = Environment.TickCount % 1000; int ms = (tick >= MOffset) ? (tick - MOffset) : (1000 - (MOffset - tick)); ms = Math.Min(999, Math.Max(0, ms));Dateless
I
44

I believe you can create a unix style datestamp accurate to a second using the following

//Find unix timestamp (seconds since 01/01/1970)
long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
ticks /= 10000000; //Convert windows ticks to seconds
timestamp = ticks.ToString();

Adjusting the denominator allows you to choose your level of precision

Intramuscular answered 18/6, 2010 at 3:8 Comment(0)
A
20

You could use the DateTime.Ticks property, which is a long and universal storable, always increasing and usable on the compact framework as well. Just make sure your code isn't used after December 31st 9999 ;)

Afterwards answered 21/5, 2009 at 9:32 Comment(11)
When you say "always increasing" - there's no guarantee that two calls to DateTime.UtcNow.Ticks will give different values though, is there? In other words you still need some caution before you use it as a unique timestamp.Obedient
actually i just used it and i have duplicate values at some points.Dost
@Jon: with always increasing I meant: it's not a tickcount which flips every 48 days or so, it's a real tick count which increases over time, so a higher value is always a later timestamp.Afterwards
@Konstantinos: you can't avoid duplicates if you use timestamps. Timestamps aren't used for unique keys, but for marking a time. You said you needed millisecond precision, and ticks has 100ns precision. The thing is that you will have duplicates. If you don't want that you need a unique sequence in the DB, which is unfortunately not db agnostic.Afterwards
"it's a real tick count which increases over time". But the system clock can go backwards - e.g. at the end of daylight saving time if you are using local time, or because the system clock was adjusted.Girish
@Joe: true. Though isn't any other timestamp which isn't based on a central sequence but on the actual time suffering from that same drawback?Afterwards
@Frans: absolutely. I only made the comment because people should think about the implications of the solution they choose. For example I'd use UTC rather than local time to at least eliminate the problems of DST.Girish
@Frans Bouma The thing is that it gave me duplicated for a sequential operation that had at least 0.3 second + difference between it. I don't know if this is a compact framework peculiarity but it's strange since you say that it has a 100ns precision to give me duplicate values for sequential operations that are not that fast between the timestamp recordings...Dost
@konstantinos: hmm... the docs say it has 100ns precision, so it's odd indeed that it has not that precision as documented. Perhaps it's the compact framework indeed, it's not a framework without bugsAfterwards
@Frans: I know this is old but... 100ns precision is not the same as 100ns accuracy. It may give you values with enough digits to see the 100ns space but the Windows system clock is usually only accurate to somewhere between 1 and 15 milliseconds. In other words, nanosecond-precision is a little useless. However, Konstantinos' time with 0.3 second interval is a little weird (because it is far more than 15ms).Hectare
Not weird at all since DateTime.Now in the Compact Framework is tied to the OS real time clock, which on most hardware only has a 1-second resolution. Device OEMs can make it high resolution, but very few choose to.Ululate
A
5

You can also use

Stopwatch.GetTimestamp().ToString();
Asserted answered 30/6, 2013 at 18:31 Comment(0)
L
4

when you need in a timestamp in seconds, you can use the following:

var timestamp = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
Lamentation answered 2/3, 2017 at 7:31 Comment(0)
F
2

If you want timestamps that correspond to actual real times BUT also want them to be unique (for a given application instance), you can use the following code:

public class HiResDateTime
{
   private static long lastTimeStamp = DateTime.UtcNow.Ticks;
   public static long UtcNowTicks
   {
       get
       {
           long orig, newval;
           do
           {
               orig = lastTimeStamp;
               long now = DateTime.UtcNow.Ticks;
               newval = Math.Max(now, orig + 1);
           } while (Interlocked.CompareExchange
                        (ref lastTimeStamp, newval, orig) != orig);

           return newval;
       }
   }
}
Fletafletch answered 16/1, 2013 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.