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 | -----------------------------------------------------------------------
-- awa-converters-dates -- Date Converters
-- Copyright (C) 2012, 2013, 2014 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-----------------------------------------------------------------------
with Ada.Calendar;
with Ada.Exceptions;
with Ada.Strings.Unbounded;
with Util.Locales;
with Util.Dates.Formats;
with Util.Properties.Bundles;
with Util.Beans.Objects.Time;
with ASF.Locales;
with ASF.Utils;
with ASF.Applications.Main;
package body AWA.Converters.Dates is
ONE_HOUR : constant Duration := 3600.0;
ONE_DAY : constant Duration := 24 * ONE_HOUR;
-- ------------------------------
-- Convert the object value into a string. The object value is associated
-- with the specified component.
-- If the string cannot be converted, the Invalid_Conversion exception should be raised.
-- ------------------------------
overriding
function To_String (Convert : in Relative_Date_Converter;
Context : in ASF.Contexts.Faces.Faces_Context'Class;
Component : in ASF.Components.Base.UIComponent'Class;
Value : in Util.Beans.Objects.Object) return String is
Bundle : ASF.Locales.Bundle;
Locale : constant Util.Locales.Locale := Context.Get_Locale;
begin
begin
ASF.Applications.Main.Load_Bundle (Context.Get_Application.all,
Name => "dates",
Locale => Util.Locales.To_String (Locale),
Bundle => Bundle);
exception
when E : Util.Properties.Bundles.NO_BUNDLE =>
Component.Log_Error ("Cannot localize dates: {0}",
Ada.Exceptions.Exception_Message (E));
end;
-- Convert the value as a date here so that we can raise an Invalid_Conversion exception.
declare
use Ada.Calendar;
Date : constant Ada.Calendar.Time := Util.Beans.Objects.Time.To_Time (Value);
Now : constant Ada.Calendar.Time := Ada.Calendar.Clock;
Dt : constant Duration := Now - Date;
Values : ASF.Utils.Object_Array (1 .. 1);
Result : Ada.Strings.Unbounded.Unbounded_String;
begin
if Dt < 60.0 then
return Bundle.Get ("date_moment_ago");
elsif Dt < 120.0 then
return Bundle.Get ("date_minute_ago");
elsif Dt < ONE_HOUR then
Values (1) := Util.Beans.Objects.To_Object (Natural (Dt / 60.0));
ASF.Utils.Formats.Format (Bundle.Get ("date_minutes_ago"),
Values, Result);
elsif Dt < 2 * ONE_HOUR then
Values (1) := Util.Beans.Objects.To_Object (Natural (Dt / 60.0));
ASF.Utils.Formats.Format (Bundle.Get ("date_hour_ago"),
Values, Result);
elsif Dt < ONE_DAY then
Values (1) := Util.Beans.Objects.To_Object (Natural (Dt / ONE_HOUR));
ASF.Utils.Formats.Format (Bundle.Get ("date_hours_ago"),
Values, Result);
elsif Dt < 2 * ONE_DAY then
Values (1) := Util.Beans.Objects.To_Object (Natural (Dt / ONE_HOUR));
ASF.Utils.Formats.Format (Bundle.Get ("date_day_ago"),
Values, Result);
elsif Dt < 7 * ONE_DAY then
Values (1) := Util.Beans.Objects.To_Object (Natural (Dt / ONE_DAY));
ASF.Utils.Formats.Format (Bundle.Get ("date_days_ago"),
Values, Result);
else
declare
use ASF.Converters.Dates;
Pattern : constant String
:= Date_Converter'Class (Convert).Get_Pattern (Context, Bundle, Component);
begin
return Util.Dates.Formats.Format (Pattern, Date, Bundle);
end;
end if;
return Ada.Strings.Unbounded.To_String (Result);
end;
exception
when E : others =>
raise ASF.Converters.Invalid_Conversion with Ada.Exceptions.Exception_Message (E);
end To_String;
end AWA.Converters.Dates;
|