Rob
Rob

Reputation: 121

Store more than 24 hours in a DateTime

I work in a bizarre and irrational industry where we need to be able to represent the time of day as 06:00:00 to 30:00:00 instead of 0:00:00 to 24:00:00. Is there any way to do this using the DateTime type? If I try to construct a date time with an hour value greater than 24 it throws an exception.

Upvotes: 12

Views: 4610

Answers (7)

HERO MO
HERO MO

Reputation: 1

I for calculate Employ work hours use this function:

     public string SumHours(string TimeIn, string TimeOut)
    {
        var parts = TimeIn.Split(':');
        var hours = Int32.Parse(parts[0]);
        var minutes = Int32.Parse(parts[1]);
        var result = new TimeSpan(hours, minutes, 0);
        TimeIn = result.ToString();

        TimeSpan Hour1 = TimeSpan.Parse(TimeIn);
        TimeSpan Hour2 = TimeSpan.Parse(TimeOut);
        Hour1 = Hour1.Add(Hour2);

        string HourtoStr = string.Format("{0:D2}:{1:D2}:{2:D2}", (Hour1.Days * 24 + Hour1.Hours), Hour1.Minutes, Hour1.Seconds);
        return HourtoStr;
    }

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838086

I think this should be a presentation issue only.

Allow your users to input data in this weird format, and immediately convert it to UTC. Do all calculations on the UTC times. Then create a ToString method to convert the results back into your weird format. You will probably also need some other utility methods and properties such as an implementation of WeirdDateTime.Day.

You could write a wrapper class around a DateTime and have all the conversion and utility methods you need on that class. I've had a go at starting it - by implementing parsing from a string in weird format. This isn't production code ready by any means, but perhaps it can give you a few ideas of how you could approach this:

class WeirdDateTime
{
    public DateTime DateTime { get; set; }

    public WeirdDateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
    {
        if (hour < 6 || hour >= 30)
            throw new ArgumentException("Not a valid WeirdDateTime", "hour");

        bool addDay;
        if (hour >= 24)
        {
            addDay = true;
            hour -= 24;
        }
        else
        {
            addDay = false;
        }

        DateTime dateTime = new DateTime(year, month, day, hour, minute, second, kind);
        if (addDay)
            dateTime = dateTime.AddDays(1);

        DateTime = dateTime;
    }

    public static WeirdDateTime Parse(string s)
    {
        Regex regex = new Regex(@"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})");
        Match match = regex.Match(s);
        if (!match.Success)
            throw new FormatException("Not a valid WeirdDateTime");

        int[] parts = match.Groups.Cast<Group>()
            .Skip(1)
            .Select(x => int.Parse(x.Value))
            .ToArray();

        int year = parts[0];
        int month = parts[1];
        int day = parts[2];
        int hour = parts[3];
        int minute = parts[4];
        int second = parts[5];

        return new WeirdDateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
    }

    public override string ToString()
    {
        throw new NotImplementedException("Write this!");
    }
}

class Program
{
    public static void Main()
    {
        WeirdDateTime weirdDateTime = WeirdDateTime.Parse("2010-01-19 27:00:00");
        DateTime dateTimeUtc = weirdDateTime.DateTime.ToUniversalTime();
        Console.WriteLine(dateTimeUtc);
    }
}

Upvotes: 10

Gabe
Gabe

Reputation: 50493

How about use a TimeSpan instead ?

DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;  
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);  

Then use the TotalHours property of the TimeSpan obj

Upvotes: 3

Jonathan Allen
Jonathan Allen

Reputation: 70307

  1. You should be using TimeSpan, not DateTime.
  2. The format options for TimeSpan is

    a: [days].[hours]:[minutes]:[seconds].[fractional seconds]

    b: [days].[hours]:[minutes]:[seconds]

    c: [days].[hours]:[minutes]

    d: [days].[hours]

    e: [days]

    f: [hours]:[minutes]:[seconds].[fractional seconds]

    g: [hours]:[minutes]:[seconds]

    h: [hours]:[minutes]

Upvotes: 1

James Curran
James Curran

Reputation: 103495

how 'bout using a normal DateTime to store the actual time, and writing a new class which stores (or derives from ) a DateTime, and has a ToString() which adjusts the output.

Upvotes: 0

Ian Jacobs
Ian Jacobs

Reputation: 5501

Just have your business logic store/return DateTime.Hours.Add(6). You'll have to be aware of this in your display logic.

Upvotes: 0

Chris Thornton
Chris Thornton

Reputation: 15817

I doubt you can do exactly what you're looking for, but I expect that you could make your own DateTime class that simply adds +6 hrs to the value. i.e. stores 00 - 24 internally, but the get/set methods make it seem like 06 - 30.

Upvotes: 2

Related Questions