'JObject.Parse datetime value comes out different on Azure Pipeline and is failing test
I have a unit test to check a generated SQL string is getting a correct date time in it, I'm passing in a JSON string to JObject.Parse which contains this string as one of the values '2022-03-27T02:00:00+01:00'. On my local machine (UK based) this correctly comes out as 2022/03/27 02:00:00 as it's BST. However, on Azure the test fails as the output comes out as 01:00:00.
I know that we should be using UTC to pass and store dates, however, techdebt and management aren't allowing this. Is JObject.Parse at fault? How do I correct this?
EDIT: Any further explanation/resources about understanding the datetime format is appreciated, it's all making my head spin
Solution 1:[1]
As suggested by Nathon Foss, use DateTime.ToUniversalTime Method
For example as mentioned on MS doc:
using System;
class Example
{
static void Main()
{
DateTime localDateTime, univDateTime;
Console.WriteLine("Enter a date and time.");
string strDateTime = Console.ReadLine();
try {
localDateTime = DateTime.Parse(strDateTime);
univDateTime = localDateTime.ToUniversalTime();
Console.WriteLine("{0} local time is {1} universal time.",
localDateTime,
univDateTime);
}
catch (FormatException) {
Console.WriteLine("Invalid format.");
return;
}
Console.WriteLine("Enter a date and time in universal time.");
strDateTime = Console.ReadLine();
try {
univDateTime = DateTime.Parse(strDateTime);
localDateTime = univDateTime.ToLocalTime();
Console.WriteLine("{0} universal time is {1} local time.",
univDateTime,
localDateTime);
}
catch (FormatException) {
Console.WriteLine("Invalid format.");
return;
}
}
}
// The example displays output like the following when run on a
// computer whose culture is en-US in the Pacific Standard Time zone:
// Enter a date and time.
// 12/10/2015 6:18 AM
// 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
// Enter a date and time in universal time.
// 12/20/2015 6:42:00
// 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
Note:
YAML scheduled triggers are set using UTC time zone. If your scheduled triggers don't seem to be firing at the right time, confirm the conversions between UTC and your local time zone, taking into account the day setting as well.
For more details about scheduled trigger time zone, refer to Troubleshoot pipeline runs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
