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 1 year has passed since last update.

RubyのTime

Last updated at Posted at 2023-03-30

症状

CentOSなんですが,Rubyで出力している時間がUTSっぽいので,JSTへの変更をどうやるかがわからなくて,その探索の結果です.

環境

> ruby -v  
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
> head -1 /etc/*elease  
==> /etc/system-release <==
CentOS Linux release 7.9.2009 (Core)

解決策

shell でTZをAsia/Tokyoとする.

Time

irbで

irb(main):005:0> require 'time'
=> true
irb(main):006:0> p Time.now
2023-03-30 03:11:20.468759837 +0000
=> 2023-03-30 03:11:20.468759837 +0000                                                           
irb(main):007:0> printenv TZ
(irb):7:in `<main>': uninitialized constant TZ (NameError)
        from /home/linuxbrew/.linuxbrew/Cellar/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /home/linuxbrew/.linuxbrew/bin/irb:25:in `load'                                     
        from /home/linuxbrew/.linuxbrew/bin/irb:25:in `<main>'                                   
irb(main):008:0> p ENV['TZ']
nil
=> nil                                                                                           
irb(main):009:0> Time.now.zone
=> "UTC"

一方で,fish上だと,

> date
2023年  3月 30日 木曜日 12:16:54 JST

なんで,どっかで設定?

fishでset

echo $TZ

でnilなんで,

set -Ux TZ JST

とするとirbで

irb(main):004:0> p ENV['TZ']
"JST"
=> "JST"                                                              
irb(main):005:0> Time.now.zone
=> "JST"
irb(main):006:0> Time.now
=> 2023-03-30 03:32:14.575439993 +0000

なんですが,Ruby3.2リファレンスでは,

現在時刻の Time オブジェクトを生成して返します。タイムゾーンは地方時となります。

と明示されていますが...さて.

timedatectlが怪しげ

bob@asura0 ~> timedatectl
Warning: Ignoring the TZ variable. Reading the system's time zone setting only.
      Local time: 木 2023-03-30 12:48:06 JST
  Universal time: 木 2023-03-30 03:48:06 UTC
        RTC time: 木 2023-03-30 12:48:10
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

としてみて,なんかJSTと打ってもダメなような気がしてきて...

irb(main):001:0> require 'time'
=> true
irb(main):002:0> Time.now
=> 2023-03-30 03:58:11.813969695 +0000
irb(main):003:0> p ENV['TZ']
"JST"
=> "JST"                                                              
irb(main):004:0> p ENV['TZ'] = "Asia/Tokyo"
"Asia/Tokyo"
=> "Asia/Tokyo"                                                       
irb(main):005:0> Time.now
=> 2023-03-30 12:58:33.612487483 +0900

として無事行けました.

最終

なんで,fishの設定(~/.config/fish/config.fish内)を

set -Ux TZ Asia/Tokyo

として

irb(main):001:0> require 'time'
=> true
irb(main):002:0> Time.now
=> 2023-03-30 12:59:49.726413086 +0900

となります.やれやれ.

queueシステムの方は,もうすでに動いているRubyコードなんで変わりませんが,次回起動する計算から正常に出力されるはず.Rubyのversionをあげるまでは普通に動いていたので,多分,CentOSとの相性だと思いますが,Ruby3.2のバグなら影響大きいかも.

追記メモ

いくつかのサイトでTime.currentを使えというのがありますが,あれは,RailsのTimeWithZoneかなんかのoverwriteみたい(?)で,私の環境では使えませんでした.


  • source ~/git_hub/grad_research_23/qiita_items/ruby_time.org
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?