Android Question Now convert OLE-date(double) in B4A date format?

AlexMaa

Member
Licensed User
How to convert the date of receipt of the OLE- automation.
Need an analog functions and FromOADate ToOADate

---------
in MS vb.net there is
Returns object DateTime, an equivalent set, choose OLE- automation.
---------
Dim instance As DateTime
Dim returnValue As Double
returnValue = instance.ToOADate ()
DateTime.FromOADate (122.11)
 

DonManfred

Expert
Licensed User
Can you post an example of a date / time string and the ole value?
Google-findings:

B4X:
double d = 40880.051388 ;
DateTime dt = DateTime.FromOADate(d);
The value is an offset in days from December 30th, 1899. So you want:
B4X:
new DateTime(1899, 12, 30).AddDays(40880.051388)
Also found this:
DateTime.FromBinary() deserializes a .NET-specific serialized version of DateTime. It works only with the binary data produced from a DateTime.ToBinary() call, not with any standard input data.

There is no Java equivalent, because the .NET DateTime class doesn't exist in Java.

If you are trying to save a .NET DateTime object to a binary format, and read it in to a Java application, you should be using a different format. For example, seconds since the UNIX epoch.
 
Last edited:

AlexMaa

Member
Licensed User
There is an application server. It cannot be changed.
The server sends data -> time values in the OLE format.
In the app (Android) receive data - now they need to convert.
I need a function to convert times from OLE in B4A date format.

please need help


----------
OLE -> Date
42119.1810185185 = 04-25-2015 4:20:40
12119.18 = 03-06-1933 4:19:12
----------
in VB6
CDate(42119,1810185185)
CDbl(CDate(04-25-2015 4:20:40))

----------
http://www.fxcodebase.com/documents/IndicoreDev/cpp/oledate.html
https://msdn.microsoft.com/en-us/library/system.datetime.tooadate.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2003/09/05/54806.aspx
 

DonManfred

Expert
Licensed User
Have you tried @DonManfred code above?
The code i found was a c# code if i remember correctly
I´m not sure if that works (not tried)

DateTime.FromBinary() deserializes a .NET-specific serialized version of DateTime. It works only with the binary data produced from a DateTime.ToBinary() call, not with any standard input data.

There is no Java equivalent, because the .NET DateTime class doesn't exist in Java.

If you are trying to save a .NET DateTime object to a binary format, and read it in to a Java application, you should be using a different format. For example, seconds since the UNIX epoch.
i posted this too in the hidden hope than you, @Erel maybe can find a way as if i remember correctly b4a is written in c# so you should be able to use the code i posted.

I´m not aware of an solution. I just did a little bit google-search.
 

Erel

Administrator
Staff member
Licensed User
This code works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(DateUtils.TicksToString(OleDateToTicks(42119.1810185185)))
   Log(DateUtils.TicksToString(OleDateToTicks(12119.18)))
End Sub

Sub OleDateToTicks(ole As Double) As Long
   Dim origin As Long = DateUtils.SetDate(1899, 12, 30)
   origin = DateTime.Add(origin, 0, 0, Floor(ole))
   origin = origin + DateTime.TicksPerDay * (ole - Floor(ole))
   Return origin
End Sub
However there are several assumptions here about the time zone (which you didn't provide any information about).
 

AlexMaa

Member
Licensed User
Thank you
---------
there is small problem - but this not critical

42119.181015 = 04/25/2015 04:20:39
VB Return
42119.181015 = 04-25-2015 4:20:40
---------
(ole - Floor(ole)) = 0.18101500000193482

calculation double calls inaccuracy at one second
(not always)
 

AlexMaa

Member
Licensed User
Thank you, Erel

What about the inverse function?
Sub TicksToOleDate(ticks As Long) As Double
 
Last edited:

Erel

Administrator
Staff member
Licensed User
This code is correct if you assume that all the time instances are GMT times:
B4X:
DateTime.SetTimeZone(0)
Log(TicksToOleDate(OleDateToTicks(42119.1810185185)))
Log(TicksToOleDate(OleDateToTicks(12119.18)))


Sub TicksToOleDate(ticks As Long) As Double
   ticks = ticks - DateUtils.SetDate(1899, 12, 30)
   Return ticks / DateTime.TicksPerDay
End Sub

Sub OleDateToTicks(ole As Double) As Long
  Dim origin As Long = DateUtils.SetDate(1899, 12, 30)
  origin = DateTime.Add(origin, 0, 0, Floor(ole))
  origin = origin + DateTime.TicksPerDay * (ole - Floor(ole))
  Return origin
End Sub
 
Top