I am currently using NodaTime based on my frustrations dealing with timezones in C#'s DateTime
class. So far, I'm really pleased.
public static string nodaTimeTest(string input)
{
var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.Zero);
var pattern = OffsetDateTimePattern.Create("yyyy-MM-dd'T'HH:mm:sso<m>", CultureInfo.InvariantCulture, defaultValue);
var result = pattern.Parse(input).Value;
return result.ToString();
}
I have three specific questions. Above is the method I use where I parse in dateTime strings. I have a format
string which allows me how to parse the input. My questions are:
Does it matter what my LocalDateTime(..)
is? The method I used is Matt Johnson's Stack example, and his came with the date 2000, 1, 1, 0, 0
. I thought that was odd, since most date classes I know use the Epoch time 1970, 1, 1, 0 ,0
, so I changed my method to contain the Epoch date, but the outputs were the same:
How do I convert the time to a Unix timestamp? It does not appear there's a built-in method to do so.
Using this method:
public static string nodaTimeTest6(string input, int timeZone)
{
// var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.Zero);
var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.FromHours(timeZone));
var pattern = OffsetDateTimePattern.Create("yyyy-MM-dd'T'HH:mm:sso<m>", CultureInfo.InvariantCulture, defaultValue);
var result = pattern.Parse(input);
return result.Value.ToString();
}
I'm testing out the abilities of NodaTime with this method -- specifically, I was wondering if I can parse in a date/time that HAS offset defined inside, and at the same time, my timeZone
input also allows input of timezones/offsets. Interestingly enough, my input timeZone
gets ignored, so the offset in my output of nodaTimeTest6
is the input date string:
Is this desired behavior?