❔ DateTime.Now UtcNow and UnixTimestamp

Alright, DateTime.Now and DateTime.UtcNow is different because one uses UTC and other one is using local time. But how does both returns same unix timestamp ? Isn't it based on the tics passed from the date which I passed as parameter and base 1970 ?
13 Replies
realivanjxツ
realivanjxツ2y ago
timestamp is universal
realivanjxツ
realivanjxツ2y ago
realivanjxツ
realivanjxツ2y ago
datetimeoffset takes into account the timezone
atakancracker
atakancracker2y ago
DateTimeOffset.UtcNow and DateTimeOffset.Now returns same results even my local time is GMT+3 so If I want to use my local time as timestamp I have to use the old way which substract from date 1970 var nowStamp = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).TotalSeconds; that is This timestamp is converted to date time from one of services that cant handle local time conversion, therefore result is showing UTC time, not local time
canton7
canton72y ago
A unix timestamp is the time since midnight 1st Jan 1970 UTC. It's a single instant in time, and happened at the same time for everyone. So it doesn't matter what timezone you're in: you're always the same number of seconds since the unix epoch
atakancracker
atakancracker2y ago
okay, now convert those DateTimes to UnixTimestamp, results will be same even date times are different (if you are not adding 1 hours equivalent tics or using substract method)
canton7
canton72y ago
Also DateTime has a Kind property which says "Local", "Utc" or "Unknown". It's rather odd. DateTime.Now is "Local", DateTime.UtcNow is "UTC". When you convert a DateTime to a DateTimeOffset, it takes the Kind property into account. var nowStamp = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).TotalSeconds; -- that's just incorrect. Because the unix epoch is defined as being in UTC, but you're specifying "Local" Yes, as I've been saying 🙂
atakancracker
atakancracker2y ago
Okay so If I want to convert my local time to unixtimestamp equivalent, I can use something like DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + 3600000 * 3
canton7
canton72y ago
What do you mean by "unixtimestamp equivalent"?
atakancracker
atakancracker2y ago
but what is my local time changes on the server number of seconds that have elapsed since my local date and time --> thats what I try to achieve in short
var hoursDifferenceFromUtc = DateTime.Now.Subtract(DateTime.UtcNow).TotalHours;
long time = DateTimeOffset.UtcNow.AddHours(hoursDifferenceFromUtc).ToUnixTimeMilliseconds();
var hoursDifferenceFromUtc = DateTime.Now.Subtract(DateTime.UtcNow).TotalHours;
long time = DateTimeOffset.UtcNow.AddHours(hoursDifferenceFromUtc).ToUnixTimeMilliseconds();
canton7
canton72y ago
Why are you trying to do this? This isn't how Unix timestamps work And it'd be a lot easier and clearer just take a DateTimeOffset, set the offset to 0, and find the Unix timestamp for that?
atakancracker
atakancracker2y ago
Consumer service is converting timestamp to date time and uses that for logging, it used to be DateTime object with local time but since we switch to timestamp, now the logs are behind the local time Its either will be accepted to have log dates as UTC based or need workaround like this
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.