質問するログイン新規登録

質問編集履歴

1

現在行っているコードを追記しました

2017/09/06 06:03

投稿

lyzmfeqpxs54
lyzmfeqpxs54

スコア237

title CHANGED
File without changes
body CHANGED
@@ -18,4 +18,44 @@
18
18
 
19
19
  # このような形でテキストに保存するのが間違い?
20
20
 
21
+ ```
22
+
23
+ ```Ruby
24
+
25
+ open(File.expand_path("/home/rails/sample/lasttime.txt"),"w"){|f| f.write DateTime.now.strftime("%F %T") }
26
+ # ↑で一度lasttime.txtに書き込み後下のrbファイルを実行
27
+
28
+ # regularly.rb
29
+
30
+ require 'date'
31
+ require 'active_support/all'
32
+
33
+ p DateTime.now.strftime('%F %T')
34
+ p DateTime.parse(open(File.expand_path("/home/rails/sample/lasttime.txt"),&:read))
35
+ p DateTime.now - DateTime.parse(open(File.expand_path("/home/rails/sample/lasttime.txt"),&:read))
36
+ p DateTime.parse(open(File.expand_path("/home/rails/sample/lasttime.txt"),&:read)) - DateTime.now
37
+
38
+
39
+ if DateTime.now - DateTime.parse(open(File.expand_path("/home/rails/sample/lasttime.txt"),&:read)) >= 1/1440r
40
+ p "1分以上経った"
41
+
42
+ else
43
+
44
+ p "1分以上経ってない"
45
+
46
+ end
47
+
48
+
49
+
50
+ # 実行結果
51
+ "2017-09-06 14:55:58"
52
+ Wed, 06 Sep 2017 13:48:17 +0000
53
+ (-28338402251309/86400000000000)
54
+ (2833840202837/8640000000000)
55
+ "1分以上経ってない"
56
+
57
+ 計算値が常に0.33付近になってしまう。
58
+ またotn様にご教示いただいた計算順序だとマイナスがついてしまう。
59
+
60
+
21
61
  ```