25.11.2010, 04:12 | #1 |
Участник
|
axaptapedia: Current Time
Источник: http://www.axaptapedia.com/Current_Time
============== Summary: Better code examples and improved documentation =Introduction= 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= Tested on Dynamics AX 2009 SP1 RU-2 ==timeNow Function== ===Client=== Retrieves the current UTC time from the local machinepublic static client utcDateTime timeNowClient() {; return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:45:02'' ===Server=== Retrieves the current UTC time from the AOS serverpublic static server utcDateTime timeNowServer() {; return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==DateTimeUtil::time Method== ===Client=== Retrieves the current time from the local machine, converts it to an int and then returns it as a utcdatetime data type.public static client utcDateTime timeClient() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::getSystemDateTime())); }''Example: 02.01.1900 16:45:02'' ===Server=== 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.public static client utcDateTime timeClientServer() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::utcNow())); }''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.public static server utcDateTime timeServer() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::parse("1900-01-02T"+time2StrHMS(timeNow()))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==today Function== ===Client=== Retrieves the current date on the local machine.public static client utcDateTime todayClient() {; return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ===Server=== Retrieves the current date on the AOS.public static server utcDateTime todayServer() {; return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ==systemDateGet Function== ===Client=== 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.public static client utcDateTime systemDateGetClient() {; return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ===Server=== 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.public static server utcDateTime systemDateGetServer() {; return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ==DateTimeUtil::getSystemDateTime Method== ===Client=== Gets the current UTC date and time on the local machinepublic static client utcDateTime getSystemDateTimeClient() {; return DateTimeUtil::getSystemDateTime(); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets the current UTC date and time on the AOSpublic static server utcDateTime getSystemDateTimeServer() {; return DateTimeUtil::getSystemDateTime(); }''Example: 02.01.1900 16:46:58'' ==COMVariant::createFromTime Method== ===Client=== Creates a new COMVariant object, initializes it with the current time on the local machine and returns a utcdatetime data type.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(); }''Example: 02.01.1900 16:45:02'' ===Server=== Creates a new COMVariant object, initializes it with the current time on the AOS and returns a utcdatetime data type.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()); }''Example: 02.01.1900 16:46:58'' ==System.DateTime::get_Now() Method== ===Client=== 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 typepublic static client utcDateTime get_NowClient() {; return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime()); }''Example: 02.01.1900 16:45:02'' ===Server=== 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 typepublic static server utcDateTime get_NowServer() {; new InteropPermission(InteropKind::ClrInterop).assert(); return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime()); }''Example: 02.01.1900 16:46:58'' ==System.DateTime::get_UtcNow Method== ===Client=== 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.public static client utcDateTime get_UtcNowClient() {; return System.DateTime::get_UtcNow(); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets a DateTime object that is set to the current date and time on the AOS, and returs it as a utcdatetime data type.public static server utcDateTime get_UtcNowServer() {; new InteropPermission(InteropKind::ClrInterop).assert(); return CLRInterop::getAnyTypeForObject( System.DateTime::get_UtcNow()); }''Example: 02.01.1900 16:46:58'' ==Win32_LocalTimeClient== ===Client=== Gets the current point in time on the local machine as Win32_LocalTime and converts it to a utcdatetime data type.public static client utcDateTime Win32_LocalTimeClient() { COM WMIService; COM localTime; COM time; ; WMIService = COM::getObjectEx(@'winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2'); localTime = WMIService.ExecQuery('Select * from Win32_LocalTime'); time = new COMEnum2Object(localTime).getFirst(); return DateTimeUtil::newDateTime(mkdate(time.day(), time.month(), time.year()), str2Time(strFmt("%1:%2:%3", strRFix(time.hour(), 2, "0"), strRFix(time.minute(), 2, "0"), strRFix(time.second(), 2, "0"))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets the current point in time on the AOS as Win32_LocalTime and converts it to a utcdatetime data type.public static server utcDateTime Win32_LocalTimeServer() { COM WMIService; COM localTime; COM time; ; new InteropPermission(InteropKind::ComInterop).assert(); WMIService = COM::getObjectEx(@'winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2'); localTime = WMIService.ExecQuery('Select * from Win32_LocalTime'); time = new COMEnum2Object(localTime).getFirst(); return DateTimeUtil::newDateTime(mkdate(time.day(), time.month(), time.year()), str2Time(strFmt("%1:%2:%3", strRFix(time.hour(), 2, "0"), strRFix(time.minute(), 2, "0"), strRFix(time.second(), 2, "0"))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==GETDATE (Transact-SQL)== ===Server=== 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.public static server utcDateTime getDateSQLServer() { 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( 'SELECT GETDATE()', 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()); }''Example: 02.01.1900 16:47:00'' ==NIST Internet Time Service== ===Client=== Returns a utcdattime data type set with a time server used by the NIST Internet Time Service (ITS). public static client utcDateTime remoteTimeServer() { #XppTexts #define.Format('20\%1T\%2') #define.Date(2) #define.Time(3) #define.Port(13) 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('time-b.nist.gov', #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))); }''Example: 02.01.1900 16:45:05'' Источник: http://www.axaptapedia.com/Current_Time
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
29.11.2010, 22:11 | #2 |
Участник
|
axaptapedia: Current Time
Источник: http://www.axaptapedia.com/Current_Time
============== Summary: Tested on RU-6 =Introduction= 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= Tested on Dynamics AX 2009 SP1 RU-2, RU-6 ==timeNow Function== ===Client=== Retrieves the current UTC time from the local machinepublic static client utcDateTime timeNowClient() {; return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:45:02'' ===Server=== Retrieves the current UTC time from the AOS serverpublic static server utcDateTime timeNowServer() {; return DateTimeUtil::newDateTime( 2\\1\\1900, timeNow(), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==DateTimeUtil::time Method== ===Client=== Retrieves the current time from the local machine, converts it to an int and then returns it as a utcdatetime data type.public static client utcDateTime timeClient() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::getSystemDateTime())); }''Example: 02.01.1900 16:45:02'' ===Server=== 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.public static client utcDateTime timeClientServer() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::utcNow())); }''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.public static server utcDateTime timeServer() {; return DateTimeUtil::newDateTime( 02\\01\\1900, DateTimeUtil::time(DateTimeUtil::parse("1900-01-02T"+time2StrHMS(timeNow()))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==today Function== ===Client=== Retrieves the current date on the local machine.public static client utcDateTime todayClient() {; return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ===Server=== Retrieves the current date on the AOS.public static server utcDateTime todayServer() {; return DateTimeUtil::newDateTime( today(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ==systemDateGet Function== ===Client=== 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.public static client utcDateTime systemDateGetClient() {; return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ===Server=== 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.public static server utcDateTime systemDateGetServer() {; return DateTimeUtil::newDateTime( systemDateGet(), 0, DateTimeUtil::getClientMachineTimeZone()); }''Example: 24.11.2010 00:00:00'' ==DateTimeUtil::getSystemDateTime Method== ===Client=== Gets the current UTC date and time on the local machinepublic static client utcDateTime getSystemDateTimeClient() {; return DateTimeUtil::getSystemDateTime(); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets the current UTC date and time on the AOSpublic static server utcDateTime getSystemDateTimeServer() {; return DateTimeUtil::getSystemDateTime(); }''Example: 02.01.1900 16:46:58'' ==COMVariant::createFromTime Method== ===Client=== Creates a new COMVariant object, initializes it with the current time on the local machine and returns a utcdatetime data type.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(); }''Example: 02.01.1900 16:45:02'' ===Server=== Creates a new COMVariant object, initializes it with the current time on the AOS and returns a utcdatetime data type.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()); }''Example: 02.01.1900 16:46:58'' ==System.DateTime::get_Now() Method== ===Client=== 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 typepublic static client utcDateTime get_NowClient() {; return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime()); }''Example: 02.01.1900 16:45:02'' ===Server=== 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 typepublic static server utcDateTime get_NowServer() {; new InteropPermission(InteropKind::ClrInterop).assert(); return CLRInterop::getAnyTypeForObject( System.DateTime::get_Now().ToUniversalTime()); }''Example: 02.01.1900 16:46:58'' ==System.DateTime::get_UtcNow Method== ===Client=== 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.public static client utcDateTime get_UtcNowClient() {; return System.DateTime::get_UtcNow(); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets a DateTime object that is set to the current date and time on the AOS, and returs it as a utcdatetime data type.public static server utcDateTime get_UtcNowServer() {; new InteropPermission(InteropKind::ClrInterop).assert(); return CLRInterop::getAnyTypeForObject( System.DateTime::get_UtcNow()); }''Example: 02.01.1900 16:46:58'' ==Win32_LocalTimeClient== ===Client=== Gets the current point in time on the local machine as Win32_LocalTime and converts it to a utcdatetime data type.public static client utcDateTime Win32_LocalTimeClient() { COM WMIService; COM localTime; COM time; ; WMIService = COM::getObjectEx(@'winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2'); localTime = WMIService.ExecQuery('Select * from Win32_LocalTime'); time = new COMEnum2Object(localTime).getFirst(); return DateTimeUtil::newDateTime(mkdate(time.day(), time.month(), time.year()), str2Time(strFmt("%1:%2:%3", strRFix(time.hour(), 2, "0"), strRFix(time.minute(), 2, "0"), strRFix(time.second(), 2, "0"))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:45:02'' ===Server=== Gets the current point in time on the AOS as Win32_LocalTime and converts it to a utcdatetime data type.public static server utcDateTime Win32_LocalTimeServer() { COM WMIService; COM localTime; COM time; ; new InteropPermission(InteropKind::ComInterop).assert(); WMIService = COM::getObjectEx(@'winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2'); localTime = WMIService.ExecQuery('Select * from Win32_LocalTime'); time = new COMEnum2Object(localTime).getFirst(); return DateTimeUtil::newDateTime(mkdate(time.day(), time.month(), time.year()), str2Time(strFmt("%1:%2:%3", strRFix(time.hour(), 2, "0"), strRFix(time.minute(), 2, "0"), strRFix(time.second(), 2, "0"))), DateTimeUtil::getClientMachineTimeZone()); }''Example: 02.01.1900 16:46:58'' ==GETDATE (Transact-SQL)== ===Server=== 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.public static server utcDateTime getDateSQLServer() { 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( 'SELECT GETDATE()', 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()); }''Example: 02.01.1900 16:47:00'' ==NIST Internet Time Service== ===Client=== Returns a utcdattime data type set with a time server used by the NIST Internet Time Service (ITS). public static client utcDateTime remoteTimeServer() { #XppTexts #define.Format('20\%1T\%2') #define.Date(2) #define.Time(3) #define.Port(13) 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('time-b.nist.gov', #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))); }''Example: 02.01.1900 16:45:05'' Источник: http://www.axaptapedia.com/Current_Time
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|