Get TimeZone offset?

mrQQmrQQ Member Posts: 239
edited 2013-09-21 in NAV Three Tier
Hello,

how can I get TZ offset in hours using dotnet libraries? No matter what I try, it always comes up as empty for me :(

Comments

  • mrQQmrQQ Member Posts: 239
    so this works fine on C#:
                DateTimeOffset dto = new DateTimeOffset(DateTime.Now);
    
                MessageBox.Show(dto.Offset.Hours.ToString());
    

    but I cannot do this on NAV:
    dto := dto.DateTimeOffset(dt.Now);
    ts := dto.Offset;
    

    because on the line ts := dto.Offset I get
    ---------------------------
    Microsoft Dynamics NAV Development Environment
    ---------------------------
    Assignment is not allowed for this variable.
    ---------------------------
    OK   
    ---------------------------
    

    and if I try to do Message(FORMAT(dto.Offset.Hours)) I get
    ---------------------------
    Microsoft Dynamics NAV Development Environment
    ---------------------------
    The variable is not a record. 'Variable.Field' is invalid.
    ---------------------------
    OK   
    ---------------------------
    

    :/
  • mrQQmrQQ Member Posts: 239
    ok, appearently TimeSpan is mapped to Duration, so if I replace ts type to Duration, assignement works fine, however Duration still comes up empty, even though in VS it works fine..
  • mrQQmrQQ Member Posts: 239
    what's even worse, if I set DTO to RunOnClient=Yes, it crashes the RTC client :)
  • VjekoVjeko Member Posts: 55
    It's not possible using pure C/AL. NAV runtime intercepts any assignment of .NET variables, and immediately maps the type that directly maps to an NAV type. There is no way to prevent it, as long as you are using DotNet variables. You could write your own assembly that wraps the methods you need, and then you could call it.

    To illustrate the depth of the problem, I can provide this code:
    Assembly := Assembly.Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86');
    o := Assembly.CreateInstance('System.TimeSpan');
    
    arr := arr.CreateInstance(GETDOTNETTYPE(Type),1);
    arr.SetValue(GETDOTNETTYPE(''),0);
    m := Assembly.GetType.GetMethod('CreateInstance',arr);
    
    arr := arr.CreateInstance(GETDOTNETTYPE(''),1);
    arr.SetValue('System.TimeSpan',0);
    o := m.Invoke(Assembly,arr);
    MESSAGE('%1',o.GetType);
    

    Obviously, I never handled System.TimeSpan directly, I used the most complicated reflection I could think of to create an instance of System.TimeSpan, but NAV runtime has still intercepted the output of the m.Invoke, which has indeed returned System.TimeSpan, and has converted it into Microsoft.Dynamics.Nav.Runtime.NavDuration immediately as it was returned from the method call.

    So - unfortunately, nothing at the C/AL level that you can do. Sorry.
    (Co-)author of "Implementing Microsoft Dynamics NAV 2009"
    http://vjeko.com/
  • mrQQmrQQ Member Posts: 239
    clear..

    however, do you have any clue as to why the returned duration is empty, instead of containing TZ offset?
  • mrQQmrQQ Member Posts: 239
    hm.. could it be that dt.Now gets converted to NAV DateTime, before being passet to the constructor, and in process loses "Kind" property/information..?
  • VjekoVjeko Member Posts: 55
    Can't really tell what's wrong there. It may be a bug in the implementation.
    (Co-)author of "Implementing Microsoft Dynamics NAV 2009"
    http://vjeko.com/
  • mrQQmrQQ Member Posts: 239
    do you know where should I report this?
Sign In or Register to comment.