Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for utc2date.vbs

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

  1. Option Explicit
  2.  
  3. Dim blnDate, blnTime
  4. Dim dtmBase, dtmUTC
  5. Dim intDateFormat, intTimeFormat, intUTC, intValid
  6. Dim strResult
  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. 		If .Named.Exists( "T" ) Then Syntax
  18. 		blnDate  = True
  19. 		blnTime  = False
  20. 		intValid = intValid + 1
  21. 	End If
  22. 	If .Named.Exists( "T" ) Then
  23. 		blnDate  = False
  24. 		blnTime  = True
  25. 		intValid = intValid + 1
  26. 	End If
  27. 	If intValid <> .Named.Count Then Syntax
  28. 	If intValid > 1 Then Syntax
  29. End With
  30.  
  31. dtmBase  = "1/1/1970" ' UTC base date
  32. ' Calculate the date by adding the Unix time in seconds to the UTC base date
  33. dtmUTC   = DateAdd( "s", intUTC, dtmBase )
  34. ' Format the output string
  35. If blnDate Then strResult = FormatDateTime( DateValue( dtmUTC ), intDateFormat )
  36. If blnTime Then
  37. 	' Calculate and display the difference in seconds between the specified and the Unix time base date
  38. 	If blnDate Then strResult = strResult & ", "
  39. 	strResult = strResult & FormatDateTime( TimeValue( dtmUTC ), intTimeFormat )
  40. End If
  41. WScript.Echo Trim( strResult )
  42.  
  43.  
  44. Sub Syntax
  45. 	WScript.Echo vbcrlf _
  46. 	           & "UTC2Date.vbs,  Version 1.00" _
  47. 	           & vbCrLf _
  48. 	           & "Convert Unix time (UTC) to the current user's standard date/time format" _
  49. 	           & vbCrLf & vbCrLf _
  50. 	           & "Usage:  CSCRIPT.EXE  //NoLogo  UTC2Date.vbs  unix_time  [ /D | /T ]" _
  51. 	           & vbCrLf & vbCrLf _
  52. 	           & "Where:  ""unix_time""   is the Unix time we want to convert" _
  53. 	           & vbCrLf _
  54. 	           & "                      (min: -2147483647, max: 2147483647)" _
  55. 	           & vbCrLf _
  56. 	           & "        /D            return date only (default: both date and time)" _
  57. 	           & vbCrLf _
  58. 	           & "        /T            return time only (/D and /T are mutually exclusive)" _
  59. 	           & vbCrLf & vbCrLf _
  60. 	           & "Note:   Though often called UTC, Unix time does not take into account leap" _
  61. 	           & vbCrLf _
  62. 	           & "        seconds, while ""official"" UTC does." _
  63. 	           & vbCrLf & vbCrLf _
  64. 	           & "Written by Rob van der Woude" _
  65. 	           & vbCrLf _
  66. 	           & "http://www.robvanderwoude.com"
  67. 	WScript.Quit 1
  68. End Sub
  69.  

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