اذهب الي المحتوي
أوفيسنا

الردود الموصى بها

تحويل الوقت والتاريخ المحلى الي التوقيت عن التوقيت العالمي الموحد (UTC) 
عرض تاريخ و اوقات دول او مدن مختلفة في نفس الوقت بناء على فرق الوقت بينعم ولين التوقيت العالمي الموحد


جدول  tblTimeZones والذى يتكون من الحقول 
ShowInForm            : اختيار البلدان للعرض في النموذج
CountryName          : اسماء المدن و البلدان
TimeDifference        : فرق التوقيت عن التوقيت العالمي الموحد (UTC)
الفارق الزمني (بالساعات، مع إشارة "+" أو "-")
DaylightSavingTime  : التوقيت الصيفي


اولا اكواد الوحدة النمطية

Option Compare Database
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetSystemTimeAPI Lib "kernel32" Alias "GetSystemTime" (lpSystemTime As SYSTEMTIME) As Long
#Else
    Private Declare Function GetSystemTimeAPI Lib "kernel32" Alias "GetSystemTime" (lpSystemTime As SYSTEMTIME) As Long
#End If

Private Type SYSTEMTIME
    ' Structure for 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 Function GetUTC() As Date
    ' Function to get the current UTC time
    Dim utctime As Date
    Dim sysTime As SYSTEMTIME
    Call GetSystemTime(sysTime)
    utctime = DateSerial(sysTime.wYear, sysTime.wMonth, sysTime.wDay) + TimeSerial(sysTime.wHour, sysTime.wMinute, sysTime.wSecond)
    GetUTC = utctime
End Function

Private Function GetSystemTime(lpSystemTime As SYSTEMTIME) As Long
    ' Declaration to get system time
    GetSystemTime = GetSystemTimeAPI(lpSystemTime)
End Function

هذه الدوال توفر الحصول على الوقت الحالي بالتوقيت العالمي (UTC)

SYSTEMTIME هو هيكل يستخدم لتخزين التاريخ والوقت
GetSystemTimeAPI هى احد دوال API لـ Windows وظيفتها الحصول على الوقت العالمي (UTC)
GetUTC هى دالة تستدعي الدالة GetSystemTimeAPI للحصول على الوقت الحالي بالتوقيت العالمي (UTC) ويتم اعادته كقيمة تاريخ/وقت

طيب بعد ذلك الاكواد داخل النموذج
النموذج يعرض توقيتات متعددة لدول مختلفة بناء على الاعدادات الموجودة في الجدول tblTimeZone

Const FormatDisplayDate As String = "dd/mm/yyyy"
Const FormatDisplayTime As String = "hh:mm:ss AM/PM"
Const CountDisplayCountry As Integer = 5

Private Sub Form_Load()
    ' Set the form's timer interval to update every 1 second
    Me.TimerInterval = 1000
    ' Call the function to update times and dates
    UpdateTimes
End Sub

Private Sub Form_Timer()
    ' Call the function to update times and dates when the timer event occurs
    UpdateTimes
End Sub

Private Sub UpdateTimes()
    On Error GoTo ErrorHandler

    Dim rs As DAO.Recordset
    Dim utctime As Date
    Dim i As Integer

    ' Get the current UTC time
    utctime = GetUTC()
    ' Debug.Print "UTC Time: "; utctime

    ' Open the recordset to fetch data from the tblTimeZones table
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblTimeZones WHERE ShowInForm = True")

    ' Check if recordset is not empty
    If Not rs.EOF Then
        rs.MoveFirst
        i = 1
        ' Loop through each record in the recordset and update the form fields
        Do While Not rs.EOF And i <= CountDisplayCountry ' Limiting to 5 fields as per your requirement

            ' Assign values to form fields for each country
            If FieldExists("txtCountry" & i) Then
                Me("txtCountry" & i) = rs!CountryName
                Me("txtTimeDifference" & i) = rs!TimeDifference
                Me("chkDaylightSavingTime" & i) = rs!DaylightSavingTime
                
                ' Adjust time and date based on daylight saving time
                Dim localTime As Date
                If rs!DaylightSavingTime Then
                    localTime = DateAdd("h", rs!TimeDifference + 1, utctime)
                Else
                    localTime = DateAdd("h", rs!TimeDifference, utctime)
                End If
      
                Me("txtTime" & i) = Format(localTime, FormatDisplayTime)
                Me("txtDate" & i) = Format(localTime, FormatDisplayDate)
            End If
            rs.MoveNext
            i = i + 1
        Loop
    Else
        ' Display a message if no records found for countries to display
        'MsgBox "No countries found to display in the form.", vbExclamation, "No Records"
        Exit Sub
    End If

    ' Close the recordset
    rs.Close
    Set rs = Nothing
      
    Exit Sub

ExitHandler:
    Exit Sub

ErrorHandler:
    Select Case Err.Number
        Case 2465
            ' Can't find the Object
            Resume ExitHandler
        Case Else
            MsgBox "Error in UpdateTimes: " & Err.Number & vbCrLf & Err.Description, vbExclamation
            'Debug.Print Err.Number & " " & Err.Description
            Resume ExitHandler
    End Select
End Sub

Private Function FieldExists(fieldName As String) As Boolean
    ' Check if a field exists in the form
    On Error Resume Next
    FieldExists = (Me(fieldName).Name <> "")
    On Error GoTo 0
End Function

الاعلان عن الثوابت 
Const FormatDisplayDate     :  للتحكم فى شكل تسيق التاريخ الذى سوف يتم عرضه
Const FormatDisplayTime     : للتحكم فى شكل تسيق الوقت الذى سوف يتم عرضه
Const CountDisplayCountry  : تحديد عدد الدول التى نريد عرض اوقاتها فى النموذج والذى على اساسة ايضا عدد العناصر فى النموذج لهذه البيانات

Form_Load: عند تحميل النموذج، يتم تعيين الفاصل الزمني للمؤقت إلى ثانية واحدة ثم يتم استدعاء الدالة UpdateTimes

Form_Timer: يتم استدعاء الدالة UpdateTimes كل ثانية لتحديث التوقيتات

UpdateTimes

وظيفة هذه الدالة هي الحصول على الوقت الحالي بالتوقيت العالمي (UTC) باستخدام الدالة GetUTC

فتح مجموعة السجلات من الجدول  tblTimeZones لجلب البيانات بناؤ على شرط أن يكون الحقل ShowInForm مضبوطًا على True

في حلقة تكرارية يتم تحديث البيانات في العناصر في النموذج بناء على بيانات السجلات مع الأخذ بعين الاعتبار التوقيت الصيفي إذا كان مفعلاً

يتم التعامل مع الأخطاء باستخدام كتلة ErrorHandler لضمان عدم تعطل البرنامج بسبب الأخطاء

FieldExists: دالة للتحقق مما إذا كان عنصر معين موجودا في النموذج

جدول  tblTimeZones يحتوي على بيانات عن بلدان مختلفة بما في ذلك فرق التوقيت والتوقيت الصيفي وما إذا كانت البيانات يجب عرضها حيث يتم عرض البلدان المحددة فقط  من خلال (ShowInForm = True) في النموذج

العناصر فى النموذج كالاتى
txtCountry1, txtCountry2, txtCountry3, txtCountry4, txtCountry5
المفروض يتم جلب اسماء البلدان من الجدول هنا
-----------------------------------
txtTime1, txtTime2, txtTime3, txtTime4, txtTime5
المفروض يتم عرض التوقيت المحلى لكل بلد هنا
-----------------------------------
txtTimeDifference1, txtTimeDifference2, txtTimeDifference3, txtTimeDifference4, txtTimeDifference5
المفروض يتم جلب الفرق في التوقيت لكل بلد هنا
-----------------------------------
chkDaylightSavingTime1, chkDaylightSavingTime2, chkDaylightSavingTime3, chkDaylightSavingTime4, chkDaylightSavingTime5
المفروض يتم عرض ان كان التوقيت الصيفي مفعلا ام لا هنا
-----------------------------------
txtDate1, txtDate2, txtDate3, txtDate4, txtDate5
المفروض يتم عرض التاريخ طبقا للتوقيت المحلى لكل بلد هنا

-----------------------------------
المفروض كل ذلك يحدث من خلال الكود بمجرد فتح النموذج بطريقة الية والشرط طبعا هو جلب البيانات بناء على البلدان المختارة عرض بيناتها من خلال اختيارها من الحقل ShowInForm

 

واخيرا المرفقات 
المرفق الاول وهو الاساس والذى تم استعراض الافكار والاكواد  السابقة طبقا له
المرفق الثانى فقط تم اضافة عدد نماذج لساعات على ان تكون نماذج فرعية 

---.PNG.70f59752a0fb14100aef46c468962e62.PNG

 

 

TimeZones.zip TimeZones UP 2.zip

  • Like 1
  • Thanks 3
رابط هذا التعليق
شارك

عظمة على عظمة يا سيد المعلمين :clapping:

لله درك .. دايما تجيب الديب من ديله .. بسم الله ما شاء الله تبارك الله 😂

روووح ربنا يوفقك يا عم 😊🌼🌹

  • Haha 1
رابط هذا التعليق
شارك

في 29‏/5‏/2024 at 22:28, kanory said:

مشاء الله تبارك الله

فكرة الساعات بصراحة هى اللى تعبتنى حبتين

ولكن يا استاذى الجليل كنتم السبب فيها بوضعكم لصورة تخيلية عن الموضوع بشكل الساعات
وانا تعلمتم على ايديكم ولازلت اتعلم منكم وكل اساتذتى العظماء جزاكم الله عنا كل خير 

  • Thanks 1
رابط هذا التعليق
شارك

  • 1 month later...
في 29‏/5‏/2024 at 19:31, kkhalifa1960 said:

استاذنا @ابو جودي من أول مرة ساعدتني وأنت مبدع . جزاك الله كل خير . :fff::wavetowel::fff:

العفو منكم استاذى الجليل وملمى القدير انا اقل طويلب علم ينهل من ينابيع علومكم انتم وباقى الاساتذة العظماء يعلم الله تعالى انا تعلمت ولازلت اتعلم على اياديكم بارك الله لنا فيكم وكل اساتذتى وجعل الله لكم بكل حرف حسنة ويضاعف لكم من واسع فضله عليكم اضعافا مضاعفة

 

هههههه اسف الرد متأخر جدا النور قطع ونسيت والله :yes:

رابط هذا التعليق
شارك

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • اضف...

Important Information