Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for stardate.bat

(view source code of stardate.bat as plain text)

  1. @ECHO OFF
  2. :: Check OS version and command line arguments
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. IF [%1]==[] GOTO Start
  5.  
  6. ECHO.
  7. ECHO StarDate.bat,  Version 1.00 for Windows NT
  8. ECHO Display the current stardate
  9. ECHO.
  10. ECHO Usage:  STARDATE
  11. ECHO.
  12. ECHO Algorithm found on The StarTrek Gallery
  13. ECHO http://www.trainerscity.com/startrek/
  14. ECHO.
  15. ECHO Written by Rob van der Woude
  16. ECHO http://www.robvanderwoude.com
  17. ECHO.
  18. IF NOT "%OS%"=="Windows_NT" GOTO End
  19.  
  20. :Start
  21. SETLOCAL
  22.  
  23. :: Determine temporary file name
  24. SET TempReg=%Temp:"=%
  25. SET TempReg="%TempReg%.\_Temp.reg"
  26.  
  27. :: Store current date and time in variables in default Locale format
  28. FOR /F "tokens=1* delims= " %%A IN ('DATE/T') DO SET Today=%%B
  29. FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A
  30.  
  31. :: Export registry settings to a temporary file
  32. START /W REGEDIT /E %TempReg% "HKEY_CURRENT_USER\Control Panel\International"
  33.  
  34. :: Read date and time format from exported registry file
  35. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TempReg% ^| FIND /I "iDate"') DO SET iDate=%%B
  36. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TempReg% ^| FIND /I "sDate"') DO SET sDate=%%B
  37. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TempReg% ^| FIND "iTime" ^| FIND /V "iTimePrefix"') DO SET iTime=%%B
  38. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TempReg% ^| FIND "sTime" ^| FIND /V "sTimeFormat"') DO SET sTime=%%B
  39.  
  40. :: Delete temporary file
  41. DEL %TempReg%
  42.  
  43. :: Remove quotes from variables
  44. SET iDate=%iDate:"=%
  45. SET sDate=%sDate:"=%
  46. SET iTime=%iTime:"=%
  47. SET sTime=%sTime:"=%
  48.  
  49. :: Parse date depending on registry settings
  50. IF %iDate%==0 FOR /F "tokens=1-4* delims=%sDate% " %%A IN ('DATE/T') DO (
  51. 	SET Day=%%C
  52. 	SET Month=%%B
  53. 	SET Year=%%D
  54. )
  55. IF %iDate%==1 FOR /F "tokens=1-4* delims=%sDate% " %%A IN ('DATE/T') DO (
  56. 	SET Day=%%B
  57. 	SET Month=%%C
  58. 	SET Year=%%D
  59. )
  60. IF %iDate%==2 FOR /F "tokens=1-4* delims=%sDate% " %%A IN ('DATE/T') DO (
  61. 	SET Day=%%D
  62. 	SET Month=%%C
  63. 	SET Year=%%B
  64. )
  65. IF %Year% LSS 100 SET /A Year = %Year% + 2000
  66.  
  67. :: Parse time depending on registry settings
  68. FOR /F "tokens=1,2* delims=%sTime% " %%A IN ('ECHO %Now%') DO (
  69. 	SET Hour=%%A
  70. 	SET Mins=%%B
  71. )
  72. IF %iTime%==1 GOTO Calc
  73. SET AmPm=%Mins:~2,1%
  74. SET Mins=%Mins:~0,2%
  75. IF %Hour% LSS 12 IF /I %AmPm%==P SET /A Hour = %Hour% + 12
  76. IF %Hour% EQU 12 IF /I %AmPm%==A SET Hour=0
  77.  
  78. :: Calculate stardate according to algorithm found on
  79. :: http://www.trainerscity.com/startrek/stardate.php3?lang=en
  80. :Calc
  81. :: Calculate fraction of day
  82. SET /A dDay = 24 * 60
  83. SET /A fDay = %Mins% + 60 * %Hour%
  84.  
  85. :: Check if current year is a leap year
  86. SET LeapYear=0
  87. SET /A Test = %Year%/4
  88. SET /A Test = %Test%*4
  89. IF %Test% EQU %Year% SET LeapYear=1
  90. SET /A Test = %Year%/100
  91. SET /A Test = %Test%*100
  92. IF %Test% EQU %Year% SET LeapYear=0
  93. SET /A Test = %Year%/400
  94. SET /A Test = %Test%*400
  95. IF %Test% EQU %Year% SET LeapYear=1
  96.  
  97. :: Calculate fraction of year
  98. SET /A dYear = 365 + %LeapYear%
  99. SET fYear=%Day%
  100. IF %Month% GTR  1 SET /A fYear = %fYear% + 31
  101. IF %Month% GTR  2 SET /A fYear = %fYear% + 28 + %LeapYear%
  102. IF %Month% GTR  3 SET /A fYear = %fYear% + 31
  103. IF %Month% GTR  4 SET /A fYear = %fYear% + 30
  104. IF %Month% GTR  5 SET /A fYear = %fYear% + 31
  105. IF %Month% GTR  6 SET /A fYear = %fYear% + 30
  106. IF %Month% GTR  7 SET /A fYear = %fYear% + 31
  107. IF %Month% GTR  8 SET /A fYear = %fYear% + 31
  108. IF %Month% GTR  9 SET /A fYear = %fYear% + 30
  109. IF %Month% GTR 10 SET /A fYear = %fYear% + 31
  110. IF %Month% GTR 11 SET /A fYear = %fYear% + 30
  111.  
  112. :: Calculate stardate from current year and calculated fractions
  113. SET /A sY = %Year% - 2323
  114. SET /A sX = ( 1 + 2000 * %fYear% ) / ( 2 * %dYear% )
  115. SET /A sF = ( 1 + 2 * %fDay% ) / 288
  116. SET StarDate=%sY%%sX%.%sF%
  117.  
  118. :: Display the result
  119. ECHO.
  120. ECHO Today is stardate %StarDate%
  121.  
  122. ENDLOCAL
  123. :End
  124.  

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