I want to get time with millisecond
Currently using Timer() method but it just give access upto second
Any idea?
Please make sure i don't want to convert second into millisecond want to get with millisecond
In fact Timer function gives you seconds with milliseconds. Integer part of returned value is the number of seconds since midnight and the fraction part can be converted into milliseconds - just multiply it by 1000.
t = Timer
' Int() behaves exactly like Floor() function, i.e. it returns the biggest integer lower than function's argument
temp = Int(t)
Milliseconds = Int((t-temp) * 1000)
Seconds = temp mod 60
temp = Int(temp/60)
Minutes = temp mod 60
Hours = Int(temp/60)
WScript.Echo Hours, Minutes, Seconds, Milliseconds
' Let's format it
strTime = String(2 - Len(Hours), "0") & Hours & ":"
strTime = strTime & String(2 - Len(Minutes), "0") & Minutes & ":"
strTime = strTime & String(2 - Len(Seconds), "0") & Seconds & "."
strTime = strTime & String(4 - Len(Milliseconds), "0") & Milliseconds
WScript.Echo strTime
This uses the standard dateserial in vbs
'get a timeserial with milliseconds
d1= date+ timer/86400
Wscript.echo datestamp(d1)&" "& timestamp(d1)
function datestamp(d)
datestamp=year(d) &"/"& month(d) & "/" & day(d)
end function
function timestamp(d)
ymd=fix(d)
h=hour(d)
m=minute(d)
s=second(d)
timestamp=right("00" & h,2)&":"& right("00" & m,2)& ":" & right("00" & s,2)& ","& _
right("000"& 86400000*(d-timeserial(h,m,s)-ymd),3)
end function
Building upon MBu's answer, here's a Sub version. Sprinkle calls to this around your code for messages in the immediate window, so you can see where delays are happening.
' *** Debug.Print the time with milliseconds, and a message of your choice
Private Sub DebugPrintTime(strWhereFrom As String)
On Error GoTo ErrHandler
Dim sglTimer As Single
Dim sglWholeSecs As Single
Dim Millisecs As Variant ' as a variant, Len() will give the length of string representation of this value
Dim Seconds As Variant
Dim Minutes As Variant
Dim Hours As Variant
Dim strTime As String
sglTimer = timer
sglWholeSecs = Int(sglTimer)
Millisecs = Int((sglTimer - sglWholeSecs) * 1000)
Seconds = sglWholeSecs Mod 60
sglWholeSecs = Int(sglWholeSecs / 60)
Minutes = sglWholeSecs Mod 60
Hours = Int(sglWholeSecs / 60)
strTime = String(2 - Len(Hours), "0") & Hours & ":"
strTime = strTime & String(2 - Len(Minutes), "0") & Minutes & ":"
strTime = strTime & String(2 - Len(Seconds), "0") & Seconds & "."
strTime = strTime & String(3 - Len(Millisecs), "0") & Millisecs
Debug.Print strTime, strWhereFrom
Exit Sub
ErrHandler:
MsgBox "Error in Sub DebugPrintTime" & vbCrLf & Err.Description & vbCrLf & strWhereFrom
Err.Clear
End Sub
Related
I have last boot time from WMI and it looks as '20141103113859.220250+060'. i want to convert it to number of days and time from the current time.
is it possible?
From Help
Use the SWbemDateTime object to convert these to regular dates and times.
Windows 2000/NT and Windows 98/95: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
Set dtmInstallDate = CreateObject( _
"WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery( _
"Select * from Win32_OperatingSystem")
For Each strOS in objOS
dtmInstallDate.Value = strOS.InstallDate
Wscript.Echo dtmInstallDate.GetVarDate
Next
To get help.
http://msdn.microsoft.com/en-us/windows/hardware/hh852363
Install the Windows SDK but just choose the documentation.
Next simple function should work for any argument in valid CIM_DATETIME format.
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) _
& "/" & Mid(dtmDate, 5, 2) _
& "/" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2))
End Function
An example:
InstallDate (wmi): 20141205231553.000000+060
InstallDate: 2014/12/05 23:15:53
However, a wmi query could return Null, e.g. VarType(dtmDate)=1 for a particular instance of a date; in next script is the function modified:
option explicit
Dim strWmiDate
strWmiDate = "20141103113859.220250+060"
Wscript.Echo strWmiDate _
& vbNewLine & WMIDateStringToDate(strWmiDate) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(strWmiDate), Now) _
& vbNewLine _
& vbNewLine & WMIDateStringToDate(Null) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(Null), Now)
Function WMIDateStringToDate(byVal dtmDate)
If VarType(dtmDate)=1 Then
WMIDateStringToDate = FormatDateTime( Now) 'change to whatever you want
Else
'
' to keep script locale independent:
' returns ANSI (ISO 8601) datetime format (24 h)
'
' yyyy-mm-dd HH:MM:SS
'
WMIDateStringToDate = Left(dtmDate, 4) _
& "-" & Mid(dtmDate, 5, 2) _
& "-" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2)
End If
End Function
Output:
==>cscript 29535638.vbs
20141103113859.220250+060
2014-11-03 11:38:59
157
09.04.2015 15:36:38
0
#Serenity has given this same answer while i was writting, but ...
Option Explicit
WScript.Echo getLastBootUpTime()
WScript.Echo WMIDate2Date( "20141103113859.220250+060" )
WScript.Echo GetElapsedTime( getLastBootUpTime(), Now )
Function WMIDate2Date( ByVal WMIDate )
With WScript.CreateObject("WbemScripting.SWbemDateTime")
.Value = WMIDate
WMIDate2Date = .GetVarDate(False)
End With
End Function
Function getLastBootUpTime()
Dim oOS
For Each oOS In GetObject( "winmgmts:\\.\root\cimv2").ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
getLastBootUpTime = WMIDate2Date(oOS.LastBootUpTime)
Next
End Function
Function GetElapsedTime( ByVal Date1, ByVal Date2 )
Dim seconds, aLabels, aValues, aDividers, i
aLabels = Array( " days, ", ":", ":", "" )
aDividers = Array( 86400, 3600, 60, 1 )
aValues = Array( 0, 0, 0, 0 )
i = 0
seconds = Abs( DateDiff( "s", Date1, Date2 ))
Do While seconds > 0
aValues(i) = Fix( seconds / aDividers(i) )
seconds = seconds - aValues(i) * aDividers(i)
aValues(i) = CStr(aValues(i)) & aLabels(i)
i=i+1
Loop
GetElapsedTime = Join(aValues, "")
End Function
You won't get around splitting the WMI date string to make it to a date string that VBScript understands. Try this:
<%
wmiDate = "20141103113859.220250+060"
' note i am using date format: [m/d/Y H:m:s]
' if you prefer other format, i.e. [d.m.Y H:m:s] switch mid offsets
fromDate = Mid(wmiDate,5,2) & "/" & Mid(wmiDate,7,2) & "/" & Left(wmiDate,4)
fromTime = Mid(wmiDate,9,2) & ":" & Mid(wmiDate,11,2) & ":" & Mid(wmiDate,13,2)
toDate = Date & " " & Time
response.write(DateDiff("d",fromDate & " " & fromTime,toDate) & " Days<br />")
response.write(DateDiff("h",Date & " " & fromTime,toDate) & " Hours<br />")
%>
It uses Mid()and Left()functions to split WMI date into the needed parts for VBScript. Then the DateDiff() function will deliver the interval difference first for d= days and then for h= hours. You will notice when calculating hours i just used the time part of the WMI string, since we already calculated days difference, we only want hours left over.
Interesting article explaining VBScript Date and Time (Iso Formats)
As a comment was so kindly remarking the date format i used and the result of the hour calculation, i added a comment line explaining the date format i used (i used m/d/Y H:m:s but depending on your local, you might prefer d.m.Y H:m:s then you need to swap the Mid() offsets to get the right order). I also appended the current Time to the toDate and in the hour calculation prepended the current Date to calculate the correct time difference.
'========================================================================
'## Global Object and Variable Settings
'========================================================================
Dim WshShell: Set WshShell = CreateObject("WScript.Shell")
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strSystemDrive: strSystemDrive = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
'========================================================================
'## Main Code
'========================================================================
'On Error Resume Next
'---------------------------------------------------------------------------------------------------------------------------
'## Script Variables
'---------------------------------------------------------------------------------------------------------------------------
Dim strFolder: strFolder = strSystemDrive & "\Endpoint Reboot Info"
Dim strFile: strFile = strSystemDrive & "\Endpoint Reboot Info\Endpoint_Reboot_Logfile.txt"
strcurrentDateTime = Now()
Dim string0: string0 = "Endpoint restart analysis initialized"
Dim string1: string1 = "Calculating the endpoint uptime .. If greater than 14 days, machine will be restarted"
Dim string2: string2 = "Warning: The current OS Uptime exceeds 14 days! This system will be rebooted!"
Dim string3: string3 = " As the current OS Uptime is less than 14 days, this system will NOT be rebooted currently"
Dim string4: string4 = "This system will restart now!"
Dim string5: string5 = "User has clicked cancel, hence PC was NOT restarted"
Dim string6: string6 = "User has clicked OK. Restarting PC now.."
'---------------------------------------------------------------------------------------------------------------------------
'## Code for creating the folder and file necessary for logging and initializing the log file
'---------------------------------------------------------------------------------------------------------------------------
Function CreateLogFile(filename)
Dim f: set f = objFSO.OpenTextFile(strFile, 8, True)
f.WriteLine strcurrentDateTime & " " & string0
f.WriteLine strcurrentDateTime & " " & string1
Set CreateLogFile = f
End Function
'---------------------------------------------------------------------------------------------------------------------------
'## Code for checking endpoint OS uptime and force restarting post message display to end user
'---------------------------------------------------------------------------------------------------------------------------
Dim objTSO: Set objTSO = CreateLogFile(strFile)
strComputer = "."
Const FOR_APPENDING = 8
SET objWMIDateTime = CREATEOBJECT("WbemScripting.SWbemDateTime")
SET objWMI = GETOBJECT("winmgmts:\\" & strComputer & "\root\cimv2")
SET colOS = objWMI.InstancesOf("Win32_OperatingSystem")
FOR EACH objOS in colOS
objWMIDateTime.Value = objOS.LastBootUpTime
objTSO.WriteLine "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
"System Up Time: " & TimeSpan(objWMIDateTime.GetVarDate,NOW) & _
" (hh:mm:ss)"
NEXT
FUNCTION TimeSpan(dt1, dt2)
' Function to display the difference between
' 2 dates in hh:mm:ss format
IF (ISDATE(dt1) AND ISDATE(dt2)) = FALSE THEN
TimeSpan = "00:00:00"
EXIT FUNCTION
END IF
seconds = ABS(DATEDIFF("S", dt1, dt2))
minutes = seconds \ 60
hours = minutes \ 60
minutes = minutes MOD 60
seconds = seconds MOD 60
IF LEN(hours) = 1 THEN hours = "0" & hours
TimeSpan = hours & ":" & _
RIGHT("00" & minutes, 2) & ":" & _
RIGHT("00" & seconds, 2)
If (hours > 336) Then
f.WriteLine strcurrentDateTime & " " & string2
Dim retval: retval = InputBox("Warning!: The current OS Uptime exceeds 14 days! This system will be rebooted! Please save ALL of your work and ONLY then click OK")
If IsEmpty(retval) Then
msgbox ("User has terminated the action by clicking cancel")
objTSO.WriteLine string5
Else
objTSO.WriteLine string6
WshShell.Run "shutdown.exe -R -T 0"
End If
Else
WScript.Sleep 10000
strcurrentDateTime = Now()
objTSO.WriteLine strcurrentDateTime & string3
WScript.Quit
End If
f.Close()
END FUNCTION
'---------------------------------------------------------------------------------------------------------------------------
'## End of code/VB Script
'---------------------------------------------------------------------------------------------------------------------------
I have now put up the full script as suggested. Please help with below queries:
This Line is not getting written into the log file
objTSO.WriteLine "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
"System Up Time: " & TimeSpan(objWMIDateTime.GetVarDate,NOW) & _
" (hh:mm:ss)"
Also please check the logic of the code by changing the (hours>336) condition. This script will be executed locally on the machines, so instead of the previous reboot function, i have now modified it to WshShell.Run "shutdown.exe -R -T 0"
Please guide! Thanks!!
Your function CreateLogFile creates/opens the file, but then closes it right away:
Function CreateLogFile()
If objFSO.FileExists(strFile) Then
Set objTSO = objFSO.OpenTextFile(strFile, FOR_APPENDING)
objTSO.WriteLine strcurrentDateTime & " " & string0
objTSO.WriteLine strcurrentDateTime & " " & string1
objTSO.Close()
Else
objFSO.CreateTextFile(strFile)
Set objTSO = objFSO.OpenTextFile(strFile, FOR_APPENDING)
objTSO.WriteLine strcurrentDateTime & " " & string0
objTSO.WriteLine strcurrentDateTime & " " & string1
objTSO.Close()
End If
End Function
meaning that when the function returns the file handle is already closed, causing all subsequent attempts to write to the file (without opening it again) to fail.
What you actually want to do is change your function to something like this:
Function CreateLogFile(filename)
Dim f : Set f = objFSO.OpenTextFile(filename, 8, True)
f.WriteLine Now & " " & string0
f.WriteLine Now & " " & string1
Set CreateLogFile = f
End Function
Dim objTSO : Set objTSO = CreateLogFile(strFile)
...
objTSO.Close 'at the end of the script
Setting the 3rd parameter of the OpenTextFile method to True causes it to create the file in case it's missing.
How to generating pulse using VBScript?
I tried with Timer function, but no idea how to use it.
t1 = Timer
temp = Int(t1)
Seconds = temp mod 60
strTime = String(2 - Len(Hours), "0") & Hours & ":"
strTime = strTime & String(2 - Len(Minutes), "0") & Minutes & ":"
strTime = strTime & String(2 - Len(Seconds), "0") & Seconds
I have last boot time from WMI and it looks as '20141103113859.220250+060'. i want to convert it to number of days and time from the current time.
is it possible?
From Help
Use the SWbemDateTime object to convert these to regular dates and times.
Windows 2000/NT and Windows 98/95: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
Set dtmInstallDate = CreateObject( _
"WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery( _
"Select * from Win32_OperatingSystem")
For Each strOS in objOS
dtmInstallDate.Value = strOS.InstallDate
Wscript.Echo dtmInstallDate.GetVarDate
Next
To get help.
http://msdn.microsoft.com/en-us/windows/hardware/hh852363
Install the Windows SDK but just choose the documentation.
Next simple function should work for any argument in valid CIM_DATETIME format.
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) _
& "/" & Mid(dtmDate, 5, 2) _
& "/" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2))
End Function
An example:
InstallDate (wmi): 20141205231553.000000+060
InstallDate: 2014/12/05 23:15:53
However, a wmi query could return Null, e.g. VarType(dtmDate)=1 for a particular instance of a date; in next script is the function modified:
option explicit
Dim strWmiDate
strWmiDate = "20141103113859.220250+060"
Wscript.Echo strWmiDate _
& vbNewLine & WMIDateStringToDate(strWmiDate) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(strWmiDate), Now) _
& vbNewLine _
& vbNewLine & WMIDateStringToDate(Null) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(Null), Now)
Function WMIDateStringToDate(byVal dtmDate)
If VarType(dtmDate)=1 Then
WMIDateStringToDate = FormatDateTime( Now) 'change to whatever you want
Else
'
' to keep script locale independent:
' returns ANSI (ISO 8601) datetime format (24 h)
'
' yyyy-mm-dd HH:MM:SS
'
WMIDateStringToDate = Left(dtmDate, 4) _
& "-" & Mid(dtmDate, 5, 2) _
& "-" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2)
End If
End Function
Output:
==>cscript 29535638.vbs
20141103113859.220250+060
2014-11-03 11:38:59
157
09.04.2015 15:36:38
0
#Serenity has given this same answer while i was writting, but ...
Option Explicit
WScript.Echo getLastBootUpTime()
WScript.Echo WMIDate2Date( "20141103113859.220250+060" )
WScript.Echo GetElapsedTime( getLastBootUpTime(), Now )
Function WMIDate2Date( ByVal WMIDate )
With WScript.CreateObject("WbemScripting.SWbemDateTime")
.Value = WMIDate
WMIDate2Date = .GetVarDate(False)
End With
End Function
Function getLastBootUpTime()
Dim oOS
For Each oOS In GetObject( "winmgmts:\\.\root\cimv2").ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
getLastBootUpTime = WMIDate2Date(oOS.LastBootUpTime)
Next
End Function
Function GetElapsedTime( ByVal Date1, ByVal Date2 )
Dim seconds, aLabels, aValues, aDividers, i
aLabels = Array( " days, ", ":", ":", "" )
aDividers = Array( 86400, 3600, 60, 1 )
aValues = Array( 0, 0, 0, 0 )
i = 0
seconds = Abs( DateDiff( "s", Date1, Date2 ))
Do While seconds > 0
aValues(i) = Fix( seconds / aDividers(i) )
seconds = seconds - aValues(i) * aDividers(i)
aValues(i) = CStr(aValues(i)) & aLabels(i)
i=i+1
Loop
GetElapsedTime = Join(aValues, "")
End Function
You won't get around splitting the WMI date string to make it to a date string that VBScript understands. Try this:
<%
wmiDate = "20141103113859.220250+060"
' note i am using date format: [m/d/Y H:m:s]
' if you prefer other format, i.e. [d.m.Y H:m:s] switch mid offsets
fromDate = Mid(wmiDate,5,2) & "/" & Mid(wmiDate,7,2) & "/" & Left(wmiDate,4)
fromTime = Mid(wmiDate,9,2) & ":" & Mid(wmiDate,11,2) & ":" & Mid(wmiDate,13,2)
toDate = Date & " " & Time
response.write(DateDiff("d",fromDate & " " & fromTime,toDate) & " Days<br />")
response.write(DateDiff("h",Date & " " & fromTime,toDate) & " Hours<br />")
%>
It uses Mid()and Left()functions to split WMI date into the needed parts for VBScript. Then the DateDiff() function will deliver the interval difference first for d= days and then for h= hours. You will notice when calculating hours i just used the time part of the WMI string, since we already calculated days difference, we only want hours left over.
Interesting article explaining VBScript Date and Time (Iso Formats)
As a comment was so kindly remarking the date format i used and the result of the hour calculation, i added a comment line explaining the date format i used (i used m/d/Y H:m:s but depending on your local, you might prefer d.m.Y H:m:s then you need to swap the Mid() offsets to get the right order). I also appended the current Time to the toDate and in the hour calculation prepended the current Date to calculate the correct time difference.
Does anyone have a simple means in VBScript to get the current time in UTC?
Thanx,
Chris
I use a simple technique
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate (now())
wscript.echo "Local Time: " & dateTime
wscript.echo "UTC Time: " & dateTime.GetVarDate (false)
More info on SWbemDateTime
If you wanted to convert UTC back to local time do this:
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate now(),false REM Where now is the UTC date
wscript.echo cdate(dateTime.GetVarDate (true))
There are lots of examples out there. If you can access the registry this one will work for you:
od = now()
set oShell = CreateObject("WScript.Shell")
atb = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\" &_
"Control\TimeZoneInformation\ActiveTimeBias"
offsetMin = oShell.RegRead(atb)
nd = dateadd("n", offsetMin, od)
Response.Write("Current = " & od & "<br>UTC = " & nd)
From http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-convert-local-time-to-utc-gmt-time.html
You can get time bias from Win32_TimeZone WMI class.
myDate = "9/4/2013 17:23:08"
For Each objItem In GetObject(_
"winmgmts:\\.\root\cimv2").ExecQuery(_
"Select * from Win32_TimeZone")
bias = objItem.Bias
Next
myDate = DateAdd("n", bias, myDate)
WScript.Echo myDate
With SetVarDate the offset change due to transition to daylight saving time (from +060 to +120) occurred one hour too soon. The RegRead(HKLM\..\ActiveTimeBias) method was spot-on. If reproduction is desired, just put the pc clock on a time just before and just after the expected transition time and check the results.
Here is an example that formats the date to UTC as well. Note that you cannot format to a millesecond level with this.
Dim formattedDate
Dim utcDate
Set objShell = WScript.CreateObject("WScript.Shell")
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate(now())
utcDate = dateTime.GetVarDate(false)
wscript.echo "Local Time: " & dateTime
wscript.echo "UTC Time: " & utcDate
formattedDate = DatePart("yyyy",utcDate) & "-" & Right("0" & DatePart("m",utcDate), 2) & "-" & Right("0" & DatePart("d",utcDate), 2)
& "T" & Right("0" & DatePart("h",utcDate), 2) & ":" & Right("0" & DatePart("n",utcDate), 2)
& ":" & Right("0" & DatePart("s",utcDate), 2) & ".000+0000"
wscript.echo formattedDate
'results in a format that looks like this: 1970-01-01T00:00:00.000+0000
set dateTime=Nothing
set objShell=Nothing
Based on above functions - returns a delta-value to be added to the current time to return UTC.
Or call it with DATE+TIME to return UTC.
Call it once and store in a global variable to offset any date/time to UTC.
Conversely Subtract it from any UTC to get the time in the current time zone.
The additional ROUND towards the bottom is an attempt to compensate for floating point errors in the conversion to the nearest second.
Function Time_add_To_get_UTC(Optional DateTime = 0) ''as double
'' Returns value to add to current time to get to UTC
''Based on above functions : )
''return offset from current time to UTC
''https://stackoverflow.com/questions/15887700/utc-time-assignment-in-vbscript/22842128
Dim SWDT ''As SWbemDateTime
Dim dt ''As Date
Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
dt = Date + Time()
SWDT.SetVarDate (dt)
Time_add_To_get_UTC = CDbl(SWDT.GetVarDate(False)) - CDbl(SWDT.GetVarDate(True))
Time_add_To_get_UTC = CDbl(Round(Time_add_To_get_UTC * 24 * 60 * 60, 0) / 24 / 60 / 60)
Time_add_To_get_UTC = DateTime + Time_add_To_get_UTC
End Function