scinote-web/app/models/repository_date_time_value.rb

31 lines
813 B
Ruby
Raw Normal View History

2019-11-18 22:21:57 +08:00
# frozen_string_literal: true
2019-12-06 19:50:19 +08:00
class RepositoryDateTimeValue < RepositoryDateTimeValueBase
def data_changed?(new_data)
2019-12-10 20:39:08 +08:00
new_time = Time.zone.parse(new_data)
new_time.to_i != data.to_i
end
2019-12-10 20:39:08 +08:00
def formatted
2020-03-18 16:57:57 +08:00
super(:full_with_comma)
end
def export_formatted
2020-03-18 17:35:31 +08:00
I18n.l(data, format: :full)
end
def self.new_with_payload(payload, attributes)
value = new(attributes)
value.data = Time.zone.parse(payload)
value
end
2020-01-10 16:29:20 +08:00
def self.import_from_text(text, attributes, options = {})
2020-03-18 16:53:47 +08:00
date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%') + ' %H:%M'
2020-03-25 00:45:09 +08:00
Time.zone = options.dig(:user, :settings, :time_zone) || 'UTC'
new(attributes.merge(data: Time.zone.strptime(text, date_format)))
rescue ArgumentError
nil
end
2019-11-18 22:21:57 +08:00
end