C
C#2y ago
garrus_j

❔ 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:
"Date": "2022-10-30T00:00:00",
"Base": 1173.386,
"TimeSpans": [
{
"TimeSpan": "00:00-01:00",
"Value": 1030.94
},
{
"TimeSpan": "01:00-02:00",
"Value": 1009.4
},
{
"TimeSpan": "02:00-02:00",
"Value": 1006.71
},
{
"TimeSpan": "dst",
"Value": 1003.41
},
{
"TimeSpan": "03:00-04:00",
"Value": 984.24
},
{
"TimeSpan": "04:00-05:00",
"Value": 985.68
},
{
"TimeSpan": "05:00-06:00",
"Value": 990.94
},
"Date": "2022-10-30T00:00:00",
"Base": 1173.386,
"TimeSpans": [
{
"TimeSpan": "00:00-01:00",
"Value": 1030.94
},
{
"TimeSpan": "01:00-02:00",
"Value": 1009.4
},
{
"TimeSpan": "02:00-02:00",
"Value": 1006.71
},
{
"TimeSpan": "dst",
"Value": 1003.41
},
{
"TimeSpan": "03:00-04:00",
"Value": 984.24
},
{
"TimeSpan": "04:00-05:00",
"Value": 985.68
},
{
"TimeSpan": "05:00-06:00",
"Value": 990.94
},
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
garrus_j
garrus_j2y ago
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 date
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
garrus_j
garrus_j2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts
❔ Creating a seperate .net project that serves as a backend for my react webappI want to use dotnet to serve as my backend for my webapp, although I have no idea how to set it up ❔ ✅ Generics 'where' : Limitations of what types are allowsWhen I'm specifying what types are allowed, is it possible to say anything about what types are *not❔ Why Serilog doesn't loggin into a MSSQL DatabaseHi dear friends, I'm working on a solution that contains 5 projects, ( `Core`, `IdentityCore`, `EFCo✅ ASP.NET API (EF Core) RelationshipsUsing the following example in my learnings, I'm struggling to make this work the way I want it to. ❔ How to reference a list of objects that derive from 'Class<T>', if T is different for all of them?I have a manager class, Manager<T>. The child classes for this set T to their own type, due to a fun✅ How to flush NLog on application exit with MELHi, I am using Microsoft.Extension.Logger (MEL) ILogger abstraction, and NLog as my provided impleme❔ changing values in .csv file with file i/o commandshello, i'm trying to replace a certain column in a csv file with file i/o commands but i can't quite✅ CORS: Cross-Origin Request BlockedHello, I'm trying to figure out how to configure CORS on my .NET Web Api Application. The Api Appli✅ How do I move a Window in WinUI 3 C#?Hey everyone! I have a window that is shaped like a taskbar, so it's {screenWidth} x 50px tall. Curr❔ VIsual Studio 2022 Spell CheckingWhen I just opened visual studio 2022 today, it randomly just started doing spell checking on my cod