OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/lib/api/v3/utilities/date_time_formatter.rb

107 lines
3.7 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
module API
module V3
module Utilities
class DateTimeFormatter
def self.format_date(date, allow_nil: false)
return nil if date.nil? && allow_nil
date.to_date.iso8601
end
def self.parse_date(value, property_name, allow_nil: false)
return nil if value.nil? && allow_nil
begin
date_and_time = DateTime.iso8601(value)
rescue ArgumentError
raise API::Errors::PropertyFormatError.new(property_name, 'ISO 8601 date only', value)
end
date_only = date_and_time.to_date
# we only want to accept "timeless" dates, e.g. "2015-01-31",
# but not "2015-01-31T01:02:03".
# However Date.iso8601 is too generous and would accept that
unless date_and_time == date_only
raise API::Errors::PropertyFormatError.new(property_name, 'ISO 8601 date only', value)
end
date_only
end
def self.format_datetime(datetime, allow_nil: false)
return nil if datetime.nil? && allow_nil
datetime.to_datetime.utc.iso8601
end
def self.parse_datetime(value, property_name, allow_nil: false)
return nil if value.nil? && allow_nil
begin
return DateTime.iso8601(value).utc
rescue ArgumentError
raise API::Errors::PropertyFormatError.new(property_name,
'ISO 8601 date and time',
value)
end
end
def self.format_duration_from_hours(hours, allow_nil: false)
return nil if hours.nil? && allow_nil
Duration.new(hours_and_minutes(hours)).iso8601
end
def self.parse_duration_to_hours(duration, property_name, allow_nil: false)
return nil if duration.nil? && allow_nil
begin
iso_duration = ISO8601::Duration.new(duration.to_s)
iso_duration.to_seconds / 3600.0
rescue ISO8601::Errors::UnknownPattern
raise API::Errors::PropertyFormatError.new(property_name,
'ISO 8601 duration',
duration)
end
end
def self.hours_and_minutes(hours)
hours = hours.to_f
minutes = (hours - hours.to_i) * 60
{ hours: hours.to_i, minutes: minutes }
end
private_class_method :hours_and_minutes
end
end
end
end