-1

I have spent quite a few hours and still unable to understand this:

Dim unix_time_at_midnight As Long
DateTime.DateFormat = "MM/dd/yyyy"   
unix_time_at_midnight = DateTime.DateParse(DateTime.Date(unix_time*1000))/1000

where both unix_time_at_midnight and unix_time are long values. I understand DateTime.DateParse excepts a String and converts it to DateTime. What is (DateTime.Date(unix_time*1000))/1000 returning and what is its equivalent in Java? The requirement is to get the number of seconds since GMT midnight and I have successfully implemented it in Java. However, I would like to understand this particular line of code written in VB.net

EDIT: This method was written in Basic4Android and probably constitutes more of its libraries then vb.net. However, I have looked into each for details but unable to understand. Would appreciate if you could elaborate. Please see the links.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Sarah
  • 1,895
  • 2
  • 21
  • 39
  • Looks like an extension method. The `Date` property on `DateTime` is read only. – Oded Jan 20 '13 at 08:59
  • In that case, it probably expects a number in milliseconds. – Mr Lister Jan 20 '13 at 09:03
  • Yes, @MrLister - but that also means that there is no way we can tell what it actually does. Sarah - you will need to look into the source code of the extension method yourself. Perhaps post it here if you don't understand it. – Oded Jan 20 '13 at 09:04
  • @Oded I have edited my question based on your comments, does it help? – Sarah Jan 20 '13 at 09:16
  • @Sarah - What don't you understand? The documentation in the page you linked to for `Basic4Android` for this method seems to explain what it does. – Oded Jan 20 '13 at 09:45
  • @Oded I don't understand what does this return: (DateTime.Date(unix_time*1000))/1000 considering DateTime.DateParse accepts a string value while unit_time is long and is DateTime.DateParse equivalent to Date in java? – Sarah Jan 20 '13 at 09:52

1 Answers1

1

Take this:

DateTime.Date(unix_time*1000)

The documentation says:

Date (Ticks As Long) As String

Returns a string representation of the date (which is stored as ticks). The date format can be set with the DateFormat keyword.

So that part returns a string representing the date.

It then uses DateTime.DateParse, which is documented as:

DateParse (Date As String) As Long

Parses the given date string and returns its ticks representation.


Taken together, this appears to take the ticks, multiplied by 1000, converted to a string that doesn't contain hour information which is parsed back to ticks which are divided by 1000.

The important thing to note is that the DateFormat set on the line before contains only the formatting for the date, no hours/minutes/seconds and smaller units of time exist in it. This means that the string returned essentially represents midnight of that date.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I am sorry for the continuous questions but if the string returned is the midnight of a date, then unix_time_at_midnight which is a long value represents what? What is DateTime.Date(unix_time*1000) in java? – Sarah Jan 20 '13 at 10:31
  • @Sarah - [Unix time](http://en.wikipedia.org/wiki/Unix_time) is measured in seconds from midnight, January 1st, 1970. That's what the `ticks` represent. What is it in Java? I though you were asking about VB.NET? It would be similar and I believe JodaTime can handle Unix epoch ticks. – Oded Jan 20 '13 at 10:38