How to declare DateTime property with proper DateTimeKind?
I want to have something like this:
[Kind=DateTimeKind.Utc]
public DateTime CreatedOn { get; set; }
Is this possible to set Kind declaratively like that? What is a proper syntax? I have stub classes to use in my JSON WCF Webservice and when other end (Java client) get's dates - they all like:
123456000-0500
Where "-0500" is my timezone and if I take only left part then it's not correct UTC date(I have to substract 5 hours). I'd rather fix it on server.
EDIT:
I save all dates in database (SQL Server) as UTC. I never deal with local dates except when need to display it.
I have simple class which I pass as response in my WCF service - JSON. When I populate this class - I query database with EF and assign values in this class.
On client, which is Android phone with Java - I use Gson library to deserialize. Gson can't deserialize WCF format, so I already wrote my ugly deserializer like this:
public class GsonDateDeserializer implements JsonDeserializer { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseE开发者_StackOverflow社区xception { String s = json.getAsString(); s = s.replace("/Date(", ""); s = s.replace(")/", "");
//if there us no data passed in - that means NULL if (s.equals("")) return null; //If we got timezone info with this date - cut it off if (s.length() > 5 && (s.indexOf("-") == s.length()-5 || s.indexOf("+") == s.length()-5)) { s = s.substring(0, s.length()-5); } Long l = Long.valueOf(s); return new Date(l); }
}
I thought that WCF passes "-0500" only for information so client knows which timezone it should be converted to. But no. It passes left number with 5 hours added. So, I don't want server to be "smart" and just want it to pass all dates as "-0000"
Well, that date is a proper ISO 8601, so the other end should accept it. If you can't change that, or you don't want to, you can do the following:
private DateTime m_createdOn;
public DateTime CreatedOn
{
get { return m_createdOn; }
set { m_createdOn = value.ToUniversalTime(); }
}
EDIT: You mean the date with timezone is wrong? In that case, you should probably let your database layer create the dates with the proper DateKind
.
I ended up finishing my own function. Now when I deserialize I take into account time zone portion.
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
String s = json.getAsString().replace("/Date(", "").replace(")/", "");
//if there us no data passed in - that means NULL
if (s.equals("")) return null;
//If we got timezone info handle separately:
long offset = 0;
if (s.length() > 5 && (s.indexOf("-") == s.length()-5 || s.indexOf("+") == s.length()-5))
{
//get offset minutes
offset = Long.valueOf(s.substring(s.length()-4, s.length()-2))*60 + Long.valueOf(s.substring(s.length()-2, s.length()));
//Apply direction
if (s.indexOf("-") == s.length()-5) offset = -offset;
//Cutoff offset
s = s.substring(0, s.length()-5);
}
return new Date(Long.valueOf(s) + offset * 60 * 1000);
}
精彩评论