RFC3339 to DateTime

Title says it, I can't find anything online for RFC3339 to Datetime, only the reverse
18 Replies
Angius
Angius3mo ago
Isn't RFC3339 just spicy ISO-8601? If so, then DateTime.Parse() will handle it If that doesn't work, DateTime.ParseExact()
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
phantomlord127
phantomlord1273mo ago
Just out of curiosity: why DateTimeOffset?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo ago
$datetimelie
please please please get into the habit of using DateTimeOffset instead of DateTime in your code everywhere you possibly can. DateTimeOffset contains the UTC offset with the data so is lossless. DateTime is very very weird in how it stores data. DateTime only knows UTC and "local" time. But your local time zone can change! Laptops easily move across state lines, even while an app is currently running. (So you are in California running an app, put some DateTime instance in memory, hop on a plane to New York, then resume your app 3 time zones ahead. What on earth will be contained within the DateTime instance?) But wait, this was a lie. DateTime actually has 3 ways it keeps track of time: UTC, local (which can change while an app is running), and unspecified (unknown, essentially). (Unknown = I don't know if it's UTC or local, the app will figure it out later and call ToUniversal or ToLocal before calling ToString or any comparison routine.) But wait, this was a lie. DateTime actually has a secret fourth way of storing data that's not exposed in the standard API surface! It's truly an abomination of a type. All of this nonsense is irrelevant if we just pretend DateTime doesn't exist and we instead use DateTimeOffset everywhere.
- GrabYourPitchforks (start: https://discord.com/channels/143867839282020352/143867839282020352/988353756108312607)
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
phantomlord127
phantomlord1273mo ago
Thank you for the info. Didn't know that
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
phantomlord127
phantomlord1273mo ago
You mean my timezone? It's UTC + 2
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Core
Core3mo ago
Sorry, but what if I'm using DateTime everywhere because I'm only working with UTC Is that still a bad thing?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo ago
TeBeCo
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
Quoted by
<@689473681302224947> from #web (click here)
React with ❌ to remove this embed.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Core
Core3mo ago
Yes, that is right
phantomlord127
phantomlord1273mo ago
I think currently it's a format for me. I didn't know about Datimeoffset or the RFC Document.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server