❔ DateTime help with NodaTime
So I receive timeseries data from an API which I then save in a db. The API delivers the data in hourly timespans, which are always using the Central European Standard Time zone. I want to save it in UTC format in the database, but daylight savings is giving me a real headache..
Take October 30th 2022, the data looks like this:
I'm using NodaTime to try and handle this. I parse a DateTime object using the Date and the second half of the TimeSpan, but this way I always end up with a double instance of 2022-10-30T02:00:00, and this will always be converted to 2022-10-30T01:00:00 UTC. What I need is for the first instance to become 2022-10-30T00:00:00 to be correct in UTC terms. Anyone have any smart ideas?
5 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
public static DateTimeOffset ConvertCETToUTC(this DateTime cetTime)
{
// var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
//
// bool isDST = dateTimeCET.IsDaylightSavingTime();
// DateTimeOffset dateTimeOffsetUTC = TimeZoneInfo.ConvertTimeToUtc(dateTimeCET, timeZoneInfo);
var cetInstant = Instant.FromDateTimeUtc(cetTime.ToUniversalTime());
var cetZone = DateTimeZoneProviders.Tzdb["Europe/Berlin"];
var cetZoned = new ZonedDateTime(cetInstant, cetZone);
var utcZoned = cetZoned.ToInstant().InUtc();
DateTime utcTime = utcZoned.ToDateTimeUtc();
return utcTime;
}
thats what I've got so far, it works, but obviously it can't differentiate the first and second instance of 2am on that dateUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
It is a full DateTime object, which is the Date in the json plus the second hour of the timespan joined together. So 2022-10-30T00:00:00 plus 01:00 would be the first item, or 2022-10-30T01:00:00
Because I want the end of every hourly period to be a full datetime object in the database. I don't know why the api doesnt just deliver it that way
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.