How to get GMT time on MAC with vba.

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
439
Office Version
  1. 2016
Platform
  1. Windows
Currently using the following on Windows to get GMT/UTC time but I need a way of doing this on MAC as well. Thanks in advance.
VBA Code:
Function UTC() As Date

Dim dt As Object

Set dt = CreateObject("WbemScripting.SWbemDateTime")
    
With dt
    .SetVarDate Now
    UTC = .GetVarDate(False)
End With
    'Debug.Print UTC
End Function
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here is what I have for Mac Excel 2011:
VBA Code:
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
Function UTC() As Date
    Dim f As Long, s As String, r As Long, x As String
    f = popen("date -u +%T", "r")
    If f = 0 Then Exit Function
    While feof(f) = 0
        s = Space(50)
        r = fread(s, 1, Len(s) - 1, f)
        If r > 0 Then
            s = Left$(s, r)
            x = x & s
        End If
    Wend
    UTC = CDate(x)
End Function
 
Upvote 0
Here is what I have for Mac Excel 2011:
Thank you for your response. Unfortunately I have no way of testing it until Thursday.

If the user isn't on Excel 2011 would the following conditional compilers in the code below be correct?

The following link shows that ptrsafe is needed for MAC excel 2016 but not 2011. VBA - Mac APIs | vba Tutorial
What about older versions ?

VBA Code:
 #If VBA7 Then

        Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
        Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
        Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
        Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr

 #Else
        Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
        Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
        Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
 #End If
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top