gpt4 book ai didi

c# - 关于日期格式的问题 - 16 :15 to 16:00

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:38:45 24 4
gpt4 key购买 nike

轮类是这样的:

1- 00:00 ->08:00
2- 08:00 ->16:00
3- 16:00 ->00:00

轮类时有 worker 在做短工。

有时工作从 15:55 开始到 16:15 结束。

我必须分开工作,以 2 个类次继续进行。

例如 15:55 ->16:00 和 16:00 -> 16:15

我得到 16:15 和它的一天的值。

例如'2019-01-15 16:17:20.123'

我如何将 '2019-01-15 16:17:20.123' 转换为 '2019-01-15 16:00:00.000'

最佳答案

对于类次计算,您可以使用此代码

class ShiftInfo : IEquatable<ShiftInfo>
{
public ShiftInfo(TimeSpan startTime, string name)
{
this.StartTime = startTime;
this.Name = name;
}

public TimeSpan StartTime
{
get;
private set;
}

public string Name
{
get;
private set;
}

public bool Equals(ShiftInfo other)
{
return StartTime.Equals(other.StartTime);
}

public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}

if (obj is ShiftInfo)
{
return this.Equals((ShiftInfo)obj);
}

return false;
}

public override int GetHashCode()
{
return StartTime.GetHashCode();
}
}

class ShiftConfig : IEnumerable<ShiftInfo>
{
private readonly ICollection<ShiftInfo> _shiftInfos;
public ShiftConfig(params ShiftInfo[] shiftInfos)
{
_shiftInfos = shiftInfos.Distinct().OrderBy(e => e.StartTime).ToList();
}

public ShiftConfig(HashSet<ShiftInfo> shiftInfos)
{
_shiftInfos = shiftInfos.OrderBy(e => e.StartTime).ToList();
}

public IEnumerator<ShiftInfo> GetEnumerator()
{
return _shiftInfos.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return _shiftInfos.GetEnumerator();
}
}

class ShiftWorkItem
{
public ShiftWorkItem(ShiftInfo shift, DateTime shiftFrom, DateTime shiftUntil, DateTime workFrom, DateTime workUntil)
{
Shift = shift;
ShiftFrom = shiftFrom;
ShiftUntil = shiftUntil;
WorkFrom = workFrom;
WorkUntil = workUntil;
}

public ShiftInfo Shift
{
get;
private set;
}

public DateTime ShiftFrom
{
get;
private set;
}

public DateTime ShiftUntil
{
get;
private set;
}

public TimeSpan ShiftDuration
{
get
{
return ShiftUntil - ShiftFrom;
}
}

public DateTime WorkFrom
{
get;
private set;
}

public DateTime WorkUntil
{
get;
private set;
}

public TimeSpan WorkDuration
{
get
{
return WorkUntil - WorkFrom;
}
}
}

static class ShiftConfigExtensions
{
public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, TimeSpan duration)
{
return EnumerateShifts(config, from, from.Add(duration));
}

public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, DateTime until)
{
DateTime day = from.Date.AddDays(-1);
DateTime? shiftStart = null;
ShiftInfo lastShift = null;
while (true)
{
foreach (var shift in config)
{
var shiftEnd = day.Add(shift.StartTime);
if (shiftStart != null)
{
if ((shiftStart.Value <= from && shiftEnd >= from) || (shiftStart.Value <= until && shiftEnd >= until) || (shiftStart.Value > from && shiftEnd <= until))
{
var workFrom = shiftStart.Value < from ? from : shiftStart.Value;
var workUntil = shiftEnd > until ? until : shiftEnd;
yield return new ShiftWorkItem(lastShift, shiftStart.Value, shiftEnd, workFrom, workUntil);
}
}

if (shiftEnd >= until)
{
yield break;
}

shiftStart = shiftEnd;
lastShift = shift;
}

day = day.AddDays(1);
}
}
}

用法

public static void Main(string[] args)
{
var sc = new ShiftConfig(
new ShiftInfo(TimeSpan.FromHours(6), "early"),
new ShiftInfo(TimeSpan.FromHours(14), "late"),
new ShiftInfo(TimeSpan.FromHours(22), "night"));
Console.WriteLine(" | SHIFT | WORK ");
Console.WriteLine(" Date Shiftname | from until dur. | from until dur. ");
Console.WriteLine("======================|=========================|=========================");
foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 02, 37, 25), TimeSpan.FromHours(28.34)))
{
Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
}
}

产生

                      |          SHIFT          |          WORK              Date    Shiftname  |   from    until   dur.  |   from    until   dur.  ======================|=========================|=========================2018-12-31 night      | 22:00:00 06:00:00 8.00h | 02:37:25 06:00:00 3.38h2019-01-01 early      | 06:00:00 14:00:00 8.00h | 06:00:00 14:00:00 8.00h2019-01-01 late       | 14:00:00 22:00:00 8.00h | 14:00:00 22:00:00 8.00h2019-01-01 night      | 22:00:00 06:00:00 8.00h | 22:00:00 06:00:00 8.00h2019-01-02 early      | 06:00:00 14:00:00 8.00h | 06:00:00 06:57:49 0.96h

.net fiddle sample

or to cover your sample

public static void Main(string[] args)
{
var sc = new ShiftConfig(
new ShiftInfo(TimeSpan.FromHours(0), "night"),
new ShiftInfo(TimeSpan.FromHours(8), "early"),
new ShiftInfo(TimeSpan.FromHours(16), "late"));
Console.WriteLine(" | SHIFT | WORK ");
Console.WriteLine(" Date Shiftname | from until dur. | from until dur. ");
Console.WriteLine("======================|=========================|=========================");
foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 15, 55, 00), TimeSpan.FromMinutes(20)))
{
Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
}
}

我们得到

                      |          SHIFT          |          WORK              Date    Shiftname  |   from    until   dur.  |   from    until   dur.  ======================|=========================|=========================2019-01-01 early      | 08:00:00 16:00:00 8.00h | 15:55:00 16:00:00 0.08h2019-01-01 late       | 16:00:00 00:00:00 8.00h | 16:00:00 16:15:00 0.25h

.net fiddle sample

关于c# - 关于日期格式的问题 - 16 :15 to 16:00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54212346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com