Axaptapedia is now maintained by AgileCadence For more information please click here.
Current Time
Contents
- 1 Introduction
- 2 Dynamics AX versions
- 3 Getting the current utcDateTime
- 3.1 timeNow Function
- 3.2 DateTimeUtil::time Method
- 3.3 today Function
- 3.4 systemDateGet Function
- 3.5 DateTimeUtil::getSystemDateTime Method
- 3.6 COMVariant::createFromTime Method
- 3.7 System.DateTime::get_Now() Method
- 3.8 System.DateTime::get_UtcNow Method
- 3.9 %date% %time% (Shell Scripting via WinAPI)
- 3.10 Win32_LocalTime (WMI via COM)
- 3.11 Net Time /Domain (Shell Scripting via System.Diagnostics.Process)
- 3.12 GETDATE (Transact-SQL via System.Data.SqlClient.SqlCommand)
- 3.13 NIST Internet Time Service (via System.Net.Sockets.TcpClient)
Introduction[edit]
This document lists different ways of getting the current date and/or time on the local machine, AOS server or else where, and then converting it to a utcdatetime data type.
Dynamics AX versions[edit]
Tested on Dynamics AX 2009 SP1 RU-2, RU-6
Getting the current utcDateTime[edit]
timeNow Function[edit]
Client[edit]
Retrieves the current UTC time from the local machine<xpp>public static client utcDateTime timeNowClient() {;
return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 02.01.1900 16:45:02
Server[edit]
Retrieves the current UTC time from the AOS server<xpp>public static server utcDateTime timeNowServer() {;
return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 02.01.1900 16:46:58
DateTimeUtil::time Method[edit]
Client[edit]
Retrieves the current time from the local machine, converts it to an int and then returns it as a utcdatetime data type.<xpp>public static client utcDateTime timeClient() {;
return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::getSystemDateTime()));
}</xpp>Example: 02.01.1900 16:45:02
Server[edit]
Retrieves the current time from the AOS, rather than the local machine, converts it to an int and then returns it as a utcdatetime data type.<xpp>public static client utcDateTime timeClientServer() {;
return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::utcNow()));
}</xpp>Example: 02.01.1900 16:46:58
Retrieves the current time from the AOS, converts it to an int and then returns it as a utcdatetime data type.<xpp>public static server utcDateTime timeServer()
{;
return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::parse("1900-01-02T"+time2StrHMS(timeNow()))), DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 02.01.1900 16:46:58
today Function[edit]
Client[edit]
Retrieves the current date on the local machine.<xpp>public static client utcDateTime todayClient() {;
return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 24.11.2010 00:00:00
Server[edit]
Retrieves the current date on the AOS.<xpp>public static server utcDateTime todayServer() {;
return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 24.11.2010 00:00:00
systemDateGet Function[edit]
Client[edit]
Retrieves the session date if it has been set, otherwise the current date of the local machine, and then converts it to a utcdatetime data type.<xpp>public static client utcDateTime systemDateGetClient() {;
return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 24.11.2010 00:00:00
Server[edit]
Retrieves the session date if it has been set, otherwise the current date of the AOS, and then converts it to a utcdatetime data type.<xpp>public static server utcDateTime systemDateGetServer() {;
return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone());
}</xpp>Example: 24.11.2010 00:00:00
DateTimeUtil::getSystemDateTime Method[edit]
Client[edit]
Gets the current UTC date and time on the local machine<xpp>public static client utcDateTime getSystemDateTimeClient() {;
return DateTimeUtil::getSystemDateTime();
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
Gets the current UTC date and time on the AOS<xpp>public static server utcDateTime getSystemDateTimeServer() {;
return DateTimeUtil::getSystemDateTime();
}</xpp>Example: 24.11.2010 16:46:58
COMVariant::createFromTime Method[edit]
Client[edit]
Creates a new COMVariant object, initializes it with the current time on the local machine and returns a utcdatetime data type.<xpp>public static client utcDateTime createFromTimeClient() {
#XppTexts #define.Format("\%1 \%2") #define.Date(1) #define.Time(2) COMVariant cOMVariant = COMVariant::createFromTime(timeNow()); container dateParts = str2Con(cOMVariant.ToString(), #Space); System.Globalization.CultureInfo cultureInfo; System.DateTime dateTime; ;
cultureInfo = System.Globalization.CultureInfo::get_CurrentCulture();
dateTime = System.DateTime::Parse( strFmt(#Format, conPeek(dateParts, #Date), conPeek(dateParts, #Time)), cultureInfo, System.Globalization.DateTimeStyles::NoCurrentDateDefault);
return dateTime.ToUniversalTime();
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
Creates a new COMVariant object, initializes it with the current time on the AOS and returns a utcdatetime data type.<xpp>public static server utcDateTime createFromTimeServer() {
#XppTexts #define.Format("\%1 \%2") #define.Date(1) #define.Time(2) COMVariant cOMVariant = COMVariant::createFromTime(timeNow()); container dateParts = str2Con(cOMVariant.ToString(), #Space); System.Globalization.CultureInfo cultureInfo; System.DateTime dateTime; ;
new InteropPermission(InteropKind::ClrInterop).assert();
cultureInfo = System.Globalization.CultureInfo::get_CurrentCulture();
dateTime = System.DateTime::Parse( strFmt(#Format, conPeek(dateParts, #Date), conPeek(dateParts, #Time)), cultureInfo, System.Globalization.DateTimeStyles::NoCurrentDateDefault);
return CLRInterop::getAnyTypeForObject(dateTime.ToUniversalTime());
}</xpp>Example: 24.11.2010 16:46:58
System.DateTime::get_Now() Method[edit]
Client[edit]
Gets a DateTime object that is set to the current date and time on the local machine, expressed as the local time, and returns it as a utcdatetime data type<xpp>public static client utcDateTime get_NowClient() {;
return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime());
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
Gets a DateTime object that is set to the current date and time on the AOS, expressed as the local time, and returns it as a utcdatetime data type<xpp>public static server utcDateTime get_NowServer() {;
new InteropPermission(InteropKind::ClrInterop).assert();
return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime());
}</xpp>Example: 24.11.2010 16:46:58
System.DateTime::get_UtcNow Method[edit]
Client[edit]
Gets a DateTime object that is set to the current date and time on the local machine, and returs it as a utcdatetime data type.<xpp>public static client utcDateTime get_UtcNowClient() {;
return System.DateTime::get_UtcNow();
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
Gets a DateTime object that is set to the current date and time on the AOS, and returs it as a utcdatetime data type.<xpp>public static server utcDateTime get_UtcNowServer() {;
new InteropPermission(InteropKind::ClrInterop).assert();
return CLRInterop::getAnyTypeForObject( System.DateTime::get_UtcNow());
}</xpp>Example: 24.11.2010 16:46:58
%date% %time% (Shell Scripting via WinAPI)[edit]
Client[edit]
<xpp>/// <summary> /// Returns the current date and time of the local machine as a utcdatetime data type. /// </summary> public static client utcDateTime echoDateTimeClient(
FilePath _file = )
{
#WinAPI #XppTexts #File #define.Application('cmd') #define.Parameters('/c "echo %date% %time%>\%1"') FilePath file = _file; System.DateTime dateTime; ;
// Gets the time from the local machine and writes it to a file, but only // when an existing file was not passed in. if (!file) { file = WinAPI::getTempFilename( WinApi::getTempPath(), #EmptyString);
WinAPI::shellExecute(#Application, strFmt(#Parameters, file), #EmptyString, #EmptyString, #SW_HIDE, true); }
// Initializes a DateTime variable with the contents of the given file. dateTime = System.DateTime::Parse( con2Str(new AsciiIo(file, #io_read).read(), #Space));
return dateTime.ToUniversalTime();
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
<xpp>/// <summary> /// Returns the current date and time of the local machine (not the AOS server) as a utcdatetime data type. /// </summary> // AOSRunMode::Server public static server utcDateTime echoDateTimeServer() {
#WinAPI #XppTexts #define.Application('cmd') #define.Parameters('/c "echo %date% %time%>\%1"') FilePath file; ;
file = WinAPI::getTempFilename( WinApi::getTempPath(), #EmptyString);
WinAPI::shellExecute(#Application, strFmt(#Parameters, file), #EmptyString, #EmptyString, #SW_HIDE, true);
// The current time was written to a file, but on the client, so we have to // switch to the client to access the file and extract the time from it. return TutorialDateTime::echoDateTimeClient(file);
}</xpp>Example: 24.11.2010 16:45:02
Win32_LocalTime (WMI via COM)[edit]
Client[edit]
<xpp>/// <summary> /// Gets the current point in time on the local machine as Win32_LocalTime and converts it to a utcdatetime data type. /// </summary> public static client utcDateTime Win32_LocalTimeClient() {
#define.Moniker('winmgmts:') #define.WMIClass('Win32_LocalTime') COM wMIService; COM localTime; COM time; System.DateTime dateTime; ;
wMIService = COM::getObjectEx(#Moniker); // BP Deviation Documented localTime = wMIService.InstancesOf(#WMIClass); time = new COMEnum2Object(localTime).getFirst(); // BP Deviation Documented dateTime = new System.DateTime( // BP Deviation Documented System.Int32::Parse(int2str(time.year())), // BP Deviation Documented System.Int32::Parse(int2str(time.month())), // BP Deviation Documented System.Int32::Parse(int2str(time.day())), // BP Deviation Documented System.Int32::Parse(int2str(time.hour())), // BP Deviation Documented System.Int32::Parse(int2str(time.minute())), // BP Deviation Documented System.Int32::Parse(int2str(time.second())));
return dateTime.ToUniversalTime();
}</xpp>Example: 24.11.2010 16:45:02
Server[edit]
<xpp>/// <summary> /// Gets the current point in time on the AOS as Win32_LocalTime and converts it to a utcdatetime data type. /// </summary> public static server utcDateTime Win32_LocalTimeServer() {
#define.Moniker('winmgmts:') #define.WMIClass('Win32_LocalTime') COM wMIService; COM localTime; COM time; System.DateTime dateTime; Set permissions = new Set(Types::Class); ;
permissions.add(new InteropPermission(InteropKind::ComInterop)); permissions.add(new InteropPermission(InteropKind::ClrInterop)); CodeAccessPermission::assertMultiple(permissions);
wMIService = COM::getObjectEx(#Moniker); // BP Deviation Documented localTime = wMIService.InstancesOf(#WMIClass); time = new COMEnum2Object(localTime).getFirst(); // BP Deviation Documented dateTime = new System.DateTime( // BP Deviation Documented System.Int32::Parse(int2str(time.year())), // BP Deviation Documented System.Int32::Parse(int2str(time.month())), // BP Deviation Documented System.Int32::Parse(int2str(time.day())), // BP Deviation Documented System.Int32::Parse(int2str(time.hour())), // BP Deviation Documented System.Int32::Parse(int2str(time.minute())), // BP Deviation Documented System.Int32::Parse(int2str(time.second())));
return CLRSystemDateTime2UtcDateTime(dateTime.ToUniversalTime());
}</xpp>Example: 24.11.2010 16:46:58
Net Time /Domain (Shell Scripting via System.Diagnostics.Process)[edit]
Client[edit]
<xpp>/// <summary> /// Gets a string with the current time on the domain server and parses it to return it as a utcdatetime data type. /// </summary> public static client utcDateTime netTimeClient() {
#XppTexts #ERX #SRSModel #define.Program('cmd') #define.Parameters('/c "net time /domain:\%1"') #define.Region('en-us') // Needed for XP, which seems to return the date in the US regional format. This seems to have been fixed with Windows 7. #define.MDY(213) #define.OutputDate(2) #define.OutputTime(1) System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo; System.IO.StreamReader standardOutput; System.DateTime dateTime; UserInfo userInfo; int startPosition; str output; container split; ;
// Gets the domain name select firstonly UserInfo where UserInfo.Id == curUserId();
// Defines the process to be executed. startInfo = process.get_StartInfo(); startInfo.set_UseShellExecute(false); startInfo.set_RedirectStandardOutput(true); startInfo.set_WorkingDirectory(#defaultDrive); // The process will fail if the working directory is a network share. startInfo.set_FileName(#Program); startInfo.set_Arguments(strFmt(#Parameters, userInfo.networkDomain));
// Executes the process process.set_StartInfo(startInfo); process.Start(); process.WaitForExit();
// Gets the output from the process and releases the objects. standardOutput = process.get_StandardOutput(); process.Dispose(); output = standardOutput.ReadLine(); // Output: Aktuelle Zeit auf \\MyDomain ist 11/30/2010 4:12 PM. standardOutput.Dispose();
// Gets the date starting point. startPosition = strfind(output, #Space, strfind(output, #SRSPathSeparator, 1, strLen(output)), -strfind(output, #SRSPathSeparator, 1, strLen(output))) + 1;
// Trims the text to the date and time output = substr(output, startPosition, strLen(output) - startPosition); // Converts the string to a utcdatetime data type. dateTime = System.DateTime::Parse(output, System.Globalization.CultureInfo::GetCultureInfoByIetfLanguageTag(#Region), System.Globalization.DateTimeStyles::NoCurrentDateDefault); return dateTime.ToUniversalTime();
}</xpp>Example: 24.11.2010 16:47:00
GETDATE (Transact-SQL via System.Data.SqlClient.SqlCommand)[edit]
Server[edit]
<xpp>/// <summary> /// Returns the current database system timestamp as a utcdatetime data type without the database time zone offset. /// This value is derived from the operating system of the computer on which the instance of SQL Server is running. /// </summary> /// <remarks> /// Could also use CURRENT_TIMESTAMP or {fn Now()}. /// </remarks> public static server utcDateTime getDateSQLServer() {
#localmacro.Statement 'SELECT GETDATE()' #endmacro SysSQLSystemInfo systemInfo = SysSQLSystemInfo::construct(); System.Data.SqlClient.SqlConnectionStringBuilder builder; System.Data.SqlClient.SqlConnection connect; System.Data.SqlClient.SqlCommand command; System.DateTime dateTime; ;
new InteropPermission(InteropKind::ClrInterop).assert();
builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.set_DataSource(systemInfo.getLoginServer()); builder.set_InitialCatalog(systemInfo.getloginDatabase()); builder.set_IntegratedSecurity(true);
command = new System.Data.SqlClient.SqlCommand(#Statement, new System.Data.SqlClient.SqlConnection(builder.ToString()));
connect = command.get_Connection(); connect.Open();
dateTime = command.ExecuteScalar();
command.Dispose(); connect.Dispose();
return CLRInterop::getAnyTypeForObject(dateTime.ToUniversalTime());
}</xpp>Example: 24.11.2010 16:47:00
NIST Internet Time Service (via System.Net.Sockets.TcpClient)[edit]
Client[edit]
<xpp>/// <summary> /// Returns a utcdattime data type set with a time server used by the NIST Internet Time Service (ITS). /// </summary> public static client utcDateTime remoteTimeServer() {
#XppTexts #define.Format('20\%1T\%2') #define.Date(2) #define.Time(3) #define.Port(13) #define.Server('time-b.nist.gov') System.Net.Sockets.TcpClient tcpClient; System.Net.Sockets.NetworkStream networkStream; System.Text.Encoding encoding; System.Byte[] bytes; int receiveBufferSize; container result; ;
tcpClient = new System.Net.Sockets.TcpClient(); tcpClient.Connect(#Server, #Port); networkStream = tcpClient.GetStream(); receiveBufferSize = tcpClient.get_ReceiveBufferSize(); bytes = new System.Byte[receiveBufferSize](); networkStream.Read(bytes, 0, receiveBufferSize); tcpClient.Close();
encoding = System.Text.Encoding::get_ASCII(); result = str2Con(encoding.GetString(bytes), #Space);
return DateTimeUtil::parse(strFmt(#Format, conPeek(result, #Date), conPeek(result, #Time)));
}</xpp>Example: 24.11.2010 16:45:05