2
// TimeSpanQueryValue.cs
5
// Gabriel Burt <gburt@novell.com>
7
// Copyright (C) 2007-2008 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Collections.Generic;
31
using System.Globalization;
34
using System.Text.RegularExpressions;
42
public enum TimeFactor {
52
public class TimeSpanQueryValue : IntegerQueryValue
54
/*public static readonly Operator Equal = new Operator ("equals", "= {0}", "==", "=", ":");
55
public static readonly Operator NotEqual = new Operator ("notEqual", "!= {0}", true, "!=", "!:");
56
public static readonly Operator LessThanEqual = new Operator ("lessThanEquals", "<= {0}", "<=");
57
public static readonly Operator GreaterThanEqual = new Operator ("greaterThanEquals", ">= {0}", ">=");
58
public static readonly Operator LessThan = new Operator ("lessThan", "< {0}", "<");
59
public static readonly Operator GreaterThan = new Operator ("greaterThan", "> {0}", ">");*/
61
protected double offset = 0;
62
protected TimeFactor factor = TimeFactor.Second;
64
public override string XmlElementName {
65
get { return "timespan"; }
68
//protected static AliasedObjectSet<Operator> operators = new AliasedObjectSet<Operator> (Equal, NotEqual, LessThan, GreaterThan, LessThanEqual, GreaterThanEqual);
69
protected static AliasedObjectSet<Operator> ops = new AliasedObjectSet<Operator> (LessThan, GreaterThan, LessThanEqual, GreaterThanEqual);
70
public override AliasedObjectSet<Operator> OperatorSet {
71
get { return operators; }
74
public override object Value {
75
get { return offset; }
78
public virtual double Offset {
79
get { return offset; }
82
public TimeFactor Factor {
83
get { return factor; }
86
public double FactoredValue {
87
get { return Offset / (double) factor; }
90
// FIXME replace period in following with culture-dependent character
91
private static Regex number_regex = new Regex ("\\d+(\\.\\d+)?", RegexOptions.Compiled);
92
public override void ParseUserQuery (string input)
94
Match match = number_regex.Match (input);
95
if (match != Match.Empty && match.Groups.Count > 0) {
98
val = Convert.ToDouble (match.Groups[0].Captures[0].Value);
99
} catch (FormatException) {
100
val = Convert.ToDouble (match.Groups[0].Captures[0].Value, NumberFormatInfo.InvariantInfo);
102
foreach (TimeFactor factor in Enum.GetValues (typeof(TimeFactor))) {
103
if (input == FactorString (factor, val, true) || input == FactorString (factor, val, false)) {
104
SetUserRelativeValue (val, factor);
112
public override string ToUserQuery ()
114
return FactorString (factor, FactoredValue, true);
117
public virtual void SetUserRelativeValue (double offset, TimeFactor factor)
119
SetRelativeValue (offset, factor);
122
public void SetRelativeValue (double offset, TimeFactor factor)
124
this.factor = factor;
125
this.offset = (long) (offset * (double)factor);
129
public override void LoadString (string val)
132
SetRelativeValue (Convert.ToDouble (val), TimeFactor.Second);
139
protected void DetermineFactor ()
141
double val = Math.Abs (offset);
142
foreach (TimeFactor factor in Enum.GetValues (typeof(TimeFactor))) {
143
if (val >= (double) factor) {
144
this.factor = factor;
149
public override void ParseXml (XmlElement node)
152
LoadString (node.InnerText);
153
if (node.HasAttribute ("factor")) {
154
this.factor = (TimeFactor) Enum.Parse (typeof(TimeFactor), node.GetAttribute ("factor"));
161
public override void AppendXml (XmlElement node)
163
base.AppendXml (node);
164
node.SetAttribute ("factor", factor.ToString ());
167
public override string ToSql (Operator op)
169
return Convert.ToString (offset * 1000, System.Globalization.CultureInfo.InvariantCulture);
172
protected virtual string FactorString (TimeFactor factor, double count, bool translate)
174
string result = null;
175
int plural_count = StringUtil.DoubleToPluralInt (count);
178
case TimeFactor.Second: result = Catalog.GetPluralString ("{0} second", "{0} seconds", plural_count); break;
179
case TimeFactor.Minute: result = Catalog.GetPluralString ("{0} minute", "{0} minutes", plural_count); break;
180
case TimeFactor.Hour: result = Catalog.GetPluralString ("{0} hour", "{0} hours", plural_count); break;
181
case TimeFactor.Day: result = Catalog.GetPluralString ("{0} day", "{0} days", plural_count); break;
182
case TimeFactor.Week: result = Catalog.GetPluralString ("{0} week", "{0} weeks", plural_count); break;
183
case TimeFactor.Month: result = Catalog.GetPluralString ("{0} month", "{0} months", plural_count); break;
184
case TimeFactor.Year: result = Catalog.GetPluralString ("{0} year", "{0} years", plural_count); break;
185
default: return null;
189
case TimeFactor.Second: result = plural_count == 1 ? "{0} second" : "{0} seconds"; break;
190
case TimeFactor.Minute: result = plural_count == 1 ? "{0} minute" : "{0} minutes"; break;
191
case TimeFactor.Hour: result = plural_count == 1 ? "{0} hour" : "{0} hours"; break;
192
case TimeFactor.Day: result = plural_count == 1 ? "{0} day" : "{0} days"; break;
193
case TimeFactor.Week: result = plural_count == 1 ? "{0} week" : "{0} weeks"; break;
194
case TimeFactor.Month: result = plural_count == 1 ? "{0} month" : "{0} months"; break;
195
case TimeFactor.Year: result = plural_count == 1 ? "{0} year" : "{0} years"; break;
196
default: return null;
200
return String.Format (result, StringUtil.DoubleToTenthsPrecision (
201
count, false, translate ? NumberFormatInfo.CurrentInfo : NumberFormatInfo.InvariantInfo));
b'\\ No newline at end of file'