forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime_range_presenter.rb
More file actions
118 lines (100 loc) · 3.24 KB
/
Copy pathdatetime_range_presenter.rb
File metadata and controls
118 lines (100 loc) · 3.24 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
# encoding: UTF-8
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
module Utils
class DatetimeRangePresenter
attr_reader :start, :zone
def initialize(datetime, end_datetime = nil, datetime_type=:event, zone=nil)
zone ||= ::Time.zone
@start = datetime.in_time_zone(zone) rescue datetime
@_finish = end_datetime.in_time_zone(zone) rescue end_datetime
@_datetime_type = datetime_type
@zone = zone
end
def as_string(options={})
return nil unless start
shorten_midnight = options.fetch(:shorten_midnight, false)
if is_not_range?
if shorten_midnight && should_display_as_date?
start_date_string
else
datetime_component(start_date_string, start)
end
else
present_range
end
end
private
def present_range
if start.to_date == finish.to_date
I18n.t('time.ranges.same_day', "%{date} from %{start_time} to %{end_time}",
date: start_date_string, start_time: start_as_time, end_time: finish_as_time)
else
start_string = datetime_component(start_date_string, start)
end_string = datetime_component(end_date_string, finish)
I18n.t('time.ranges.different_days', "%{start_date_and_time} to %{end_date_and_time}",
start_date_and_time: start_string, end_date_and_time: end_string)
end
end
def should_display_as_date?
(datetime_type == :due_date && start.hour == 23 && start.min == 59) ||
(datetime_type == :event && start.hour == 0 && start.min == 0)
end
def is_not_range?
!finish || finish == start
end
def start_as_time
present_time(start)
end
def finish_as_time
present_time(finish)
end
def datetime_component(date_string, time)
time_string = present_time(time)
if datetime_type == :due_date
I18n.t('time.due_date', "%{date} by %{time}", date: date_string, time: time_string)
else
I18n.t('time.event', "%{date} at %{time}", date: date_string, time: time_string)
end
end
def start_date_string
present_date(start)
end
def end_date_string
present_date(finish)
end
def present_time(time)
TimePresenter.new(time, zone).as_string
end
def present_date(date)
Utils::DatePresenter.new(date.to_date, zone).as_string(date_style)
end
def finish
return nil if datetime_type == :due_date || !valid_raw_type?
@_finish
end
def date_style
datetime_type == :verbose ? :long : :no_words
end
def datetime_type
return @_datetime_type if valid_raw_type?
:event
end
def valid_raw_type?
@_datetime_type.is_a?(Symbol)
end
end
end