您的位置:首页 > 编程语言 > C#

用C#修改WM5.0和WM6.0 系统平台的时间

2010-11-25 22:40 295 查看
用C#在windowsMobile5.0/6.0平台上进行修改系统时间。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

using DigitalCity.Terminal.Loglib;

namespace ModifyTime

{
/// <summary>

///  实现修改本地时间的功能
/// </summary>
public class SyncSystemTime
{
/// <summary>
/// 根据传入的时间信息,修改本地时间
/// </summary>
/// <param name="serviceTime">传入的时间信息</param>
public void SetLocalSystemTime(string serviceTime)
{
try
{
// 系统时间结构
SYSTEMTIME systime = new SYSTEMTIME();
// 截取年信息
systime.wYear = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(0 , 4)) );
// 截取月信息
systime.wMonth = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(4 , 2)) );
// 截取日信息
systime.wDay = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(6 , 2)) );
// 截取小时信息
systime.wHour = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(8 , 2)) );
if ( systime.wHour < 8 )
{
systime.wDay = ( ushort ) ( systime.wDay - 1 );
if ( systime.wDay <= 0 )
{
systime.wMonth = ( ushort ) ( systime.wMonth - 1 );
if ( systime.wMonth <= 0 )
{
systime.wMonth = 12;

systime.wYear = ( ushort ) ( systime.wYear - 1 );

}

switch ( systime.wMonth )
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
systime.wDay = 31;
break;
case 2:
if ( ( systime.wYear / 4 == 0 ) ||
( ( systime.wYear / 100 == 0 ) && ( systime.wYear / 400 == 0 ) ) )
{
systime.wDay = 29;
}
else
{

systime.wDay = 28;
}
break;
default:
systime.wDay = 30;
break;
}

}
}
// 将时间转为东八区时间
systime.wHour = ( ushort ) ( ( systime.wHour + 24 - 8 ) % 24 );
// 截取分钟信息
systime.wMinute = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(10 , 2)) );
// 截取秒信息
systime.wSecond = ( ushort ) ( Convert.ToUInt32(serviceTime.Substring(12 , 2)) );
// 调用系统方法,设置系统实际
SetSystemTime(ref systime);
}
catch ( Exception )
{
Log.log.WriteIntoFile("传入的服务器时间不正确,引发异常");
}
}

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);

/// <summary>
/// 系统时间结构
/// </summary>
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;
}
}
// end of class
}
// end of namespace
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: