简体   繁体   English

在DateTime中存储超过24小时

[英]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. 我在一个奇怪且非理性的行业工作,我们需要能够将时间表示为06:00:00到30:00:00而不是0:00:00到24:00:00。 Is there any way to do this using the DateTime type? 有没有办法使用DateTime类型? If I try to construct a date time with an hour value greater than 24 it throws an exception. 如果我尝试构造一个小于24的小时值的日期时间,则会抛出异常。

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. 允许您的用户以这种奇怪的格式输入数据,并立即将其转换为UTC。 Do all calculations on the UTC times. 对UTC时间进行所有计算。 Then create a ToString method to convert the results back into your weird format. 然后创建一个ToString方法将结果转换回您的奇怪格式。 You will probably also need some other utility methods and properties such as an implementation of WeirdDateTime.Day . 您可能还需要一些其他实用程序方法和属性,例如WeirdDateTime.Day的实现。

You could write a wrapper class around a DateTime and have all the conversion and utility methods you need on that class. 您可以围绕DateTime编写一个包装类,并拥有该类所需的所有转换和实用程序方法。 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);
    }
}

How about use a TimeSpan instead ? 如何使用TimeSpan呢?

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 然后使用TimeSpan obj的TotalHours属性

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. 我怀疑你能做到你正在寻找的东西,但我希望你可以制作你自己的DateTime类,只需在该值上加上+6小时。 ie stores 00 - 24 internally, but the get/set methods make it seem like 06 - 30. 即内部存储00 - 24,但get / set方法使其看起来像06 - 30。

Just have your business logic store/return DateTime.Hours.Add(6). 只需让您的业务逻辑存储/返回DateTime.Hours.Add(6)。 You'll have to be aware of this in your display logic. 您必须在显示逻辑中注意这一点。

如何使用普通的DateTime存储实际时间,并编写一个存储(或派生自)DateTime的新类,并具有调整输出的ToString()。

  1. You should be using TimeSpan, not DateTime. 您应该使用TimeSpan,而不是DateTime。
  2. The format options for TimeSpan is TimeSpan的格式选项是

    a: [days].[hours]:[minutes]:[seconds].[fractional seconds] a:[天]。[小时]:[分钟]:[秒]。[小秒]

    b: [days].[hours]:[minutes]:[seconds] b:[天]。[小时]:[分钟]:[秒]

    c: [days].[hours]:[minutes] c:[天]。[小时]:[分钟]

    d: [days].[hours] d:[天]。[小时]

    e: [days] e:[天]

    f: [hours]:[minutes]:[seconds].[fractional seconds] f:[小时]:[分钟]:[秒]。[小秒]

    g: [hours]:[minutes]:[seconds] g:[小时]:[分钟]:[秒]

    h: [hours]:[minutes] h:[小时]:[分钟]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM