(view source code of utc2iso.vbs as plain text)
Option Explicit
Dim blnDate, blnTime
Dim dtmBase, dtmUTC
Dim intDay, intHour, intMin, intMonth, intSec, intUTC, intValid, intYear
Dim strISO
With WScript.Arguments
' Check command line arguments
If .Unnamed.Count <> 1 Then Syntax
intUTC = .Unnamed(0)
If Not IsNumeric( intUTC ) Then Syntax
intValid = 0
blnDate = True
blnTime = True
If .Named.Exists( "D" ) Then
blnDate = True
blnTime = False
intValid = intValid + 1
End If
If .Named.Exists( "T" ) Then
blnDate = False
blnTime = True
intValid = intValid + 1
End If
If intValid <> .Named.Count Then Syntax
If intValid > 1 Then Syntax
End With
dtmBase = "1/1/1970" ' UTC base date
' Calculate the date by adding the Unix time in seconds to the UTC base date
dtmUTC = DateAdd( "s", intUTC, dtmBase )
' Format the output string
intYear = DatePartLZ( "yyyy", dtmUTC )
intMonth = DatePartLZ( "m", dtmUTC )
intDay = DatePartLZ( "d", dtmUTC )
intHour = DatePartLZ( "h", dtmUTC )
intMin = DatePartLZ( "n", dtmUTC )
intSec = DatePartLZ( "s", dtmUTC )
If blnDate Then strISO = intYear & "-" & intMonth & "-" & intDay
If blnTime Then strISO = strISO & " " & intHour & ":" & intMin & ":" & intSec
' Display the result
WScript.Echo Trim( strISO )
Function DatePartLZ( myInterval, myDate )
' Add a leading zero to the DatePart() if necessary
Dim strDatePart
strDatePart = DatePart( myInterval, myDate )
If Len( strDatePart ) < 2 Then strDatePart = "0" & strDatePart
DatePartLZ = strDatePart
End Function
Sub Syntax
WScript.Echo vbcrlf _
& "UTC2ISO.vbs, Version 1.01" _
& vbCrLf _
& "Convert Unix time (UTC) to ISO date and/or time" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT.EXE //NoLogo UTC2ISO.vbs unix_time [ /D | /T ]" _
& vbCrLf & vbCrLf _
& "Where: ""unix_time"" is the Unix time we want to convert" _
& vbCrLf _
& " (min: -2147483647, max: 2147483647)" _
& vbCrLf _
& " /D return date only (default: both date and time)" _
& vbCrLf _
& " /T return time only (/D and /T are mutually exclusive)" _
& vbCrLf & vbCrLf _
& "Note: Though often called UTC, Unix time does not take into account leap" _
& vbCrLf _
& " seconds, while ""official"" UTC does." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" _
& vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0057 seconds