Method: LinuxStat::Kernel.build_date

Defined in:
lib/linux_stat/kernel.rb

.build_dateObject

Returns the time when the kernel was compiled.

The return value is a Time object.

If the information isn’t available, it will return nil

The time will be searched in specific order.

  • It will match any date matching any of these formats:

  1. %b %d %H:%M:%S %z %Y

  2. %d %b %Y %H:%M:%S %z

  3. %Y-%m-%d

Most kernels have date in them in this format.

Do note that Ruby sometimes fails to work with timezones like BST for example.

In such case, the timezone is unrealiable and often returns the local timezone.

You have to use regexp yourself to get the proper zone.

Use LinuxStat::Kernel.build_date_string to get the original string if you need that.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/linux_stat/kernel.rb', line 86

def build_date
	return nil if splitted.empty?

	@@time ||= begin
		require 'time' unless Time.respond_to?(:strptime)

		splitted.each_cons(5).map do |x|
			joined = x.each(&:strip!).join(?\s.freeze)

			# Match 21 Oct 2020 01:11:20 +0000
			if joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/]
				Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil

			# Match Aug 25 17:23:54 UTC 2020
			elsif joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{2}\s\w+\s\d*$/]
				Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil

			# Match 2017-09-19
			elsif joined[/\d{4}-\d{1,2}-\d{1,2}/]
				Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil

			else
				nil
			end
		end.tap(&:compact!)[0]
	end
end