Powered by GeSHi

Source code for datepart.bat

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

  1. @ECHO OFF
  2. ECHO.
  3.  
  4. :: Check Windows version (XP Pro or later)
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6.  
  7. :: Check command line argument (one mandatory)
  8. IF     "%~1"=="" GOTO Syntax
  9. IF NOT "%~2"=="" GOTO Syntax
  10.  
  11. :: Check if help is required
  12. ECHO.%1 | FINDSTR.EXE /R /C:"[/?\.]" >NUL && GOTO Syntax
  13.  
  14. :: Check if WMIC is available
  15. WMIC.EXE Alias /? >NUL 2>&1 || GOTO Syntax
  16.  
  17. :: Localize variables
  18. SETLOCAL ENABLEDELAYEDEXPANSION
  19.  
  20. :: Reset/initialize the variables used
  21. SET DatePart=
  22. SET Day=
  23. SET Month=
  24. SET Year=
  25. SET Error=0
  26.  
  27. :: Fill array with valid arguments
  28. SET DatePart.d=Day
  29. SET DatePart.Day=Day
  30.  
  31. SET DatePart.h=Hour
  32. SET DatePart.Hour=Hour
  33.  
  34. SET DatePart.i=DST
  35. SET DatePart.DST=DST
  36.  
  37. SET DatePart.l=LeapYear
  38. SET DatePart.LeapYear=LeapYear
  39.  
  40. SET DatePart.m=Month
  41. SET DatePart.Month=Month
  42.  
  43. SET DatePart.n=Minute
  44. SET DatePart.Minute=Minute
  45.  
  46. SET DatePart.q=Quarter
  47. SET DatePart.Quarter=Quarter
  48.  
  49. SET DatePart.s=Second
  50. SET DatePart.Second=Second
  51.  
  52. SET DatePart.w=DayOfWeek
  53. SET DatePart.DayOfWeek=DayOfWeek
  54.  
  55. SET DatePart.ww=WeekOfYear
  56. SET DatePart.WeekOfYear=WeekOfYear
  57.  
  58. SET DatePart.y=DayOfYear
  59. SET DatePart.DayOfYear=DayOfYear
  60.  
  61. SET DatePart.yyyy=Year
  62. SET DatePart.Year=Year
  63.  
  64. :: If DST test is requested, check if PHP is available
  65. IF "!DatePart.%~1!"=="DST" (
  66. 	PHP
  67. )
  68.  
  69. :: Get current day, month, year and determine if we have a leap year
  70. CALL :Today
  71. CALL :LeapYear
  72. ECHO.!DatePart.%~1! | FIND /I "OfYear" >NUL && (
  73. 	CALL :DayOfYear
  74. 	CALL :WeekOfYear
  75. )
  76.  
  77. :: Check if command line argument is listed in array
  78. SET DatePart. | FINDSTR /R /I /C:"\.%~1=" >NUL
  79. IF ERRORLEVEL 1 (
  80. 	ENDLOCAL
  81. 	GOTO Syntax
  82. )
  83.  
  84. :: If the value is already stored in a variable use that variable, otherwise use WMIC.
  85. IF DEFINED !DatePart.%~1! (
  86. 	SET DP=!DatePart.%~1!
  87. 	CALL SET DatePart=%%!DP!%%
  88. ) ELSE (
  89. 	FOR /F "skip=1" %%A IN ('WMIC Path Win32_LocalTime Get !DatePart.%~1! /Format:table 2^>NUL ^|^| SET Error=1') DO (
  90. 		IF "!DatePart!"=="" SET DatePart=%%A
  91. 	)
  92. )
  93.  
  94. :: Display result
  95. ECHO.%DatePart%
  96.  
  97. :: Check for errors trapped by WMIC
  98. IF "%Error%"=="1" (
  99. 	ENDLOCAL
  100. 	GOTO Syntax
  101. )
  102.  
  103. :: Done
  104. ENDLOCAL & SET DatePart=%DatePart%
  105. EXIT /B %DatePart%
  106.  
  107.  
  108. :DayOfYear
  109. :: Fill array with cumulative number of days of past months
  110. SET /A DaysPast.1  = 0
  111. SET /A DaysPast.2  = %DaysPast.1%  + 31
  112. SET /A DaysPast.3  = %DaysPast.2%  + 28 + %LeapYear%
  113. SET /A DaysPast.4  = %DaysPast.3%  + 31
  114. SET /A DaysPast.5  = %DaysPast.4%  + 30
  115. SET /A DaysPast.6  = %DaysPast.5%  + 31
  116. SET /A DaysPast.7  = %DaysPast.6%  + 30
  117. SET /A DaysPast.8  = %DaysPast.7%  + 31
  118. SET /A DaysPast.9  = %DaysPast.8%  + 31
  119. SET /A DaysPast.10 = %DaysPast.9%  + 30
  120. SET /A DaysPast.11 = %DaysPast.10% + 31
  121. SET /A DaysPast.12 = %DaysPast.11% + 30
  122. SET /A DayOfYear   = !DaysPast.%Month%! + %Day%
  123. GOTO:EOF
  124.  
  125.  
  126. :LeapYear
  127. SET LeapYear=0
  128. SET /A "Test = %Year% %% 4"
  129. IF %Test% EQU 0 SET LeapYear=1
  130. SET /A "Test = %Year% %% 100"
  131. IF %Test% EQU 0 SET LeapYear=0
  132. SET /A "Test = %Year% %% 400"
  133. IF %Test% EQU 0 SET LeapYear=1
  134. GOTO:EOF
  135.  
  136.  
  137. :Today
  138. FOR /F "skip=1 tokens=1-4" %%A IN ('WMIC Path Win32_LocalTime Get Day^,DayOfWeek^,Month^,Year /Format:table 2^>NUL ^|^| SET Error=1') DO (
  139. 	IF "!Day!"==""       SET Day=%%A
  140. 	IF "!DayOfWeek!"=="" SET DayOfWeek=%%B
  141. 	IF "!Month!"==""     SET Month=%%C
  142. 	IF "!Year!"==""      SET Year=%%D
  143. )
  144. IF %DayOfWeek% EQU 0 SET DayOfWeek=7
  145. GOTO:EOF
  146.  
  147.  
  148. :WeekOfYear
  149. SET /A ThisWeeksSunday = %DayOfYear% - %DayOfWeek% + 7
  150. SET /A WeekOfYear = %ThisWeeksSunday% / 7
  151. SET /A FirstThursday = %ThisWeeksSunday% - 7 * %WeekOfYear% + 4
  152. IF %FirstThursday% GTR 7 SET /A WeekOfYear += 1
  153. GOTO:EOF
  154.  
  155.  
  156. :Syntax
  157. ECHO DatePart.bat, Version 3.20 for Windows XP Professional and later
  158. ECHO Returns the specified part of the current date or time
  159. ECHO.
  160. ECHO Usage:   DATEPART   option
  161. ECHO.
  162. ECHO Option:  d    or Day          Returns:   day of month
  163. ECHO          h    or Hour                    hour
  164. ECHO          i    or DST                     DST (1 if true, 0 if not) **
  165. ECHO          l    or LeapYear                leap year (1 if true, 0 if not)
  166. ECHO          m    or Month                   month
  167. ECHO          n    or Minute                  minutes
  168. ECHO          q    or Quarter                 quarter
  169. ECHO          s    or Second                  seconds
  170. ECHO          w    or DayOfWeek               day of week
  171. ECHO          ww   or WeekOfYear              ISO week number
  172. ECHO          y    or DayOfYear               day of year
  173. ECHO          yyyy or Year                    year
  174. ECHO.
  175. ECHO Notes:   All values are numeric, without leading zeros, and are returned
  176. ECHO          on screen, as "errorlevel" and in variable %%DatePart%%.
  177. ECHO          Week 0 means the last week (52 or 53) of the previous year.
  178. ECHO          DST check will only work if PHP is installed.
  179. ECHO.
  180. ECHO Written by Rob van der Woude
  181. ECHO http://www.robvanderwoude.com
  182.