Class: SharedTools::Tools::CurrentDateTimeTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/current_date_time_tool.rb

Overview

Returns the current date, time, and timezone from the local system.

Examples:

tool = SharedTools::Tools::CurrentDateTimeTool.new
tool.execute                    # full output
tool.execute(format: 'date')    # date fields only

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ CurrentDateTimeTool

Returns a new instance of CurrentDateTimeTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



34
35
36
# File 'lib/shared_tools/tools/current_date_time_tool.rb', line 34

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



15
# File 'lib/shared_tools/tools/current_date_time_tool.rb', line 15

def self.name = 'current_date_time_tool'

Instance Method Details

#execute(format: 'full') ⇒ Hash

Returns date/time information.

Parameters:

  • format (String) (defaults to: 'full')

    output format

Returns:

  • (Hash)

    date/time information



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shared_tools/tools/current_date_time_tool.rb', line 40

def execute(format: 'full')
  @logger.info("CurrentDateTimeTool#execute format=#{format}")

  now = Time.now
  utc = now.utc

  date_info = {
    year:            now.year,
    month:           now.month,
    day:             now.day,
    day_of_week:     now.strftime('%A'),
    day_of_week_num: now.wday,
    week_of_year:    now.strftime('%U').to_i,
    quarter:         ((now.month - 1) / 3) + 1,
    ordinal_day:     now.yday
  }

  time_info = {
    hour:             now.hour,
    minute:           now.min,
    second:           now.sec,
    timezone:         now.zone,
    utc_offset:       now.strftime('%z'),
    utc_offset_hours: (now.utc_offset / 3600.0).round(2)
  }

  iso_info = {
    iso8601:       now.iso8601,
    iso8601_utc:   utc.iso8601,
    unix_timestamp: now.to_i
  }

  case format.to_s.downcase
  when 'date'
    { success: true }.merge(date_info)
  when 'time'
    { success: true }.merge(time_info)
  when 'iso8601'
    { success: true }.merge(iso_info)
  else
    { success: true, dst: now.dst? }
      .merge(date_info)
      .merge(time_info)
      .merge(iso_info)
  end
end