Issue calculating time difference c#
up vote
1
down vote
favorite
I have a check i run on the server and on a desktop application but the result is not consistent.
This is the code on the server to add a timestamp to a string which i send back to the desktop app. So that the returned string expires after some time and cannot be used as it is encrypted.
Server:
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime TodayDate = UnixTimeStampToDateTime(unixTimestamp);
Desktop App:
//li.tStamp this comes from the server
DateTime time = UnixTimeStampToDateTime(Convert.ToDouble(li.tStamp));
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime time2 = UnixTimeStampToDateTime(unixTimestamp);
The condition:
double SecondsDifference = time2.Subtract(time).TotalSeconds;
if (SecondsDifference > 0 && SecondsDifference < 30)
{
// DO SOMETHING HERE
}
The thing is the result is not consistent it works on some pc an not on another what am I doing wrong here?
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
c# .net datetime unix-timestamp
|
show 8 more comments
up vote
1
down vote
favorite
I have a check i run on the server and on a desktop application but the result is not consistent.
This is the code on the server to add a timestamp to a string which i send back to the desktop app. So that the returned string expires after some time and cannot be used as it is encrypted.
Server:
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime TodayDate = UnixTimeStampToDateTime(unixTimestamp);
Desktop App:
//li.tStamp this comes from the server
DateTime time = UnixTimeStampToDateTime(Convert.ToDouble(li.tStamp));
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime time2 = UnixTimeStampToDateTime(unixTimestamp);
The condition:
double SecondsDifference = time2.Subtract(time).TotalSeconds;
if (SecondsDifference > 0 && SecondsDifference < 30)
{
// DO SOMETHING HERE
}
The thing is the result is not consistent it works on some pc an not on another what am I doing wrong here?
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
c# .net datetime unix-timestamp
2
I'm suspecting that it is related to the value of theDateTimeKind
property - with the first examples it's set toUnspecified
, with the last example it's set toUtc
. I think you should set it toUtc
always.
– Zohar Peled
yesterday
2
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Why are you using.ToLocalTime();
? What if theSecondsDifference
is negative? Some computers may not be timesync
– J. van Langen
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday
|
show 8 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a check i run on the server and on a desktop application but the result is not consistent.
This is the code on the server to add a timestamp to a string which i send back to the desktop app. So that the returned string expires after some time and cannot be used as it is encrypted.
Server:
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime TodayDate = UnixTimeStampToDateTime(unixTimestamp);
Desktop App:
//li.tStamp this comes from the server
DateTime time = UnixTimeStampToDateTime(Convert.ToDouble(li.tStamp));
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime time2 = UnixTimeStampToDateTime(unixTimestamp);
The condition:
double SecondsDifference = time2.Subtract(time).TotalSeconds;
if (SecondsDifference > 0 && SecondsDifference < 30)
{
// DO SOMETHING HERE
}
The thing is the result is not consistent it works on some pc an not on another what am I doing wrong here?
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
c# .net datetime unix-timestamp
I have a check i run on the server and on a desktop application but the result is not consistent.
This is the code on the server to add a timestamp to a string which i send back to the desktop app. So that the returned string expires after some time and cannot be used as it is encrypted.
Server:
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime TodayDate = UnixTimeStampToDateTime(unixTimestamp);
Desktop App:
//li.tStamp this comes from the server
DateTime time = UnixTimeStampToDateTime(Convert.ToDouble(li.tStamp));
Double unixTimestamp = (double)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime time2 = UnixTimeStampToDateTime(unixTimestamp);
The condition:
double SecondsDifference = time2.Subtract(time).TotalSeconds;
if (SecondsDifference > 0 && SecondsDifference < 30)
{
// DO SOMETHING HERE
}
The thing is the result is not consistent it works on some pc an not on another what am I doing wrong here?
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
c# .net datetime unix-timestamp
c# .net datetime unix-timestamp
edited yesterday
Wai Ha Lee
5,640123661
5,640123661
asked yesterday
confusedMind
1,24242053
1,24242053
2
I'm suspecting that it is related to the value of theDateTimeKind
property - with the first examples it's set toUnspecified
, with the last example it's set toUtc
. I think you should set it toUtc
always.
– Zohar Peled
yesterday
2
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Why are you using.ToLocalTime();
? What if theSecondsDifference
is negative? Some computers may not be timesync
– J. van Langen
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday
|
show 8 more comments
2
I'm suspecting that it is related to the value of theDateTimeKind
property - with the first examples it's set toUnspecified
, with the last example it's set toUtc
. I think you should set it toUtc
always.
– Zohar Peled
yesterday
2
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Why are you using.ToLocalTime();
? What if theSecondsDifference
is negative? Some computers may not be timesync
– J. van Langen
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday
2
2
I'm suspecting that it is related to the value of the
DateTimeKind
property - with the first examples it's set to Unspecified
, with the last example it's set to Utc
. I think you should set it to Utc
always.– Zohar Peled
yesterday
I'm suspecting that it is related to the value of the
DateTimeKind
property - with the first examples it's set to Unspecified
, with the last example it's set to Utc
. I think you should set it to Utc
always.– Zohar Peled
yesterday
2
2
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Why are you using
.ToLocalTime();
? What if the SecondsDifference
is negative? Some computers may not be timesync– J. van Langen
yesterday
Why are you using
.ToLocalTime();
? What if the SecondsDifference
is negative? Some computers may not be timesync– J. van Langen
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday
|
show 8 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409123%2fissue-calculating-time-difference-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I'm suspecting that it is related to the value of the
DateTimeKind
property - with the first examples it's set toUnspecified
, with the last example it's set toUtc
. I think you should set it toUtc
always.– Zohar Peled
yesterday
2
The unix epoch is always UTC. That makes new DateTime(1970, 1, 1) wrong, it will be interpreted as local time. Note how UnixTimeStampToDateTime() did it right.
– Hans Passant
yesterday
Can you explain what consistency you expect ?
– Dmytro Mukalov
yesterday
Why are you using
.ToLocalTime();
? What if theSecondsDifference
is negative? Some computers may not be timesync– J. van Langen
yesterday
@J.vanLangen found this sampel online on how to convert unix timestamp to datetime.
– confusedMind
yesterday