Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13847 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
23using System.Collections.Generic;
24using System.Globalization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.DataImporter.Data.Model {
28  [StorableClass]
29  public class DateTimeColumn : ColumnBase {
30    [Storable]
31    private List<DateTime?> values;
32
33    [StorableConstructor]
34    protected DateTimeColumn(bool deserializing) : base(deserializing) { }
35
36    public DateTimeColumn(string columnName)
37      : base(columnName) {
38      this.values = new List<DateTime?>();
39    }
40
41    public DateTimeColumn(string columnName, int capacity)
42      : this(columnName) {
43      this.values.Capacity = capacity;
44    }
45
46    public override Type DataType {
47      get { return typeof(DateTime?); }
48    }
49
50    public override string ToString() {
51      return base.ToString() + " <Date>";
52    }
53
54    protected override System.Collections.IList Values {
55      get { return this.values; }
56    }
57
58    public override void AddValue(IComparable value) {
59      this.values.Add((DateTime?)value);
60    }
61
62    public override void AddValueOrNull(IComparable value) {
63      this.values.Add(ConvertToDateTime(value));
64    }
65
66    public override void ChangeValue(int position, IComparable value) {
67      this.values[position] = (DateTime?)value;
68    }
69
70    public override void ChangeValueOrNull(int position, IComparable value) {
71      this.values[position] = ConvertToDateTime(value);
72    }
73
74    public override void ChangeValueOrLeaveOldValue(int position, IComparable value) {
75      if (value == null)
76        this.values[position] = null;
77      else {
78        DateTime? val = ConvertToDateTime(value);
79        if (val != null)
80          this.values[position] = val;
81      }
82    }
83
84    public override void InsertValue(int position, IComparable value) {
85      this.values.Insert(position, (DateTime?)value);
86    }
87
88    private DateTime? ConvertToDateTime(IComparable value) {
89      DateTime val;
90
91      if (value == null)
92        return null;
93
94      if (DateTime.TryParse(value.ToString(), out val))
95        return val;
96
97      if (DateTime.TryParseExact(value.ToString(), "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out val))
98        return val;
99
100      return null;
101    }
102
103    public override ColumnBase CreateCopyOfColumnWithoutValues() {
104      return CreateCopyOfColumnWithoutValues(this.values.Capacity);
105    }
106
107    public override ColumnBase CreateCopyOfColumnWithoutValues(int capacity) {
108      return new DateTimeColumn(this.Name, capacity);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.