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;
}









share|improve this question




















  • 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






  • 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 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

















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;
}









share|improve this question




















  • 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






  • 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 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















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;
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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 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
















  • 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






  • 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 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










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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks