Am o situație în care vreau să adaug ore la o întâlnire și să am o nouă întindere în jurul zilei de lucru. Am cobbled o funcție pentru a determina această nouă dată, dar doriți să vă asigurați că nu uit nimic.
Orele care vor fi adăugate se numesc "întârziere". Ar putea fi cu ușurință un parametru al funcției.
Vă rugăm să postați sugestii. [Avertisment VB.NET]
Private Function GetDateRequired() As Date
''// A decimal representation of the current hour
Dim hours As Decimal = Decimal.Parse(Date.Now.Hour) + (Decimal.Parse(Date.Now.Minute) / 60.0)
Dim delay As Decimal = 3.0 ''// delay in hours
Dim endOfDay As Decimal = 12.0 + 5.0 ''// end of day, in hours
Dim startOfDay As Decimal = 8.0 ''// start of day, in hours
Dim newHour As Integer
Dim newMinute As Integer
Dim dateRequired As Date = Now
Dim delta As Decimal = hours + delay
''// Wrap around to the next day, if necessary
If delta > endOfDay Then
delta = delta - endOfDay
dateRequired = dateRequired.AddDays(1)
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
newHour = startOfDay + newHour
Else
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
End If
dateRequired = New Date(dateRequired.Year, dateRequired.Month, dateRequired.Day, newHour, newMinute, 0)
Return dateRequired
End Sub
Note: This will probably not work if delay is more than 9 hours long. It should never change from 3, through.
EDITAȚI | ×: Scopul este să găsiți data și ora pe care le obțineți ca urmare a adăugării mai multor ore la ora curentă. Acesta este folosit pentru a determina o valoare implicită pentru o dată scadentă a unei depuneri. Vreau să adaug 3 ore la ora curentă pentru a obține data scadentă. Cu toate acestea, nu vreau date scadente care să depășească ora 17:00 în ziua curentă. Deci, am incercat sa fac orele impartite intre ziua de azi (pana la ora 17:00) si (maine, de la ora 8 dimineata), astfel incat adaugarea a 3 ore pana la 4pm sa va dea 19am, pentru ca 1 ora este adaugata la sfarsitul zilei de astazi si 2 orele sunt adăugate la începutul de mâine.
Probabil ar trebui să scrieți câteva teste automatizate pentru fiecare condiție pe care o puteți gândi și apoi să începeți mai mult brainstormingul, scriind testele pe măsură ce vă gândiți la ele. În acest fel, puteți vedea cu siguranță că va funcționa și va continua să funcționeze dacă faceți alte modificări. Uită-te la Test Driven Development dacă îți plac rezultatele.
Bine, cum rămâne cu asta? Diferența dintre abordări ar trebui să vorbească de la sine.
De asemenea, acest lucru este testat în măsura în care îl pot arunca. Garanția durează până ... acum.
Sper ca ajuta!
Module Module1
Public Function IsInBusinessHours(ByVal d As Date) As Boolean
Return Not (d.Hour < 8 OrElse d.Hour > 17 OrElse d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday)
End Function
Public Function AddInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
Dim work As Date = fromDate.AddHours(hours)
While Not IsInBusinessHours(work)
work = work.AddHours(1)
End While
Return work
End Function
Public Function LoopInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
Dim work As Date = fromDate
While hours > 0
While hours > 0 AndAlso IsInBusinessHours(work)
work = work.AddHours(1)
hours -= 1
End While
While Not IsInBusinessHours(work)
work = work.AddHours(1)
End While
End While
Return work
End Function
Sub Main()
Dim test As Date = New Date(2008, 8, 8, 15, 0, 0)
Dim hours As Integer = 5
Console.WriteLine("Date: " + test.ToString() + ", " + hours.ToString())
Console.WriteLine("Just skipping: " + AddInBusinessHours(test, hours))
Console.WriteLine("Looping: " + LoopInBusinessHours(test, hours))
Console.ReadLine()
End Sub
End Module
Am lucrat cu următoarea formulă (pseudocod) cu un succes:
now <- number of minutes since the work day started
delay <- number of minutes in the delay
day <- length of a work day in minutes
x <- (now + delay) / day {integer division}
y <- (now + delay) % day {modulo remainder}
return startoftoday + x {in days} + y {in minutes}