Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/DateTimeColumn.cs @ 6451

Last change on this file since 6451 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 3.4 KB
RevLine 
[6134]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[6133]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Xml;
27using System.Globalization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.DataImporter.Data.Model {
31  [StorableClass]
32  public class DateTimeColumn : ColumnBase {
33    [Storable]
34    private List<DateTime?> values;
35
36    private DateTimeColumn() : base(string.Empty) {
37      this.DataType = typeof(DateTime?);
38    }
39
40    public DateTimeColumn(string columnName)
41      : base(columnName) {
42      this.DataType = typeof(DateTime?);
43      this.values = new List<DateTime?>();
44    }
45
46    public DateTimeColumn(string columnName, int capacity)
47      : this(columnName) {
48      this.values.Capacity = capacity;
49    }
50
51    public override string ToString() {
52      return base.ToString() + " <Date>";
53    }
54
55    protected override System.Collections.IList Values {
56      get { return this.values; }
57    }
58
59    public override void AddValue(IComparable value) {
60      this.values.Add((DateTime?)value);
61    }
62
63    public override void AddValueOrNull(IComparable value) {
64      this.values.Add(ConvertToDateTime(value));
65    }
66
67    public override void ChangeValue(int position, IComparable value) {
68      this.values[position] = (DateTime?)value;
69    }
70
71    public override void ChangeValueOrNull(int position, IComparable value) {
72      this.values[position] = ConvertToDateTime(value);
73    }
74
75    public override void ChangeValueOrLeaveOldValue(int position, IComparable value) {
76      if (value == null)
77        this.values[position] = null;
78      else {
79        DateTime? val = ConvertToDateTime(value);
80        if (val != null)
81          this.values[position] = val;
82      }
83    }
84
85    public override void InsertValue(int position, IComparable value) {
86      this.values.Insert(position, (DateTime?)value);
87    }
88
89    private DateTime? ConvertToDateTime(IComparable value) {
90      DateTime val;
91
92      if (value == null)
93        return null;
94
95      if (DateTime.TryParse(value.ToString(), out val))
96        return val;
97
98      if (DateTime.TryParseExact(value.ToString(), "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out val))
99        return val;
100
101      return null;
102    }
103
104    public override ColumnBase CreateCopyOfColumnWithoutValues() {
105      return CreateCopyOfColumnWithoutValues(this.values.Capacity);
106    }
107
108    public override ColumnBase CreateCopyOfColumnWithoutValues(int capacity) {
109      return new DateTimeColumn(this.Name, capacity);
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.