Cant seem to Creat a CLR DateTimeOffset object

Started by galaara98, September 07, 2015, 05:46:31 PM

Previous topic - Next topic

galaara98

so i need to call a propriatry CLR function that expect a system.DateTimeOffset as on of the arguments
i am having a hella time getting one

;Convert WIL YMDHMS to a DateTime Object
arrYMDHMS = Arrayize( TimeYmdHms(), ":" )
objYear = ObjectClrNew( 'System.Int32', arrYMDHMS[0] )
objMonth = ObjectClrNew( 'System.Int32', arrYMDHMS[1] )
objDay = ObjectClrNew( 'System.Int32', arrYMDHMS[2] )
objHour = ObjectClrNew( 'System.Int32', arrYMDHMS[3] )
objMin = ObjectClrNew( 'System.Int32', arrYMDHMS[4] )
objSec = ObjectClrNew( 'System.Int32', arrYMDHMS[5] )
ObjNull = ObjectType( "NULL", 0 )


; DateTimeOffset should be able to take Y,M,D,H,M,S,TimeSpan as a constructor, but I can't get a TimeSpan
objTimeSpan = ObjectClrNew("System.Timespan",-6,0,0)
; Goes boom: Method Type Signature is not interop compatible

;also tried
objZero = ObjectClrNew( 'System.Int32', 0 )
objNegSix = ObjectClrNew( 'System.Int32', -6 )
objTimeSpan = ObjectClrNew("System.TimeSpan",objNegSix,objZero,objZero)
;same no-go

;I tried to get a timespan from a return of calling a Propertu of the TimeZoneInfo object... Still nothing
ObjTZ = ObjectClrNew( 'System.TimeZoneInfo')
ObjTimeSpan = ObjTZ.BaseUtcOffset ; didnt work = Error: Non Static Method requires a target
ObjTimeSpan = ObjTZ.Local.BaseUtcOffset; didn;t work either: Method Type Signature is not interop compatible.. back to the original Error

ObjDTOff = ObjectClrNew( 'System.DateTimeOffset',objYear, objMonth, objDay, objHour, objMin, objSec, objTimeSpan)

;OR it looks like you could construct a DateTimeOffset with just a DateTime, but still having trouble
ObjDT = ObjectClrNew( 'System.DateTime',objYear, objMonth, objDay, objHour, objMin, objSec)
ObjDTOff = ObjectClrNew( 'System.DateTimeOffset',ObjDT)

; still doesnt work


; the end game is to get this to work
;https://github.com/emertechie/SyslogNet
SysLogServer = %A server capable of recieving Syslog over TCP on 514%
SysLogPort = 514
LocalHostName = %FQDN of This Computer%
AppName = %string of some arbitrary appname i made up%

UserFacility1 = ObjectClrNew("SyslogNet.Client.Facility",ObjectClrNew( 'System.Int32',16))
SeverityNotify = ObjectClrNew("SyslogNet.Client.Severity",ObjectClrNew( 'System.Int32',5))
SyslogSerializer = ObjectClrNew("SyslogNet.Client.Serialization.SyslogRfc3164MessageSerializer")
SysLogSender = ObjectClrNew("SyslogNet.Client.Transport.SyslogTcpSender",SyslogServer,SyslogPort)
SyslogMessage = ObjectClrNew("SyslogNet.Client.SyslogMessage",objDTOff,UserFacility1,SeverityNotify,LocalHostName,Appname,"TEST MESSAGE")




....IFICantBYTE

See:
https://msdn.microsoft.com/en-us/library/bb354978(v=vs.110).aspx
and
https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

I think you are almost there with the first bit of your code .. don't use a NULL for the timespan though...
I am no expert, but it looks like you need to construct your timespan object a bit like you have done with the main part of the datetime object.
I think it can be constructed a few different ways... using an Int64 with the number of ticks, or various Int32s containing the hours minutes secs etc.
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

td

"System.TimeSpan" is a structure.  Because of omissions made by MSFT in their CLR hosting interfaces, it cannot be constructed directly with WinBatch CLR hosting.  You can, however, write a C# cover class for the structure, compile the class inside your script and then instantiate the structure and access its methods.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

A quick and dirty wrapper example.

Code (winbatch) Select
objectClrOption("use","System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; C# wrapper class implementation
cSharpSource = ``
cSharpSource = cSharpSource :`using System;`:@lf
cSharpSource = cSharpSource :`namespace WinBatch {`:@lf
cSharpSource = cSharpSource :`  public class TimeSpanWrap {`
cSharpSource = cSharpSource :`    protected TimeSpan span;`:@lf
cSharpSource = cSharpSource :`    public void Create(int days, int hours, int minutes, int seconds, int millisec) {`:@lf     
cSharpSource = cSharpSource :`      span = new TimeSpan(days, hours, minutes, seconds, millisec);`:@lf
cSharpSource = cSharpSource :`      return;`:@lf
cSharpSource = cSharpSource :`    }`:@lf
cSharpSource = cSharpSource :`    public double TotalMilliseconds {`:@lf
cSharpSource = cSharpSource :`      get { return span.TotalMilliseconds; }`:@lf
cSharpSource = cSharpSource :`    }`:@lf
cSharpSource = cSharpSource :`  }`:@lf
cSharpSource = cSharpSource :`}`:@lf


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Compile the c# source in Memory
objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE

objResult = objCSharp.CompileAssemblyFromSource(objParams,cSharpSource)

; Check for errors
If objResult.Output.Count > 0
   strOutput = ''
   For x = 0 to objResult.Output.Count-1
      if strOutput == "" then strOutput = objResult.Output.Item(x)
      else strOutput = strOutput:@lf:objResult.Output.Item(x)
   Next
   Pause('Compiler Output',strOutput)
Endif

;; Test
objTsWrap = ObjectClrNew('WinBatch.TimeSpanWrap')
objTsWrap.Create( 0, 1, 1, 1, 0)
Message("Test TimeSpanWrap", objTsWrap.TotalMilliseconds)


Note that MSFT published most of the core framework source on line so it wouldn't be to difficult to compile your own WinBatch compatible class that provided all the functionality of the TimeSpan structure.  Here is a link to the TimeSpan source:

http://referencesource.microsoft.com/#mscorlib/system/timespan.cs
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade