-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathdate_time_formatter.rb
More file actions
126 lines (106 loc) · 4.74 KB
/
Copy pathdate_time_formatter.rb
File metadata and controls
126 lines (106 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# 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 COPYRIGHT and LICENSE files for more details.
#++
module API
module V3
module Utilities
module DateTimeFormatter
module_function
def format_date(date, allow_nil: false)
return nil if date.nil? && allow_nil
date.to_date.iso8601
end
def 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,
I18n.t("api_v3.errors.expected.date"),
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,
I18n.t("api_v3.errors.expected.date"),
value)
end
date_only
end
def parse_datetime(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,
I18n.t("api_v3.errors.expected.datetime"),
value)
end
date_and_time
end
def format_datetime(datetime, allow_nil: false)
return nil if datetime.nil? && allow_nil
datetime.to_datetime.utc.iso8601(3)
end
def format_duration_from_hours(hours, allow_nil: false)
return nil if hours.nil? && allow_nil
# Duration.new truncates its :seconds argument via Float#to_i
# (ruby-duration gem) rather than rounding -- round explicitly
# here first so e.g. 3600.9 seconds (1h 0.9s) serializes as
# PT1H1S, not silently dropped to PT1H.
Duration.new(seconds: (hours * 3600).round).iso8601
end
def 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,
I18n.t("api_v3.errors.expected.duration"),
duration)
end
end
def format_duration_from_days(days, allow_nil: false)
return nil if days.nil? && allow_nil
# Duration.new truncates its :seconds argument via Float#to_i
# (ruby-duration gem) rather than rounding -- round explicitly
# here first, same as format_duration_from_hours above, so a
# fractional-day value doesn't silently lose up to ~1s of precision.
Duration.new(seconds: (days * 3600 * 24).round).iso8601
end
def parse_duration_to_days(duration, property_name, allow_nil: false)
return nil if duration.nil? && allow_nil
parse_duration_to_hours(duration, property_name).to_i / 24
end
end
end
end
end