Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for utc2iso.vbs

(view source code of utc2iso.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim blnDate, blnTime
  4. Dim dtmBase, dtmUTC
  5. Dim intDay, intHour, intMin, intMonth, intSec, intUTC, intValid, intYear
  6. Dim strISO
  7.  
  8. With WScript.Arguments
  9. 	' Check command line arguments
  10. 	If .Unnamed.Count <> 1 Then Syntax
  11. 	intUTC = .Unnamed(0)
  12. 	If Not IsNumeric( intUTC ) Then Syntax
  13. 	intValid = 0
  14. 	blnDate  = True
  15. 	blnTime  = True
  16. 	If .Named.Exists( "D" ) Then
  17. 		blnDate  = True
  18. 		blnTime  = False
  19. 		intValid = intValid + 1
  20. 	End If
  21. 	If .Named.Exists( "T" ) Then
  22. 		blnDate  = False
  23. 		blnTime  = True
  24. 		intValid = intValid + 1
  25. 	End If
  26. 	If intValid <> .Named.Count Then Syntax
  27. 	If intValid > 1 Then Syntax
  28. End With
  29.  
  30. dtmBase  = "1/1/1970" ' UTC base date
  31. ' Calculate the date by adding the Unix time in seconds to the UTC base date
  32. dtmUTC   = DateAdd( "s", intUTC, dtmBase )
  33. ' Format the output string
  34. intYear  = DatePartLZ( "yyyy", dtmUTC )
  35. intMonth = DatePartLZ( "m", dtmUTC )
  36. intDay   = DatePartLZ( "d", dtmUTC )
  37. intHour  = DatePartLZ( "h", dtmUTC )
  38. intMin   = DatePartLZ( "n", dtmUTC )
  39. intSec   = DatePartLZ( "s", dtmUTC )
  40. If blnDate Then strISO = intYear & "-" & intMonth & "-" & intDay
  41. If blnTime Then strISO = strISO & " " & intHour & ":" & intMin & ":" & intSec
  42. ' Display the result
  43. WScript.Echo Trim( strISO )
  44.  
  45.  
  46. Function DatePartLZ( myInterval, myDate )
  47. 	' Add a leading zero to the DatePart() if necessary
  48. 	Dim strDatePart
  49. 	strDatePart = DatePart( myInterval, myDate )
  50. 	If Len( strDatePart ) < 2 Then strDatePart = "0" & strDatePart
  51. 	DatePartLZ = strDatePart
  52. End Function
  53.  
  54.  
  55. Sub Syntax
  56. 	WScript.Echo vbcrlf _
  57. 	           & "UTC2ISO.vbs,  Version 1.01" _
  58. 	           & vbCrLf _
  59. 	           & "Convert Unix time (UTC) to ISO date and/or time" _
  60. 	           & vbCrLf & vbCrLf _
  61. 	           & "Usage:  CSCRIPT.EXE  //NoLogo  UTC2ISO.vbs  unix_time  [ /D | /T ]" _
  62. 	           & vbCrLf & vbCrLf _
  63. 	           & "Where:  ""unix_time""   is the Unix time we want to convert" _
  64. 	           & vbCrLf _
  65. 	           & "                      (min: -2147483647, max: 2147483647)" _
  66. 	           & vbCrLf _
  67. 	           & "        /D            return date only (default: both date and time)" _
  68. 	           & vbCrLf _
  69. 	           & "        /T            return time only (/D and /T are mutually exclusive)" _
  70. 	           & vbCrLf & vbCrLf _
  71. 	           & "Note:   Though often called UTC, Unix time does not take into account leap" _
  72. 	           & vbCrLf _
  73. 	           & "        seconds, while ""official"" UTC does." _
  74. 	           & vbCrLf & vbCrLf _
  75. 	           & "Written by Rob van der Woude" _
  76. 	           & vbCrLf _
  77. 	           & "http://www.robvanderwoude.com"
  78. 	WScript.Quit 1
  79. End Sub
  80.  

page last modified: 2024-04-16; loaded in 0.0168 seconds