Another CLR question

Started by stanl, October 15, 2021, 10:14:54 AM

Previous topic - Next topic

stanl

First, I know, have used, and contributed to basic WB Date/Timestamp processing. But played around with the ObjectClrNew('System.DateTime') structure and found it very versatile. Recently, having to do a variety of Unix timestamp conversions I tried to investigate  ObjectClrNew('System.DateTimeOffset') and it's FromUnixTimeSeconds method I get a value not in accepted range error. I looked and DateTime is 32-bit while DateTimeOffset is 64-bit [but even running 64-bit studio still generates error.


Again, not a show stopper...
Code (WINBATCH) Select


ObjectClrOption('useany', 'System')
oTime = ObjectClrNew('System.DateTimeOffset')
;oTime = ObjectClrNew('System.DateTime')





td

It has nothing to do with 32 vs 64  bit. The DateTimeOffset structure requires parameters but the FCL does not accept any parameters that WinBatch passes to the constructor. For example, passing a "DateTime" to the DateTimeOffset constructor in the call to ObjectClrNew gets an out-of-range error. This true even if you cast the  This analogous to the same issue with TimeSpan.

Code (winbatch) Select
objDateTime = ObjectClrNew("System.DateTime")
Now = ObjectClrType('System.DateTime',objDateTime.Now())

; This causes an out-of-range error.
objDTOffset = ObjectClrNew('System.DateTimeOffset',Now) ; Errors


[edit] The only way I can think of to use the structure is to create a wrapper class and compile it in-line.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Thanks.  Did some digging into some 2018 snippets I had stuffed in a folder. Below is a bit crude, but workable
Code (WINBATCH) Select


;Winbatch 2021C - convert unix timestamp to WB datetime
timestamp = 1634367411
oS = CreateObject("MSScriptControl.ScriptControl")
oS.Language = "VBScript"
oS.AllowUI = @FALSE
u=oS.Eval(: 'DateAdd("s",%timestamp%,"01-Jan-1970 00:00:00")')
oS = 0
Message("Converting ":timestamp,u)
Exit



[edit] Pushing the CLR envelope should be helpful to the WB community at large.

td

We are contemplating a new approach to CLR hosting but are not sure if it is even doable yet.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on October 16, 2021, 08:50:21 AM
We are contemplating a new approach to CLR hosting but are not sure if it is even doable yet.


Anything you want to test, let me know.  Expected you would post that there was really no need to use the MsScriptcontrol as regular WB functions would handle the conversion ;D
Code (WINBATCH) Select


timestamp = 1634367411
unixBase="1970:01:01:00:00:00"
AddTime = "0000:00:00:00:00:%timestamp%"
Later=TimeAdd(unixBase, AddTime)
Message(unixBase, Later)

td

Thanks for the offer. Not promising anything yet as this is still in the I-wonder-if-its-even-doable phase. With all the dotNet related stuff being posted on Github, it would be nice to provide WinBatch scripts with more access to the several flavors of dotNet MSFT has foisted on us all.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade