Cómo: Obtener o establecer la hora del sistema
Actualización: noviembre 2007
Para obtener o establecer la hora del sistema del dispositivo, utilice la invocación de plataforma para llamar a las funciones GetSystemTime o SetSystemTime nativas.
Tenga en cuenta que la función GetSystemTime devuelve el horario universal coordinado (UTC), también conocido como hora media de Greenwich. Para obtener su hora local, debe agregar o restar el número de horas entre su zona horaria y el UTC. Por ejemplo, 24:00 (medianoche) en UTC son las 19:00 en Nueva York, un desplazamiento de menos 5 horas (UTC-5).
Para determinar el desplazamiento del UTC para su zona horaria, vea la ficha Zona horaria de Propiedades de fecha y hora.
Algunos emuladores de dispositivos inicialmente no establecen correctamente el horario de verano, que podría afectar al resultado.
Ejemplo
Este ejemplo de código define lo siguiente:
Declaraciones de invocación de plataforma para los métodos nativos en Windows Embedded CE.
Una estructura para pasar información a los métodos nativos y recibirla.
Un método administrado denominado GetTime que muestra la hora actual.
Un método administrado denominado SetTime que adelanta una hora el reloj del sistema.
Public Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure
Declare Function GetSystemTime Lib "CoreDll.dll" _
(ByRef lpSystemTime As SYSTEMTIME) As UInt32
Declare Function SetSystemTime Lib "CoreDll.dll" _
(ByRef lpSystemTime As SYSTEMTIME) As UInt32
Public Sub GetTime
' Call the native GetSystemTime method
' with the defined structure.
Dim st As New SYSTEMTIME
GetSystemTime(st)
' Show the current time.
MessageBox.Show("Current Time: " & st.wHour.ToString() _
& ":" & st.wMinute.ToString())
End Sub
Public Sub SetTime
' Call the native GetSystemTime method
' with the defined structure.
Dim st As New SYSTEMTIME
GetSystemTime(st)
' Set the system clock ahead one hour.
st.wHour = Convert.ToUInt16(((CInt(st.wHour) + 1)) Mod 24)
SetSystemTime(st)
End Sub
[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private void GetTime()
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME stime = new SYSTEMTIME();
GetSystemTime(ref stime);
// Show the current time.
MessageBox.Show("Current Time: " +
stime.wHour.ToString() + ":"
+ stime.wMinute.ToString());
}
private void SetTime()
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime = new SYSTEMTIME();
GetSystemTime(ref systime);
// Set the system clock ahead one hour.
systime.wHour = (ushort)(systime.wHour + 1 % 24);
SetSystemTime(ref systime);
MessageBox.Show("New time: " + systime.wHour.ToString() + ":"
+ systime.wMinute.ToString());
}
Compilar el código
Para este ejemplo se requieren referencias a los siguientes espacios de nombres: