Last active
September 5, 2021 14:16
-
-
Save ohno/939a35cec51081d21da116f99066a7a6 to your computer and use it in GitHub Desktop.
Dates.jl Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Dates.jl\n", | |
"\n", | |
"Juliaで日時の名前を付けたフォルダを作る方法を簡単にまとめる.\n", | |
"\n", | |
"© 2021 Shuhei Ohno\n", | |
"<br>Source: https://gist.github.com/ohno\n", | |
"<br>License: https://opensource.org/licenses/MIT" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Julia Version 1.6.2\n", | |
"Commit 1b93d53fc4 (2021-07-14 15:36 UTC)\n", | |
"Platform Info:\n", | |
" OS: Windows (x86_64-w64-mingw32)\n", | |
" CPU: Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz\n", | |
" WORD_SIZE: 64\n", | |
" LIBM: libopenlibm\n", | |
" LLVM: libLLVM-11.0.1 (ORCJIT, haswell)\n" | |
] | |
} | |
], | |
"source": [ | |
"versioninfo()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## フォルダを作る\n", | |
"\n", | |
"詳細は[こちら](https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.mkdir)を参照. フォルダは以下のように作れる. なお, 既に存在しているとエラーが出る." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"test\"" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mkdir(\"test\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"フォルダが存在するかは`ispath()`で判定できる." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ispath(\"test\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ispath(\"text\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"つまり, 以下のように判定してからフォルダを作ればエラーは出ない. 今回は時刻を名前にする予定なので, この判定はしなくてよい." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"if !ispath(\"test\")\n", | |
" mkdir(\"test\")\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 日時を取得する\n", | |
"\n", | |
"計算の履歴は全て残っている方がよい場合もある. \n", | |
"\n", | |
"\n", | |
"日時の情報を取得してyyyymmddhhmmssのように名前を付けてやればそうそう被らない. 日時の扱いに関しては[こちら](https://docs.julialang.org/en/v1/stdlib/Dates/)に説明がある. まず, `using Dates`を宣言し, `now()`で日時が取得できる." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"using Dates" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2021-09-03T11:58:30.955" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# using Dates\n", | |
"now()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"フォーマットは`Dates.format()`で指定できる. 使い方は[こちら](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.format-Tuple{TimeType,%20AbstractString})." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"20210903115831\"" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# using Dates\n", | |
"Dates.format(now(),\"yyyymmddHHMMSS\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"ミリセカンドまで入れたい場合は" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"20210903115831632\"" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# using Dates\n", | |
"Dates.format(now(),\"yyyymmddHHMMSSsss\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## まとめ\n", | |
"\n", | |
"yyyymmddHHMMSS形式の時刻の名前でフォルダを作る場合は以下のようにすればよい." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"20210903115831\"" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# using Dates\n", | |
"time = Dates.format(now(),\"yyyymmddHHMMSS\")\n", | |
"mkdir(time)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 1.6.2", | |
"language": "julia", | |
"name": "julia-1.6" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "1.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment