2017-08-05 7 views
0

私は時間を変更しようとしていますが、時刻を00:00に変更しようとすると、代わりに08:00になりますか? UTC + 8のタイムゾーンを考慮していますか?C#でDateTimePickerを使用してシステムの日付と時刻を変更していますか?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
namespace LibraryAdmin 
{ 
    public partial class Form41 : Form 
    { 
     public Form41() 
     { 
      InitializeComponent(); 
     } 

     public struct SystemTime 
     { 
      public ushort Year; 
      public ushort Month; 
      public ushort DayOfWeek; 
      public ushort Day; 
      public ushort Hour; 
      public ushort Minute; 
      public ushort Second; 
      public ushort Millisecond; 
     }; 

     [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)] 
     public extern static void Win32GetSystemTime(ref SystemTime sysTime); 

     [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)] 
     public extern static bool Win32SetSystemTime(ref SystemTime sysTime); 

     private void Form41_Load(object sender, EventArgs e) 
     {  
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      SystemTime updatedTime = new SystemTime(); 
      updatedTime.Year = (ushort)dateTimePicker1.Value.Year; 
      updatedTime.Month = (ushort)dateTimePicker1.Value.Month; 
      updatedTime.Day = (ushort)dateTimePicker1.Value.Day; 

      updatedTime.Hour = (ushort)((dateTimePicker2.Value.Hour)) ; 
      updatedTime.Minute = (ushort)dateTimePicker2.Value.Minute; 
      updatedTime.Second = (ushort)dateTimePicker2.Value.Second; 
      Win32SetSystemTime(ref updatedTime); 
     } 

     private void dateTimePicker2_ValueChanged(object sender, EventArgs e) 
     { 
      this.dateTimePicker2.Value.ToFileTimeUtc(); 
     } 
    } 
} 

答えて

2

はい、それがあるため、タイムゾーンの問題の正確です。 docs for SetSystemTime

現在のシステム時刻と日付を設定します。システム時間は協定世界時(UTC)で表されます。

あなたが特定の現地時間にそれを変更しようとしているのであれば、あなたは最初にUTCにそれを変換する必要があります。例:

private void button1_Click(object sender, EventArgs e) 
{ 
    var local = dateTimePicker1.Value; 
    var utc = local.ToUniversalTime(); 
    SystemTime updatedTime = new SystemTime 
    { 
     Year = (ushort) utc.Year, 
     Month = (ushort) utc.Month, 
     Day = (ushort) utc.Day, 
     Hour = (ushort) utc.Hour, 
     Minute = (ushort) utc.Minute, 
     Second = (ushort) utc.Second, 
    }; 
    Win32SetSystemTime(ref updatedTime); 
} 
+0

偽のアラーム!できます!ありがとうございました!!!! –

関連する問題