-1

I have a problem with the difference in getting the time which has arisen this week, possibly due to Daylight Savings Time in the U.S. There is an hour difference in C# and Java (Android). I want the Java to behave the same as the C#.

Windows 10 - C#

value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
System.Diagnostics.Trace.WriteLine("span: " + (int)span.TotalSeconds);

Unix epoch seconds:
1489764265
Which converts to:
Fri, 17 Mar 2017 15:24:25 GMT

Java - Android Studio - Run on Samsung T5330-NU

Date date = new Date(); // current date and time
Integer.toString(ConvertToTimestamp(date))

Unit epoch seconds:
1489760686
Which converts to:
Fri, 17 Mar 2017 14:24:46 GMT

The seconds (:25 vs :46) is fine. The hours are the problem (15 vs 14) Somehow daylight savings time must be involved.

What can be done?

Using this to convert Unix epoch time:
http://www.onlineconversion.com/unix_time.htm

Edit:
Original Java code gets current date/time like so:

Calendar cal = Calendar.getInstance();

Based on the comment by @AkosNagy, I tried:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MST"));

and

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MDT"));

but there is no difference.

Edit:
Changed the title because this is in the end a question about C# and why the C# is not giving the correct time.

Edit: This is the line I was looking for:

Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • 1
    What's the value of `value`? – Andy Turner Mar 17 '17 at 14:41
  • added code to show – Al Lelopath Mar 17 '17 at 14:43
  • Maybe these can help? http://stackoverflow.com/questions/10545960/how-to-tackle-daylight-savings-using-timezone-in-java http://stackoverflow.com/questions/17447481/daylight-saving-ignored-when-creating-a-java-date-using-new-dateadate-gettime One of the answer is from Jon Skeet, so it must be good :) – Akos Nagy Mar 17 '17 at 14:45
  • `new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()` isn't returning what you think it is. You need to specify the timezone, somehow (I'm not a C# guy, I don't know how). Remember that 1970/1/1 00:00:00.000 in London is actually 1970/1/1 01:00:00.000 UTC. – Andy Turner Mar 17 '17 at 14:52
  • @AndyTurner: Thanks for your reply. Unfortunately I can't change the C# code, only the Java/Android. – Al Lelopath Mar 17 '17 at 14:54
  • @AlLelopath your C# code is wrong, or your computer's clock is wrong: it isn't Fri, 17 Mar 2017 15:24:25 GMT yet. – Andy Turner Mar 17 '17 at 14:55
  • 1
    General guidelines for DateTime calculations (esp. for places that have daylight saving time): https://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_sort – ErrCode Mar 29 '17 at 23:57

2 Answers2

3

new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime() isn't returning what you think it is. You need to specify the timezone:

new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()

1970/1/1 00:00:00.000 in London was actually 1970/1/1 01:00:00.000 UTC (we were in permanent daylight savings at Unix epoch).


Or your computer's clock/timezone may be wrong. After all, you posted your code before Fri, 17 Mar 2017 15:24:25 GMT (it was about Fri, 17 Mar 2017 14:40:00 GMT).

But in any case, it makes the code more robust to specify the UTC timezone, since you won't be dependent upon the local computer's configuration.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I've tried as you've suggest in C#. It behaves the same. Hmph. – Al Lelopath Mar 17 '17 at 14:59
  • Have you checked your computer's clock? – Andy Turner Mar 17 '17 at 14:59
  • The computer is adjusted for Daylight Savings Time. The C# and the computer time match (and are correct). The Java/Android is an hour behind. The Android device time matches the C# and computer. – Al Lelopath Mar 17 '17 at 15:03
  • I'll say again: *it isn't Fri, 17 Mar 2017 15:24:25 GMT yet*. London is not yet in daylight savings, and it's currently 15:03 GMT by my watch. – Andy Turner Mar 17 '17 at 15:03
  • Hmmm, you're right. Let me think ... Still the code you suggested makes no difference. That puzzles me. – Al Lelopath Mar 17 '17 at 15:05
  • As stated, the 2 lines return the same value (inocrrect, 1 hour ahead). Would the result of the 2nd be different if we were in Standard time (as opposed to Daylight Savings Time, as we are now.) – Al Lelopath Mar 23 '17 at 14:54
1

(CHECK EDIT BELOW)

In c#, DateTime.Now returns the local time of your computer so time zones don't matter, as you can see in MSDN documentation.

For java, I found these code snippets which use other methods like LocalDateTime.now().

EDIT:

after reading your answer again, I understood that your problem is with the c# code. that is my solution: instead of that Unix epoch seconds thing:

value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
System.Diagnostics.Trace.WriteLine("span: " + (int)span.TotalSeconds);

just use this:

//first, get the corrent time
DateTime date = DateTime.Now;
//print the time
Console.WriteLine(date.ToString());
// For en-US culture, for other culture type refer to MSDN link
//output mm/dd/yyyy hh:mm:ss AM/pm

If you realy need the Unix epoch seconds, refer to this thread also check your system clock.

Hope I helped!

EDIT: I changed the link to what you needed

Community
  • 1
  • 1
itay_421
  • 313
  • 2
  • 9
  • Do you mean `date.ToString()`, not `date1`? – Al Lelopath Mar 29 '17 at 15:42
  • Not exactly. I do need Unix epoch seconds. Following the link you provided I [ended up here](http://stackoverflow.com/questions/17632584/how-to-get-the-unix-timestamp-in-c-sharp) and the line of code in the accepted answer is what I need. Please put that link in your answer and I'll accept it. – Al Lelopath Mar 29 '17 at 17:03
  • I changed the link in answer – itay_421 Mar 29 '17 at 18:59