Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active August 29, 2015 14:02

Revisions

  1. aetos382 revised this gist Jun 13, 2014. No changes.
  2. aetos382 revised this gist Jun 13, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -50,13 +50,13 @@ for /f "tokens=1,2,3 delims=/" %%i in ("%today%") do (
    )

    rem 日が 1 より大きければ、日を 1 減らして終了
    if %day% gtr 1 (
    if %day% GTR 1 (
    set /a day -= 1
    goto :RESULT
    )

    rem 月が 1 より大きければ、月を 1 減らして末日をセットして終了
    if %month% gtr 1 (
    if %month% GTR 1 (
    set /a month -= 1
    call :GET_LAST
    goto :RESULT
  3. aetos382 revised this gist Jun 13, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -26,6 +26,10 @@ rem 400 で割り切れる年は閏年
    call :GET_YESTERDAY "2000/03/01"
    if "%result%" == "2000/02/29" (echo OK) else (echo NG)

    rem 月日は 1 桁で指定しても大丈夫
    call :GET_YESTERDAY "2014/6/9"
    if "%result%" == "2014/06/08" (echo OK) else (echo NG)

    exit /b

    :GET_YESTERDAY
  4. aetos382 revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ rem 4 で割り切れて 100 で割り切れない年は閏年
    call :GET_YESTERDAY "2012/03/01"
    if "%result%" == "2012/02/29" (echo OK) else (echo NG)

    rem 100 で割り切れる年は閏年じゃない
    rem 100 で割り切れて 400 で割り切れない年は閏年じゃない
    call :GET_YESTERDAY "2100/03/01"
    if "%result%" == "2100/02/28" (echo OK) else (echo NG)

  5. aetos382 revised this gist Jun 13, 2014. 1 changed file with 45 additions and 8 deletions.
    53 changes: 45 additions & 8 deletions yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,42 @@
    @echo on
    @echo off

    set today=%date%
    rem テスト

    rem コメントアウトを外して、好きな日付に設定してテストしてね
    rem set today=2000/03/01
    rem 何でもない日
    call :GET_YESTERDAY "2014/06/13"
    if "%result%" == "2014/06/12" (echo OK) else (echo NG)

    rem 1 月以外の月初日
    call :GET_YESTERDAY "2014/06/01"
    if "%result%" == "2014/05/31" (echo OK) else (echo NG)

    rem 1 月 1 日
    call :GET_YESTERDAY "2014/01/01"
    if "%result%" == "2013/12/31" (echo OK) else (echo NG)

    rem 4 で割り切れて 100 で割り切れない年は閏年
    call :GET_YESTERDAY "2012/03/01"
    if "%result%" == "2012/02/29" (echo OK) else (echo NG)

    rem 100 で割り切れる年は閏年じゃない
    call :GET_YESTERDAY "2100/03/01"
    if "%result%" == "2100/02/28" (echo OK) else (echo NG)

    rem 400 で割り切れる年は閏年
    call :GET_YESTERDAY "2000/03/01"
    if "%result%" == "2000/02/29" (echo OK) else (echo NG)

    exit /b

    :GET_YESTERDAY
    rem 指定日の前日を求める関数

    setlocal

    set today=%~1

    rem デフォルト値は今日
    if "%today%" == "" set today=%date%

    rem / 区切りで分割する
    for /f "tokens=1,2,3 delims=/" %%i in ("%today%") do (
    @@ -36,14 +69,16 @@ rem 月と日が 1 桁ならば前に 0 を補う
    if "%month:~1,1%" == "" set month=0%month%
    if "%day:~1,1%" == "" set day=0%day%

    rem 結果表示
    echo YESTERDAY = %year%/%month%/%day%
    rem 結果をセット
    endlocal & set result=%year%/%month%/%day%

    exit /b

    :GET_LAST

    rem 末日を求める関数

    setlocal

    for /f "tokens=%month% delims=," %%i in ("31,28,31,30,31,30,31,31,30,31,30,31") do set day=%%i

    rem 2 月でなければ閏年の計算はしない
    @@ -57,11 +92,13 @@ rem 4 で割り切れない年は閏年ではない
    if %remainder1% NEQ 0 GOTO :GET_LAST_EXIT

    rem 100 で割り切れる年は、400 で割り切れないならば閏年ではない
    if %remainder2% EQU 0 if %remainder3% NEQ 0 GOTO :GET_LAST_EXIT
    if %remainder2% EQU 0 (if %remainder3% NEQ 0 GOTO :GET_LAST_EXIT)

    rem 日付に 1 を足す
    set /a day += 1

    :GET_LAST_EXIT

    endlocal & set day=%day%

    exit /b
  6. aetos382 revised this gist Jun 13, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -46,13 +46,13 @@ exit /b
    rem 末日を求める関数
    for /f "tokens=%month% delims=," %%i in ("31,28,31,30,31,30,31,31,30,31,30,31") do set day=%%i

    rem 2 月でなければ閏年の計算はしない
    if %month% NEQ 2 GOTO :GET_LAST_EXIT

    set /a remainder1=%year% %% 4
    set /a remainder2=%year% %% 100
    set /a remainder3=%year% %% 400

    rem 2 月でなければ閏年の計算はしない
    if %month% NEQ 2 GOTO :GET_LAST_EXIT

    rem 4 で割り切れない年は閏年ではない
    if %remainder1% NEQ 0 GOTO :GET_LAST_EXIT

  7. aetos382 revised this gist Jun 13, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -32,10 +32,12 @@ call :GET_LAST

    :RESULT

    rem 月と日が 1 桁ならば前に 0 を補う
    if "%month:~1,1%" == "" set month=0%month%
    if "%day:~1,1%" == "" set day=0%day%

    echo %year%/%month%/%day%
    rem 結果表示
    echo YESTERDAY = %year%/%month%/%day%

    exit /b

  8. aetos382 created this gist Jun 13, 2014.
    65 changes: 65 additions & 0 deletions yeasterday.cmd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    @echo on

    set today=%date%

    rem コメントアウトを外して、好きな日付に設定してテストしてね
    rem set today=2000/03/01

    rem / 区切りで分割する
    for /f "tokens=1,2,3 delims=/" %%i in ("%today%") do (
    set year=%%i
    set month=%%j
    set day=%%k
    )

    rem 日が 1 より大きければ、日を 1 減らして終了
    if %day% gtr 1 (
    set /a day -= 1
    goto :RESULT
    )

    rem 月が 1 より大きければ、月を 1 減らして末日をセットして終了
    if %month% gtr 1 (
    set /a month -= 1
    call :GET_LAST
    goto :RESULT
    )

    rem 年を 1 減らして末日を求める
    set /a year -= 1
    set month=12
    call :GET_LAST

    :RESULT

    if "%month:~1,1%" == "" set month=0%month%
    if "%day:~1,1%" == "" set day=0%day%

    echo %year%/%month%/%day%

    exit /b

    :GET_LAST

    rem 末日を求める関数
    for /f "tokens=%month% delims=," %%i in ("31,28,31,30,31,30,31,31,30,31,30,31") do set day=%%i

    set /a remainder1=%year% %% 4
    set /a remainder2=%year% %% 100
    set /a remainder3=%year% %% 400

    rem 2 月でなければ閏年の計算はしない
    if %month% NEQ 2 GOTO :GET_LAST_EXIT

    rem 4 で割り切れない年は閏年ではない
    if %remainder1% NEQ 0 GOTO :GET_LAST_EXIT

    rem 100 で割り切れる年は、400 で割り切れないならば閏年ではない
    if %remainder2% EQU 0 if %remainder3% NEQ 0 GOTO :GET_LAST_EXIT

    rem 日付に 1 を足す
    set /a day += 1

    :GET_LAST_EXIT

    exit /b