patterncsharpMinor
Specifying the Kind on DateTime
Viewed 0 times
specifyingkindthedatetime
Problem
I've got a system and every object in my system has a base class with property:
In the constructor for the base class I set the
This has been fine but now I'm exposing my data as json via a web service call. As the date are UTC I want the format to have the Z suffix as below:
The DateCreated in each object has
It seems weird to me that the UTC
To get the date returned as required I've changed the property in the base class to this:
This does exactly what I need but seems kind of hacky - Is this really the best way?
After Googling someone suggests that using DateTimeOffset would be more appropriate. I'm not convinced? This would have me questioning the use of DateTime in any scenario - and always using DateTimeOffset!?!
DateTime DateCreatedIn the constructor for the base class I set the
DateCreated as below:public EntityBase()
{
Active = true;
DateCreated = DateTime.UtcNow;
}This has been fine but now I'm exposing my data as json via a web service call. As the date are UTC I want the format to have the Z suffix as below:
2016-08-01T10:39:18ZThe DateCreated in each object has
Kind set to Utc because I'm setting it using DateTime.UtcNow. However, as I load the data from the database the Kind remains as Unspecified (I assumed that the UTC part would be saved to the database but it appears not). When I return the dates via the web service function I get it without the Z suffix so the calling application doesn't know it is UTC:2016-08-01T10:39:18It seems weird to me that the UTC
Kind of the DateTime object isn't saved to the database - I thought it would be. The datatype is datetime2(7).To get the date returned as required I've changed the property in the base class to this:
private DateTime _DateCreated;
[DisplayName("Date Created")]
public DateTime DateCreated
{
get { return _DateCreated; }
set
{
_DateCreated = value;
_DateCreated = DateTime.SpecifyKind(_DateCreated, DateTimeKind.Utc);//ensure this is set
}
}This does exactly what I need but seems kind of hacky - Is this really the best way?
After Googling someone suggests that using DateTimeOffset would be more appropriate. I'm not convinced? This would have me questioning the use of DateTime in any scenario - and always using DateTimeOffset!?!
Solution
_DateCreated = value;
_DateCreated = DateTime.SpecifyKind(_DateCreated, DateTimeKind.Utc);//ensure this is setThis creates a race condition: another thread can access
_DateCreated in between the two assignments and get a non-UTC value. If you're going to do it at this level it should be_DateCreated = DateTime.SpecifyKind(value, DateTimeKind.Utc);That leaves the question of whether it should be done at this level. You haven't said how you're getting the date from the database. Can that be configured?
This would have me questioning the use of DateTime in any scenario - and always using DateTimeOffset!?!
Yes.
DateTime is one of those mistakes that everyone recognises is a mistake, but is so deeply embedded in so many libraries and tools that it's a pain in the neck to eradicate.Code Snippets
_DateCreated = value;
_DateCreated = DateTime.SpecifyKind(_DateCreated, DateTimeKind.Utc);//ensure this is set_DateCreated = DateTime.SpecifyKind(value, DateTimeKind.Utc);Context
StackExchange Code Review Q#157654, answer score: 4
Revisions (0)
No revisions yet.