2015-04-21 18 views
9

In Java per ottenere il tempo di sistema in millisecondi che uso:Prendi tempo Unix millisecondi

new date().gettime() 

E 'possibile ottenere lo stesso risultato in millisecondi utilizzando Excel VBA?

+0

Perché vuoi il tempo di sistema in millisecondi? –

+0

Perché in un secondo posso postare> 1 volte e ho bisogno del tempo di controllo di ciascun post –

risposta

4

Sommario solo una possibile variante: Per ottenere i migliori risultati, utilizzare GetSystemTime.

La funzione di foglio di lavoro di Excel Now() ha una precisione relativamente buona, approssimativamente fino a 10 ms. Ma per chiamarlo devi usare una formula del foglio di lavoro.

Per ottenere correttamente il valore in millisecondi, è necessario evitare la funzione VBA Now(). La sua precisione è di circa 1 secondo.

La funzione VBA Timer() restituisce un valore single con una precisione di circa 5 millisecondi. Ma devi usare Now() per ottenere la parte della data. Ciò potrebbe causare un piccolo problema se Now() è chiamato prima di a mezzanotte e Timer() è chiamato dopo la mezzanotte (questa è probabilmente una situazione rara e non rappresenta un problema per la maggior parte delle persone).

La funzione API di Windows GetSystemTime ha una precisione millisecondo. È possibile utilizzare i valori nella struttura SYSTEMTIME per creare un doppio di Excel con la precisione millisecondo corretta. GetSystemTime restituisce l'ora UTC, quindi se si desidera la data in formato POSIX, è possibile sottrarre l'epoca UNIX (1 gennaio 1970 UTC), che è 25569 in formato data Excel (ignorando i secondi intercalari).

Il codice seguente mette a confronto la precisione di ciascun metodo:

Option Explicit 

Private 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 

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) 

Function Now_System() As Double 
    Dim st As SYSTEMTIME 
    GetSystemTime st 
    Now_System = DateSerial(st.wYear, st.wMonth, st.wDay) + _ 
     TimeSerial(st.wHour, st.wMinute, st.wSecond) + _ 
     st.wMilliseconds/86400000# 
End Function 

Function Now_Timer() As Double 
    Now_Timer = CDbl(Int(Now)) + CDbl(Timer()/86400#) 
End Function 


Sub CompareCurrentTimeFunctions() 
    ' Compare precision of different methods to get current time. 
    Me.Range("A1:D1000").NumberFormat = "yyyy/mm/dd h:mm:ss.000" 

    Dim d As Double 
    Dim i As Long 
    For i = 2 To 1000 
     ' 1) Excel NOW() formula returns same value until delay of ~10 milliseconds. (local time) 
     Me.Cells(1, 1).Formula = "=Now()" 
     d = Me.Cells(1, 1) 
     Me.Cells(i, 1) = d 

     ' 2) VBA Now() returns same value until delay of ~1 second. (local time) 
     d = Now 
     Me.Cells(i, 2) = d 

     ' 3) VBA Timer returns same value until delay of ~5 milliseconds. (local time) 
     Me.Cells(i, 3) = Now_Timer 

     ' 4) System time is precise down to 1 millisecond. (UTC) 
     Me.Cells(i, 4) = Now_System 
    Next i 
End Sub 
3

diversa interpretazione, sulla base di Excel posix time e con un adeguamento un'ora per il periodo estivo:

Sub Pose() 
    ut = ((Now - 25569) * 86400000) - 3600000 
End Sub 

Se non sufficientemente precisa, http://vbadud.blogspot.co.uk/2008/10/excel-vba-timestamp-milliseconds-using.html può essere di interesse.

+0

Nessun risultato non è lo stesso !!! Come il tuo esempio restituisce sempre 000 alla fine, e il tuo link non restituisce la stringa numerica come mi serve –

+0

Nope, Type mismatch –

+0

Riprova !: '+ (Format (Timer," 0.000 ") - Int (Format (Timer," 0.000 "))) * 1000'. – pnuts

1

Questo produce un timestamp nel formato yyyy mm dd hh:mm:ss.fff dove i millisecondi sono fff.

Dim dateToday As Date 
Dim datetimeNow As Date 
Dim secondsElapsedSinceMidnight As Double 
Dim h As Long 
Dim m As Long 
Dim s As Long 

dateToday = Now 
secondsElapsedSinceMidnight = Timer 

h = Int(secondsElapsedSinceMidnight/3600) 
m = Int(secondsElapsedSinceMidnight/60) - h * 60 
s = Int(secondsElapsedSinceMidnight) - m * 60 - h * 3600 

datetimeNow = DateSerial(Year(dateToday), Month(dateToday), Day(dateToday)) _ 
    + TimeSerial(h, m, s) 

Debug.Print Format(datetimeNow, "yyyy mm dd hh:nn:ss.") _ 
    & Format((secondsElapsedSinceMidnight _ 
     - Int(secondsElapsedSinceMidnight)) * 1000, "000") 

Come presento questa risposta, il risultato è:

2015 04 21 16:24:22.852 
+0

È un formato leggibile dall'uomo ma ho bisogno solo dei numeri –

+1

Quindi non formattarlo come ho fatto alla fine! –

1

ho trovato

Private 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 

Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) 
Sub test() 
Dim sSysTime As SYSTEMTIME 

GetLocalTime sSysTime 

MsgBox = ((Now - 25569) * 86400000) - 3600000 + sSysTime.wMilliseconds 
End Sub 
+0

Dovresti evitare di usare due diverse funzioni per ottenere il tempo. Avrai incongruenze tra i due risultati. Per ottenere risultati migliori, utilizzare solo la struttura SYSTEMTIME. – bouvierr

2

Ecco una breve proroga sulla risposta da @bouvierr come ho bisogno l'equivalente del metodo java.lang.System.currentTimeMillis() in VBA:

Private 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 

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) 

Function CurrentTimeMillis() As Double 
    ' Returns the milliseconds from 1970/01/01 00:00:00.0 to system UTC 
    Dim st As SYSTEMTIME 
    GetSystemTime st 
    Dim t_Start, t_Now 
    t_Start = DateSerial(1970, 1, 1) ' Starting time for Linux 
    t_Now = DateSerial(st.wYear, st.wMonth, st.wDay) + _ 
     TimeSerial(st.wHour, st.wMinute, st.wSecond) 
    CurrentTimeMillis = DateDiff("s", t_Start, t_Now) * 1000 + st.wMilliseconds 
End Function 
Problemi correlati