views:

11885

answers:

4

Hi there,

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.

+4  A: 

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 ;)

Frans Bouma
hmmm interesting, i ll try it out.
Konstantinos
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.
Jon Skeet
actually i just used it and i have duplicate values at some points.
Konstantinos
@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.
Frans Bouma
@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.
Frans Bouma
"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.
Joe
@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?
Frans Bouma
@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.
Joe
@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...
Konstantinos
@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 bugs
Frans Bouma
+15  A: 

I always use something like the following:

private String GetTimestamp(DateTime value) {
    return value.ToString("yyyyMMddHHmmssffff");
}

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

RobV
How come you get 21 months and only get 12? :)
PaulB
mistype on my part, corrected now
RobV
The token for year should be lowercase here: return value.ToString("yyyyMMddHHmmssffff");
Don Cote
good catch, corrected now, thanks
RobV
A: 

Remember that you canno't use the time stamp for date time!

private String GetTimestamp(DateTime value) {
    return value.ToString("yyyyMMddHHmmssffff");
}

remember that seconds go from 0 to 60!

This code is conceptually more correct and usable for your time span purpose. You can store the date time now and whenever you want you can find the Millisecond, Seconds, Day or whichever date unit you want to know how many time is passed.

var start = DateTime.Now;
//do your operation here!
var timeSpan = GetTimestamp(start);

private static int GetTimeSpan(DateTime value)
{
    return DateTime.Now.Subtract(value).Milliseconds;
}
Angelo N.
+1  A: 

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