C
C#14mo ago
mrmiyagi

❔ DateTime?

CreationTimestamp = reader.GetDateTime(04),
TransmissionTimestamp = reader.IsDBNull(09) ? (DateTime?)null : reader.GetDateTime(09),
.
.
.
.
sb.Append($"(to_timestamp('{this.CreationTimestamp.ToLocalTime():yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS'), to_timestamp('{this.TransmissionTimestamp.ToLocalTime():yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS');");
CreationTimestamp = reader.GetDateTime(04),
TransmissionTimestamp = reader.IsDBNull(09) ? (DateTime?)null : reader.GetDateTime(09),
.
.
.
.
sb.Append($"(to_timestamp('{this.CreationTimestamp.ToLocalTime():yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS'), to_timestamp('{this.TransmissionTimestamp.ToLocalTime():yyyy-MM-dd HH:mm:ss}', 'YYYY-MM-DD HH24:MI:SS');");
I am trying to convert both of these dates to local time. It works CreationTimestamp but not for TransmissionTimestamp. The error given is something along the line of: "DateTime? doesnt have any reference to ToLocalTime".
9 Replies
Pobiega
Pobiega14mo ago
DateTime is a value type, so you need use TransmissionTimestamp.Value.ToLocalTime() because DateTime? is actually Nullable<DateTime> however, DateTime is a problematic data type to begin with, as it carries no real information about timezones, so you need to make a lot of assumptions about what timezones you are using
mrmiyagi
mrmiyagiOP14mo ago
It works AND I understand it. Thx thx thx pepepray
Pobiega
Pobiega14mo ago
alr. Be careful 🙂 and prefer DateTimeOffset for all code where you can use it
mindhardt
mindhardt14mo ago
$datetime
MODiX
MODiX14mo ago
$datetimelie
mindhardt
mindhardt14mo ago
$datetimelie
MODiX
MODiX14mo ago
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 User14mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord14mo 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