Module: Qt::DateTimeCodec
- Defined in:
- lib/qt/date_time_codec.rb
Overview
Codec for typed QDateTime/QDate/QTime bridge payloads.
Constant Summary collapse
- DATETIME_PREFIX =
'qtdt:'- DATE_PREFIX =
'qtdate:'- TIME_PREFIX =
'qttime:'
Class Method Summary collapse
- .coerce_to_date(value) ⇒ Object
- .coerce_to_time(value) ⇒ Object
- .decode_for_signal(signal_name, payload) ⇒ Object
- .decode_qdate(value) ⇒ Object
- .decode_qdatetime(value) ⇒ Object
- .decode_qtime(value) ⇒ Object
- .encode_qdate(value) ⇒ Object
- .encode_qdatetime(value) ⇒ Object
- .encode_qtime(value) ⇒ Object
- .normalize_time_string(value) ⇒ Object
Class Method Details
.coerce_to_date(value) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/qt/date_time_codec.rb', line 84 def coerce_to_date(value) return value if value.is_a?(Date) return value.to_date if value.respond_to?(:to_date) Date.iso8601(value.to_s) rescue ArgumentError Date.new(1970, 1, 1) end |
.coerce_to_time(value) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/qt/date_time_codec.rb', line 75 def coerce_to_time(value) return value if value.is_a?(Time) return value.to_time if value.respond_to?(:to_time) Time.iso8601(value.to_s) rescue ArgumentError Time.at(0).utc end |
.decode_for_signal(signal_name, payload) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/qt/date_time_codec.rb', line 58 def decode_for_signal(signal_name, payload) return nil if payload.nil? signature = signal_name.to_s if signature.start_with?('dateTimeChanged(') return decode_qdatetime(payload) end if signature.start_with?('dateChanged(') return decode_qdate(payload) end if signature.start_with?('timeChanged(') return decode_qtime(payload) end StringCodec.from_qt_text(payload) end |
.decode_qdate(value) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/qt/date_time_codec.rb', line 33 def decode_qdate(value) raw = StringCodec.from_qt_text(value.to_s) payload = raw.start_with?(DATE_PREFIX) ? raw.delete_prefix(DATE_PREFIX) : raw Date.iso8601(payload) rescue ArgumentError Date.new(1970, 1, 1) end |
.decode_qdatetime(value) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/qt/date_time_codec.rb', line 20 def decode_qdatetime(value) raw = StringCodec.from_qt_text(value.to_s) payload = raw.start_with?(DATETIME_PREFIX) ? raw.delete_prefix(DATETIME_PREFIX) : raw Time.iso8601(payload) rescue ArgumentError Time.at(0).utc end |
.decode_qtime(value) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/qt/date_time_codec.rb', line 50 def decode_qtime(value) raw = StringCodec.from_qt_text(value.to_s) payload = raw.start_with?(TIME_PREFIX) ? raw.delete_prefix(TIME_PREFIX) : raw normalize_time_string(payload) rescue ArgumentError '00:00:00' end |
.encode_qdate(value) ⇒ Object
28 29 30 31 |
# File 'lib/qt/date_time_codec.rb', line 28 def encode_qdate(value) date = coerce_to_date(value) "#{DATE_PREFIX}#{date.strftime('%Y-%m-%d')}" end |
.encode_qdatetime(value) ⇒ Object
15 16 17 18 |
# File 'lib/qt/date_time_codec.rb', line 15 def encode_qdatetime(value) time = coerce_to_time(value) "#{DATETIME_PREFIX}#{time.iso8601(6)}" end |
.encode_qtime(value) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/qt/date_time_codec.rb', line 41 def encode_qtime(value) time_string = case value when Time then value.strftime('%H:%M:%S') else normalize_time_string(value.to_s) end "#{TIME_PREFIX}#{time_string}" end |
.normalize_time_string(value) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/qt/date_time_codec.rb', line 93 def normalize_time_string(value) raw = value.to_s.strip raise ArgumentError, 'time is empty' if raw.empty? return "#{raw}:00" if raw.match?(/\A\d{2}:\d{2}\z/) return raw if raw.match?(/\A\d{2}:\d{2}:\d{2}\z/) parsed = Time.parse(raw) parsed.strftime('%H:%M:%S') end |