Options

Get current UTC time in AL

idonavidonav Member Posts: 31
Hi all,

I would like to find a way to get the current UTC date in AL. I've been messing around with DateTime and similar, but it seems like it takes the user agent time zone in to account (which is out of our control), and it all just becomes a big mess with DateTimes not making any sense.

So, I need a way to get current UTC time, it is not an option to do a get request to external services, neither tampering with client computer time zones (spread around the world). All servers have their timezone set to UTC, so maybe I could fetch it from there in some way?

Suggestions appreciated, seems like a super simple task, but for some reason I'm not getting it to work.

Best Answer

Answers

  • Options
    KTA8KTA8 Member Posts: 389
    You can use a default .net object that get that system data (you don't have to develop it) and put that in run on client = false; that should work
  • Options
    idonavidonav Member Posts: 31
    DateTime.UtcNow from mscorlib would probably work if I developed a C/AL solution, but I need to do it in AL.
  • Options
    ftorneroftornero Member Posts: 522
    In BC on-premise you can interop whit dotNET from AL, I don't know if that is your case.

    This is the link:

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-get-started-call-dotnet-from-al
  • Options
    idonavidonav Member Posts: 31
    edited 2019-02-15
    Thank you @ftornero, that works perfectly fine. The problem described in the initial post only occurs when saving it to a datetime variable, but saving it as a string does the job.

    Full code below, for other (not related to this) reasons, I had to convert it to a BigInt:
        procedure GetCurrentUtcTime() UtcTime: BigInteger;
        var
            DateTime: DateTime;
            TempTime: Text;
        begin
            DateTime := CurrentDateTime();
            TempTime := Format(DateTime, 0, 9);
    
            TempTime := DelChr(TempTime, '=', '-:.TZ');
            Evaluate(UtcTime, TempTime);
        end;
    
    
Sign In or Register to comment.