Friday, February 12, 2010

Date and Time in DAX

Just a quick example of the basic date and time functions.
static void time(Args _args)
{
    int                     t,h,m,s;
    Date                    d;
    InteropPermission       perm;
    System.DateTime         dttime;
;

    //method #1
    t = timenow();
    d = systemDateGet(); //preferred to today()
    h = trunc(t/3600);
    m = trunc((t - h*3600)/60);
    s = t - h*3600 - m * 60;
    info (strfmt("%1 %2:%3:%4",d,h,strRFix(int2str(m),2,"0"),s));

    //method #2
    perm = new InteropPermission(InteropKind::ClrInterop);
    perm.assert();
    dttime = CLRInterop::staticInvoke("System.DateTime","get_Now" );
    CodeAccessPermission::revertAssert();
    info (dttime.ToString());
}
 Notes:
  • the DateTime type has the full array of .NET methods (i.e. ToShortTimeString, ToUniversalTime, etc)
  • systemDateGet() is affected by systemDateSet, making it superior in testing scenarios
  • strRFix is used to pad zeros onto single-digit minutes (i.e. 10:4 -> 10:04)

No comments:

Post a Comment