Reputation: 363
I am sorry for my formatiing.
I am receiving JSON response from the database, I need to convert the dates as per-user time zone he/she is in. I am receiving user time zone as "timezone": 5.5, or "timezone": -6.
Just for information, my application is running in Azure. I should also check daylight saving for timezones. application is developed in c#. I have tried this and this look like Nodatime is suitable for converting the date based on the timezone. I am completely new in this and try to figure out how I can change Date values in JSON based on time zone.
I have tried below code to see if dates are changing but nothing happens. I am not sure where I can put timezone. Any help is appreciated.
` var date = "2020-08-12T18:30:13.000";
var json = $"{{\"Date\": \"{date}\"}}";
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"yyyy-MM-dd HH:mm:ss z",
DateTimeZoneProviders.Tzdb
);
var settings = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None,
Converters = { new NodaPatternConverter<ZonedDateTime>(pattern) }
};
var dateObj = JsonConvert.DeserializeObject<DateObj>(json, settings);
Console.WriteLine(dateObj.Date);`
Below is JSON i am receiving
{
"entity": [
{
"JobId": 691230,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-12T18:30:16.000",
"End Time": "2020-08-12T18:32:04.000",
"Duration in Mins": 2,
"Size of Application": 315621376,
"Job Status": "Completed",
"dt": "2020-08-12T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 691230,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-12T18:30:16.000",
"End Time": "2020-08-12T18:32:04.000",
"Duration in Mins": 2,
"Size of Application": 315621376,
"Job Status": "Completed",
"dt": "2020-08-12T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 694338,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-13T12:30:15.000",
"End Time": "2020-08-13T12:32:07.000",
"Duration in Mins": 2,
"Size of Application": 282066944,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 693664,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-13T06:30:24.000",
"End Time": "2020-08-13T06:32:13.000",
"Duration in Mins": 2,
"Size of Application": 255852544,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 692966,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Windows File System",
"Instance": "N/A",
"BackupSet": "defaultBackupSet",
"Subclient": "default",
" Backup Type": "Incremental",
"Start Time": "2020-08-13T02:00:19.000",
"End Time": "2020-08-13T04:00:14.000",
"Duration in Mins": 120,
"Size of Application": 514155115394,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 692656,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-13T00:30:17.000",
"End Time": "2020-08-13T00:32:13.000",
"Duration in Mins": 2,
"Size of Application": 124780544,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 691827,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "default",
" Backup Type": "Incremental",
"Start Time": "2020-08-12T21:00:24.000",
"End Time": "2020-08-12T22:51:53.000",
"Duration in Mins": 111,
"Size of Application": 683671552,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 1,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
},
{
"JobId": 691230,
"Operation Type": "Backup",
"Client": "cnwxwdevwms01",
"Agent": "Oracle Database",
"Instance": "T0340X",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-12T18:30:16.000",
"End Time": "2020-08-12T18:32:04.000",
"Duration in Mins": 2,
"Size of Application": 315621376,
"Job Status": "Completed",
"dt": "2020-08-13T00:00:00.000",
"age": 1,
"original_0": {
"numberOfDays": "10",
"serverName": "cnwxwdevwms01"
}
}
]
}
Upvotes: 0
Views: 1224
Reputation: 447
Let me know if this can work for you.
I haven't used Nodatime as I don't think it's necessary for the purpose of timezone conversion. The problem I see is that your datetime does not contain any timezone information. .Net handles this as being Local. As the Microsoft documentation states: The time zone component of DateTimeKind.Utc date and time values uses "Z" (which stands for zero offset) to represent UTC.
Edit (per @Sanjeev Gautam's comment): As your function runs on Azure, you will need to find a way to extract the local timezone of your user. In the example below, I used UTC-9.
Specifying that your datetime are set with UTC allows you to this:
// Object Class
class DateObj
{
public DateTime Date { get; set; }
}
// JsonConverter
class DateTimeConverterTimeZone : JsonConverter<DateTime>
{
public TimeZoneInfo TimeZone { get; set; }
public DateTimeConverterTimeZone(TimeZoneInfo timezone)
{
TimeZone = timezone;
}
public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
{
writer.WriteValue(value.ToUniversalTime());
}
public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer)
{
DateTime dt = (DateTime) reader.Value;
var newDateTime= TimeZoneInfo.ConvertTime(dt, TimeZone);
return newDateTime;
}
}
//Deserialization
var date = "2020-08-12T18:30:13.000Z";
var json = $"{{\"Date\": \"{date}\"}}";
var convertedDateTime = JsonConvert.DeserializeObject<DateObj>(json, new DateTimeConverterTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC-09")));
Upvotes: 1