世界協定時刻(UTC)形式をローカル時間表示に直します


VB Tips And Sample(HOME)(戻る)



Attribute VB_Name = "Module1"
Option Explicit

'typedef struct _FILETIME {
'    DWORD dwLowDateTime;   /* 下位 32 ビット */
'    DWORD dwHighDateTime;  /* 上位 32 ビット */
'} FILETIME,



Type FILETIME
dwLowDateTime As Long '下位32ビット値
dwHighDateTime As Long '上位32ビット値
End Type

'typedef struct _SYSTEMTIME {
'    WORD wYear;
'    WORD wMonth;
'    WORD wDayOfWeek;
'    WORD wDay;
'    WORD wHour;
'    WORD wMinute;
'    WORD wSecond;
'    WORD wMilliseconds;
'} SYSTEMTIME, *PSYSTEMTIME;


Type SystemTime
     wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type




Public Declare Sub GetSystemTimeAsFileTime Lib "kernel32" (SystemTime As FILETIME)

'FileTimeToSystemTime

Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SystemTime) As Long


Public Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SystemTime)


Public Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long

Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SystemTime, lpFileTime As FILETIME) As Long




VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   2400
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   2670
   LinkTopic       =   "Form1"
   ScaleHeight     =   2400
   ScaleWidth      =   2670
   StartUpPosition =   3  'Windows の既定値
   Begin VB.CommandButton Command2 
      Caption         =   "Command2"
      Height          =   675
      Left            =   300
      TabIndex        =   1
      Top             =   1440
      Width           =   2055
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   675
      Left            =   360
      TabIndex        =   0
      Top             =   360
      Width           =   1995
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit


Private Sub Command1_Click()

'取得した世界協定時刻(UTC)形式をローカル時間表示に直します

Dim ret
Dim fitime As FILETIME '取得した世界協定時刻(UTC)形式
Dim fiLocTime As FILETIME 'ローカルタイムに直したもの
Dim sitime As SystemTime
Dim strWeek As String

GetSystemTimeAsFileTime fitime  '取得世界協定時刻(UTC)形式


Debug.Print fitime.dwHighDateTime
Debug.Print fitime.dwLowDateTime

'世界協定時刻(UTC)に基づくファイル時刻を、ローカルのファイル時刻へ変換します。
ret = FileTimeToLocalFileTime(fitime, fiLocTime)
'64 ビット形式のファイル時刻を、システム日時形式へ変換します。
ret = FileTimeToSystemTime(fiLocTime, sitime)

Debug.Print sitime.wYear; "年"
Debug.Print sitime.wMonth; "月"
Debug.Print sitime.wDay; "日"
Debug.Print sitime.wHour; "時"
Debug.Print sitime.wMinute; "分"
Debug.Print sitime.wSecond; "秒"
Debug.Print sitime.wMilliseconds; "ミリ秒"

Select Case sitime.wDayOfWeek
   Case 0
      strWeek = "Sunday"
   Case 1
      strWeek = "Monday"
   Case 2
      strWeek = "Tuesday"
   Case 3
      strWeek = "Wednesday"
   Case 4
      strWeek = "Thursday"
   Case 5
      strWeek = "Friday"
   Case 6
      strWeek = "Saturday"
End Select
Debug.Print strWeek


''The day of the week.
''
''Sunday = 0
''Monday = 1
''Tuesday = 2
''Wednesday = 3
''Thursday = 4
''Friday = 5
''Saturday = 6








End Sub


Private Sub Command2_Click()
'世界協定時刻(UTC)に基づくファイル時刻をローカル時刻に変換します

Dim ret
Dim fitime As FILETIME
Dim fiLocTime As FILETIME 'ローカルタイムに直したもの
Dim sitime As SystemTime
Dim inY As Integer
Dim inM As Integer
Dim inD As Integer
Dim inH As Integer
Dim inMi As Integer
Dim inS As Integer
Dim inMil As Integer
Dim strWeek As String
 inY = 2005
 inM = 4
 inD = 15
 inH = 15
 inMi = 26
 inS = 19
 inMil = 203



sitime.wYear = inY
sitime.wMonth = inM
sitime.wDay = inD
sitime.wHour = inH
sitime.wMinute = inMi
sitime.wSecond = inS
sitime.wMilliseconds = inMil
sitime.wDayOfWeek = 2 '無視されるので適当

'ファイルタイムに変換
ret = SystemTimeToFileTime(sitime, fitime)
'ローカルタイムに変換
ret = FileTimeToLocalFileTime(fitime, fiLocTime)
'再びシステムタイムに変換代入
ret = FileTimeToSystemTime(fiLocTime, sitime)
Debug.Print sitime.wYear; "年"
Debug.Print sitime.wMonth; "月"
Debug.Print sitime.wDay; "日"
Debug.Print sitime.wHour; "時"
Debug.Print sitime.wMinute; "分"
Debug.Print sitime.wSecond; "秒"
Debug.Print sitime.wMilliseconds; "ミリ秒"

Select Case sitime.wDayOfWeek
   Case 0
      strWeek = "Sunday"
   Case 1
      strWeek = "Monday"
   Case 2
      strWeek = "Tuesday"
   Case 3
      strWeek = "Wednesday"
   Case 4
      strWeek = "Thursday"
   Case 5
      strWeek = "Friday"
   Case 6
      strWeek = "Saturday"
End Select
Debug.Print strWeek
End Sub




©  2004 I Love Balard. All Rights Reserved.

VB Tips And Sample(HOME)(戻る)