2009-08-27 92 views
3

我只是想知道..只是日期或時間

我已經和反對這樣

public class Entry{ 

public DateTime? Date { get; set;} // This is just Date 

public DateTime? StartTime { get; set; } //This is just Time 

public TimeSpan Duration { get; set; } //Time spent on entry 

} 

有沒有更合適的類型比日期時間或者更好的策略來處理只是時間和日期?沒有必須添加DateTime.MinDate()到我所有的開始和結束時間的痛苦?

---更新---

1 - 我喜歡能夠請求,如果日期或開始時間是空seperartly條目對象。

2 - 輸入應允許用戶輸入持續時間而不指示日期。即使像DateTime.MinDate()這樣的默認日期看起來也是一種糟糕的設計。 (這就是爲什麼我選擇TimeSpan不是開始和結束時間)

+3

當你的EndTime在第二天會發生什麼? – ChrisF 2009-08-27 13:12:52

+0

你說得對Chris不好。在上面的例子中,我打出了endtime,只有Date,StartTime和Duration。好點 – BBorg 2009-08-27 13:19:59

+1

@Borg:在這種情況下,爲什麼不把'Date'屬性完全拋棄,並且'StartTime'表示日期*和*時間。畢竟,這就是'DateTime'的用途! – LukeH 2009-08-27 13:21:38

回答

6

不要拆分存儲數據的日期和時間組件。您可以提供的屬性,如果你想提取那些:

public class Entry { 

    public DateTime StartPoint { get; set; } 
    public TimeSpan Duration { get; set; } 

    public DateTime StartDate { get { return StartPoint.Date; } } 
    public TimeSpan StartTime { get { return StartPoint.TimeOfDay; } } 
    public DateTime EndPoint { get { return StartPoint + Duration; } } 
    public DateTime EndDate { get { return EndPoint.Date; } } 
    public TimeSpan EndTime { get { return EndPoint.TimeOfDay; } } 

} 

更新:
如果你想擁有空值的日期和時間,你可以添加對性能,而無需拆分的日期和時間:

public class Entry{ 

    private DateTime _startPoint; 

    public bool HasStartDate { get; private set; } 
    public bool HasStartTime { get; private set; } 
    public TimeSpan Duration { get; private set; } 

    private void EnsureStartDate() { 
     if (!HasStartDate) throw new ApplicationException("Start date is null."); 
    } 

    private void EnsureStartTime() { 
     if (!HasStartTime) throw new ApplicationException("Start time is null."); 
    } 

    public DateTime StartPoint { get { 
     EnsureStartDate(); 
     EnsureStartTime(); 
     return _startPoint; 
    } } 

    public DateTime StartDate { get { 
     EnsureStartDate(); 
     return _startPoint.Date; 
    } } 

    public TimeSpan StartTime { get { 
     EnsureStartTime(); 
     return _startPoint.TimeOfDay; 
    } } 

    public DateTime EndPoint { get { return StartPoint + Duration; } } 

    public DateTime EndDate { get { return EndPoint.Date; } } 

    public TimeSpan EndTime { get { return EndPoint.TimeOfDay; } } 

    public Entry(DateTime startPoint, TimeSpan duration) 
    : this (startPoint, true, true, duration) {} 

    public Entry(TimeSpan duration) 
    : this(DateTime.MinValue, false, false, duration) {} 

    public Entry(DateTime startPoint, bool hasStartDate, bool hasStartTime, TimeSpan duration) { 
     _startPoint = startPoint; 
     HasStartDate = hasStartDate; 
     HasStartTime = hasStartTime; 
     Duration = duration; 
    } 

} 
+0

@Guffa我看不出如何輕鬆檢查日期和時間是否爲空分開? – BBorg 2009-08-27 13:46:31

+0

@Borg:正如你在我回答後添加了這個要求,我在上面添加了一個更新。 – Guffa 2009-08-27 16:45:16

+0

@Guffa ,,很好,我要測試這一點,但我認爲這將工作得很好,TY – BBorg 2009-08-28 07:50:55

6

您可以使用TimeSpan作爲您的StartTimeEndTime屬性。這就是DateTime.TimeOfDay屬性返回的結果。

還有DateTime.Date屬性返回DateTime的時間元素設置爲午夜。

說了這麼多,我可能會建議完全拋棄你Date性能和存儲全DateTime S(即日期時間)在StartTimeEndTime性能。

+1

我會建議同樣的事情。你不需要Date屬性。只需要StartTime和EndTime包含日期信息。 – 2009-08-27 13:21:00

+0

@ Meta-Knight;在這種特殊情況下,用例允許用戶只輸入Timespent而不顯示日期或時間。然後在後面的用例中添加,但並非總是如此。 – BBorg 2009-08-27 13:32:43

+0

@Luke開船的問題總而言之,我無法檢查是否設置了時間。由於00:00:00也是有效的開始時間 – BBorg 2009-08-27 14:06:25

1

你最好離開參考文獻DateTime s。如果您只存儲時間,那麼當您的Entry跨越24小時以上時,您會遇到問題。將它們存儲爲DateTime,因爲您現在擁有它們,並且只需將格式設置爲最終用戶的時間部分即可。

+0

耶chrisF也指出了我的例子中的這個弱點:-) – BBorg 2009-08-27 13:23:23

0

我想恭維Guffa's answer有兩個最佳實踐:

  1. 存儲您所有的日期在數據庫中的UTC的。
  2. 避免System.DateTime和favor System.DateTimeOffset
    1. SQL Server 2008的datetimeoffset類型相當於DateTimeOffset。
+0

@Jim G - 我很高興答案也..但我不明白我將如何輕鬆檢查日期如果我合併到兩個屬性,從時間(時間!=空)獨立設置或不(日期!=空)? – BBorg 2009-08-27 14:07:28