0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cshで文字列操作(日付・数値)

0
Posted at

Cシェルで数値計算の開始日と終了日を計算するのに
どの形がよいのか見当してみました。

1.awkを使用する

test1.csh
set start_date = 20150423
set end_date = 20190627

set start_year   = `echo $start_date | awk '$0 = substr($0, 1, 4)'`
set start_month  = `echo $start_date | awk '$0 = substr($0, 5, 2)'`
set start_day    = `echo $start_date | awk '$0 = substr($0, 7, 2)'`
set end_year     = `echo $end_date | awk '$0 = substr($0, 1, 4)'`
set end_month    = `echo $end_date | awk '$0 = substr($0, 5, 2)'`
set end_day      = `echo $end_date | awk '$0 = substr($0, 7, 2)'`
echo "start_date = ${start_year}/${start_month}/${start_day}"
echo "  end_date = ${end_year}/${end_month}/${end_day}"

time test1.csh

1.awk 1回目 2回目 3回目
real 0m0.056s 0m0.058s 0m0.057s
user 0m0.020s 0m0.026s 0m0.021s
sys 0m0.039s 0m0.035s 0m0.039s

2.cutを使用する

test2.csh
set start_date = 20150423
set end_date = 20190627

set start_year   = `echo $start_date | cut -c 1-4`
set start_month  = `echo $start_date | cut -c 5-6`
set start_day    = `echo $start_date | cut -c 7-8`
set end_year     = `echo $end_date | cut -c 1-4`
set end_month    = `echo $end_date | cut -c 5-6`
set end_day      = `echo $end_date | cut -c 7-8`
echo "start_date = ${start_year}/${start_month}/${start_day}"
echo "  end_date = ${end_year}/${end_month}/${end_day}"

time test2.csh

2.cut 1回目 2回目 3回目
real 0m0.053s 0m0.055s 0m0.055s
user 0m0.016s 0m0.025s 0m0.028s
sys 0m0.039s 0m0.033s 0m0.030s

3.それぞれ手入力する

test3.csh

set start_year  = 2015
set start_month =    4
set start_day   =   23
set end_year    = 2019
set end_month   =    6
set end_day     =   27
@ start_date = ${start_year} * 10000 + ${start_month} * 100 + ${start_day}
@ end_date = $end_year * 10000 + $end_month * 100 + $end_day

echo "start_date = ${start_year}/${start_month}/${start_day}"
echo "  end_date = ${end_year}/${end_month}/${end_day}"

time test3.csh

3.手入力 1回目 2回目 3回目
real 0m0.038s 0m0.039s 0m0.038s
user 0m0.014s 0m0.019s 0m0.016s
sys 0m0.026s 0m0.023s 0m0.023s

どれを採用するか

一番処理が速いのは3の手入力でした。
しかし、この先私が行う数値計算では期間の条件を変えて数多くの計算をする必要があります。
そのたびに計算開始日と終了日をわざわざ6行にわたって編集するのは面倒だと感じました。

多少処理が遅くとも、8桁のタイピングを2行で済ませられる方が楽なので、
2のcutコマンドを使う方法を採用しました。


なお、この先うるう年の判定をする予定で、
年、月、日のループの際は+1をしていかなければなりません。
今の時点で月は文字列で、$start_monthの値は"04"となっておりますが、

@  start_month ++

をしたところ、値は5になっており、きちんと加算ができておりました。
小数計算はできませんでしたが、整数の加算であれば問題なさそうです。



start_dateの日付は私の大好きな「CLOCK ZERO ~終焉の一秒~ Extime」の、
end_dateの日付は「CLOCK ZERO ~終焉の一秒~ Devote」の発売日でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?