这样的转变:
1- 00:00 ->08:00
2- 08:00 ->16:00
3- 16:00 ->00:00
在轮班中,工人的工作很短暂。
有时工作从15:55开始到16:15结束。
我必须分开进行两班制。
例如15:55 ->16:00 and 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'
答案 0 :(得分:0)
您可以将舍入功能舍入到最接近的小时数,并使用SeM的建议,通常在此处询问时,您将显示您已经尝试过的内容的源代码。
d = load ("yourfile");
答案 1 :(得分:0)
您可以指定阈值并编写函数,以根据该函数评估并设置DateTime
var threshold = 15; //minutes
var dt = DateTime.Now.AddMinutes(-20);
//var dt = DateTime.Now;
DateTime resultDt;
DateTime thUp = dt.AddMinutes(15);
DateTime thDown = dt.AddMinutes(-15);
if (thUp.Hour != dt.Hour)
resultDt = new DateTime(dt.Year, dt.Month, dt.Day, thUp.Hour, 0, 0);
else if (thDown.Hour != dt.Hour)
resultDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0);
else
resultDt = dt;
答案 2 :(得分:0)
对于班次计算,您可以使用此代码
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.38h 2019-01-01 early | 06:00:00 14:00:00 8.00h | 06:00:00 14:00:00 8.00h 2019-01-01 late | 14:00:00 22:00:00 8.00h | 14:00:00 22:00:00 8.00h 2019-01-01 night | 22:00:00 06:00:00 8.00h | 22:00:00 06:00:00 8.00h 2019-01-02 early | 06:00:00 14:00:00 8.00h | 06:00:00 06:57:49 0.96h
或覆盖您的样本
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.08h 2019-01-01 late | 16:00:00 00:00:00 8.00h | 16:00:00 16:15:00 0.25h