Just a date or time

I'm just curious..

I have an object like this

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

}

Is there a more suitable type than DateTime or a better strategy for handling only time and date? No pain adding DateTime.MinDate () to all my start and end times?

--- update ---

1 - I like to ask if Date or StartTime is Null in an Entry object.

2 - The record should allow the user to enter Duration without specifying a date. Even the default date, such as DateTime.MinDate (), looks like a bad design. (That's why I choose TimeSpan not Start and EndTime)

+3
source share
4 answers

, . , :

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;
   }

}
+6

TimeSpan StartTime EndTime. DateTime.TimeOfDay.

DateTime.Date, DateTime , .

, , , Date StartTime EndTime DateTime (.. ).

+6

DateTime s. , , Entry 24- . DateTime, , , .

+1
source

I would like to compliment Guffa answer with two best practices:

  • Save all your dates in UTC format in a database.
  • Avoid System.DateTime and give System.DateTimeOffset .
    • SQL Server 2008 datetimeoffset datatype is the equivalent of DateTimeOffset.
0
source

Source: https://habr.com/ru/post/1716213/


All Articles