Handling UTC time within C#?
I am not an expert on the internals of C# so this question might actually be downright silly. If so, please correct me. I have some data (in UTC format) taken out from a SQL server.
2011-03-26 11:03:58.000
2011-03-26 11:04:25.000
...
I am parsing this file inside C# and am using the following:
DateTime date = DateTime.Parse(value);
to fetch the value into the DateTime
object. Now, I am subtracting some arbitrary time 6 hours from this time as follows:
date = date.Subtract(new TimeSpan(6, 0, 0));
And finally, I am writing this back into another file as follows:
output.WriteLine(date.ToString("yyyy-MM-dd HH:mm:ss.fff"));
Because I have not done any implicit conversions, the output is also UTC. My question is, is this kind of addition/subtraction of time allowed on the parsed date or do I need to do some UTC to C# specific conve开发者_运维百科rsion before being able to manipulate time? I am having difficulties wrapping my head around this. Would someone please clarify this?
EDIT: Attempt to write a concise question The original date is in UTC. I want to add/subtract some time and write it back in UTC. I want to know if I can manipulate the parsed date directly or I need to do some conversions i.e. tell C# explicitly that the date is in UTC format and then manipulate it and then tell it again to write back UTC date.
It doesn't really matter. A date is a date, no matter what time zone it's in. Unless you're converting it to a different time zone, .Net doesn't know or care what time zone it is.
If you want to, you can call DateTime.SpecifyKind(value, DateTimeKind.Utc)
.
You do not need to parse datetimes from SQL Server. The ADO.Net will return a SqlDateTime
object for a datetime
type column in a result. ORM libraries (LinqToSQL, Entity Framework etc) are also perfectly capable of mapping datetime
type columns to DateTime
properties. If you find yourself parsing a string, you're doing it wrong (not to mention all the implications of SET DATEFORMAT
...)
As a note the operation you described can be performed straight on the server, eg. add one hour to an UTC datetime field and save it back as UTC datetime:
UPDATE table SET column = DATEADD(hour, 1, column) WHERE ...
Is the original date the UTC time, or your local time? If the original is local time and you are subtracting 8 hours to make it UTC, then use DateTime.ToUniversalTime(). To go the other way, use DateTime.ToLocalTime(). Or am I missing something?
Use DateTimeOffset
With the input string being universal, you can use an overload of DateTime.Parse
that accepts a DateTimeStyle
to indicate this.
string utcDateString = "2011-03-26 11:03:58.000";
DateTime localDate = DateTime.Parse(utcDateString,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal);
DateTime utcDate = localDate.ToUniversalTime();
In this snippet, localDate
would be the local time of the universal time input. So for EDT (US), it would be 7:03:58 AM. utcDate
would have 11:03:58
, matching the input string.
精彩评论