Jalena Blog

  • 首页
  • 程序开发
    • Java
    • Spring
    • SpringBoot
    • SpringMvc
    • Python
    • Odoo
    • C#
    • SQL
    • Web
    • Linux
  • 移动设备
    • Android
    • Router
  • 杂七杂八
    • 我的废话
    • 音频
    • 视频
    • 常用软件
  • 关于
  1. 首页
  2. 软件开发
  3. C#
  4. 正文

WinCE 同步时间

2018-09-11

最近项目中PDA的时间总是与业务数据时间不符,因此需要一个这样的功能。

首先确定使用WebService的Response信息作为时间的获取源头。

#region 修改系统日期
[DllImport("coredll.dll")]
private static extern bool SetLocalTime(ref SystemTime lpSystemTime);

public struct SystemTime
{
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
}

/// <summary>
/// 设置系统当前日期
/// </summary>
public static void SetTime()
{

    WebRequest request = null;
    WebResponse response = null;
    WebHeaderCollection headerCollection = null;

    string webserviceUrl = string.Format("http://{0}/axis/servlet/AxisServlet", DB.GetServerAddr());
    string datetime = string.Empty;
    CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
    // Tue, 11 Sep 2018 09:46:58 GMT
    string format = "r";

    try
    {
        request = WebRequest.Create(webserviceUrl);
        request.Timeout = -1;
        request.Credentials = CredentialCache.DefaultCredentials;
        response = request.GetResponse();
        headerCollection = response.Headers;
        foreach (var item in headerCollection.AllKeys)
        {
            if (item.Equals("Date",StringComparison.CurrentCultureIgnoreCase))
            {
                datetime = headerCollection[item];
            }
        }

        SystemTime newTime = new SystemTime();
        DateTime dt = DateTime.ParseExact(datetime, format, cultureInfo).AddHours(8.00);

        newTime.wYear = Convert.ToInt16(dt.Year);
        newTime.wMonth = Convert.ToInt16(dt.Month);
        newTime.wDay = Convert.ToInt16(dt.Day);
        newTime.wDayOfWeek = Convert.ToInt16(dt.DayOfWeek);
        newTime.wHour = Convert.ToInt16(dt.Hour);
        newTime.wMinute = Convert.ToInt16(dt.Minute);
        newTime.wSecond = Convert.ToInt16(dt.Second);

        SetLocalTime(ref newTime);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        if (request != null)
        {
            request.Abort();
        }
        if (response != null)
        {
            response.Close();
        }
        if (headerCollection != null)
        {
            headerCollection.Clear();
        }
    }
}
#endregion
标签: c# WinCE
最后更新:2019-01-01

Jalena

原创内容,转载请注明出处! 部分内容来自网络,请遵守法律适用!

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

【腾讯云】云服务器特惠热卖中
标签聚合
java Centos odoo 其他 生活 wordpress docker MySql
分类
  • Android / 4篇
  • C# / 10篇
  • Codec / 9篇
  • Conversion / 14篇
  • Editing / 16篇
  • Java / 47篇
  • Javascript / 3篇
  • Linux / 30篇
  • Odoo / 25篇
  • Python / 6篇
  • SQL / 30篇
  • Web / 3篇
  • Windows / 4篇
  • 我的日记 / 10篇
  • 杂七杂八 / 93篇
  • 软件开发 / 72篇
  • 非线性编辑 / 1篇

COPYRIGHT © 2026 Jalena Blog. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备17025376号