文章摘要
本文介绍了使用VBScript编写的一个脚本,该脚本用于将输入的日期和时间(可能包含时间部分)转换为Unix时间(即UTC)。脚本首先检查命令行参数,如果没有参数则使用当前日期和时间;如果有多个参数,则拼接成完整的日期字符串。通过错误处理机制(如On Error语句),确保脚本在输入无效时提示语法错误。此外,脚本还包含一个帮助子例程(Syntax函数),用于详细说明脚本的使用方法、输入参数的定义以及注意事项,帮助用户更好地理解和使用该脚本。整体来看,该脚本在处理日期和时间转换、命令行参数处理以及帮助信息输出方面功能完善。
Option Explicit
Dim dtmDate
If WScript.Arguments.Named.Count > 0 Then Syntax
With WScript.Arguments.Unnamed
‘ Check command line arguments
If .Count=0 Then dtmDate=Now
If .Count > 0 Then dtmDate=.Item(0)
If .Count > 1 Then dtmDate=dtmDate & ” ” & .Item(1)
If .Count > 2 Then dtmDate=dtmDate & ” ” & .Item(2)
If .Count > 3 Then Syntax
On Error Resume Next
dtmDate=CDate( dtmDate )
If Err Then
On Error Goto 0
Syntax
End If
On Error Goto 0
If Not IsDate( dtmDate ) Then Syntax
End With
‘ Calculate and display the result
WScript.Echo DateDiff( “s”, “1970-01-01 00:00:00”, dtmDate )
Sub Syntax
WScript.Echo vbcrlf _
& “Date2UTC.vbs, Version 1.00” _
& vbCrLf _
& “Convert any date/time to Unix time (UTC)” _
& vbCrLf & vbCrLf _
& “Usage: CSCRIPT.EXE //NoLogo Date2UTC.vbs date [ time ]” _
& vbCrLf & vbCrLf _
& “Where: “”date”” is the date to convert (default: current date/time)” _
& vbCrLf _
& ” “”time”” is the optional time to convert” _
& vbCrLf & vbCrLf _
& “Notes: Though often called UTC, Unix time does not take into account leap” _
& vbCrLf _
& ” seconds, while “”official”” UTC does.” _
& vbCrLf _
& ” If the specified date is ambiguous, the current user’s date” _
& vbCrLf _
& ” and time format is assumed.” _
& vbCrLf & vbCrLf _
& “Written by Rob van der Woude” _
& vbCrLf _
& “http://www.robvanderwoude.com”
WScript.Quit 1
End Sub