Author Topic: Another CLR question  (Read 133 times)

stanl

  • Pundit
  • *****
  • Posts: 1572
Another CLR question
« on: October 15, 2021, 10:14:54 am »
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

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




td

  • Tech Support
  • *****
  • Posts: 3945
    • WinBatch
Re: Another CLR question
« Reply #1 on: October 15, 2021, 01:40:53 pm »
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
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

  • Pundit
  • *****
  • Posts: 1572
Re: Another CLR question
« Reply #2 on: October 16, 2021, 04:05:13 am »
Thanks.  Did some digging into some 2018 snippets I had stuffed in a folder. Below is a bit crude, but workable
Code: Winbatch

;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

  • Tech Support
  • *****
  • Posts: 3945
    • WinBatch
Re: Another CLR question
« Reply #3 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1572
Re: Another CLR question
« Reply #4 on: October 17, 2021, 04:10:58 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

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

td

  • Tech Support
  • *****
  • Posts: 3945
    • WinBatch
Re: Another CLR question
« Reply #5 on: October 17, 2021, 07:19:45 am »
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