Skip to content

Commit

Permalink
[FIXED] Working time issue when it is started on weekend
Browse files Browse the repository at this point in the history
Now, it has when with more cases to consider the `startDateTime`

Fixes #372
  • Loading branch information
hossain-khan committed Dec 23, 2024
1 parent e6a2822 commit 60069f0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/CodeSnippets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ fun main() {
// }

// Show disclaimer about the review time being inaccurate
Log.i(Art.warnAboutReviewTime())
Log.w(Art.warnAboutReviewTime())
}
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ fun main() {
}

// Show disclaimer about the review time being inaccurate
Log.i(Art.warnAboutReviewTime())
Log.w(Art.warnAboutReviewTime())
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class PullRequestStatsRepoImpl(
}

/**
* Evaluates actual time when PR was available for use to review by considering
* Evaluates actual time when PR was available for user to review by considering
* if review was requested later from the specified [reviewer].
* Or, if user self reviewed PR without being added, then the original time will be used.
*/
Expand Down
13 changes: 11 additions & 2 deletions src/main/kotlin/dev/hossain/time/DateTimeDiffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,22 @@ object DateTimeDiffer {
else -> {
var workingHours = Duration.ZERO
var previousWorkingDay =
if (startDateTime.isAfterWorkingHour()) startDateTime.nextWorkingHourOrSame() else startDateTime
when {
startDateTime.isOnWorkingDay().not() -> startDateTime.nextWorkingDay().prevWorkingHour()
startDateTime.isAfterWorkingHour() -> startDateTime.nextWorkingHourOrSame()
else -> startDateTime
}
var immediateNextWorkingDay = previousWorkingDay.nextNonWorkingHour()

/*println("startDateTime = ${startDateTime.format()},\n" +
"endDateTime = ${endDateTime.format()},\n" +
"previousWorkingDay = ${previousWorkingDay.format()},\n" +
"immediateNextWorkingDay = ${immediateNextWorkingDay.format()},\n" +
"immediateNextWorkingDay.isBefore(endDateTime)=${immediateNextWorkingDay.isBefore(endDateTime)},\n" +
"!immediateNextWorkingDay.isSameDay(endDateTime)=${!immediateNextWorkingDay.isSameDay(endDateTime)}")*/
"!immediateNextWorkingDay.isSameDay(endDateTime)=${!immediateNextWorkingDay.isSameDay(endDateTime)},\n" +
"previousWorkingDay.isSameDay(immediateNextWorkingDay)=${previousWorkingDay.isSameDay(immediateNextWorkingDay)},\n" +
"previousWorkingDay.isOnWorkingDay().not()=${previousWorkingDay.isOnWorkingDay().not()},\n" +
"\n")*/

// Loop through the dates while `immediateNextWorkingDay` is before end date and is not same day
while (immediateNextWorkingDay.isBefore(endDateTime) && !immediateNextWorkingDay.isSameDay(endDateTime)) {
Expand Down Expand Up @@ -118,6 +125,8 @@ object DateTimeDiffer {
startDateTime: ZonedDateTime,
endDateTime: ZonedDateTime,
): Duration {
// Checks if both start and end date-times are on working days.
// Throws an IllegalArgumentException if either of the date-times is not on a working day.
if ((startDateTime.isOnWorkingDay() && endDateTime.isOnWorkingDay()).not()) {
throw IllegalArgumentException("This function can only handle working day diff")
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/dev/hossain/time/TemporalsExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ object TemporalsExtension {

/**
* Adjuster that gives next end of day working hour or same if it's already non-working hour.
*
* TODO - consider day too
*
* Example:
* - Current Date Time: Monday 3 PM --> Monday 5 PM (shift to after 5:00 PM)
* - Current Date Time: Tuesday 10 AM --> Tuesday 5 PM (shift to after 5:00 PM)
* - Current Date Time: Wednesday 6 PM --> Wednesday 6 PM (same, already non-working hour)
* - Current Date Time: Thursday 8 PM --> Thursday 8 PM (same, already non-working hour)
* - Current Date Time: Friday 4 PM --> Friday 5 PM (shift to after 5:00 PM)
* - Current Date Time: Saturday 11 AM --> Saturday 11 AM (same, already non-working hour)
* - Current Date Time: Sunday 2 PM --> Sunday 2 PM (same, already non-working hour)
*/
NEXT_NON_WORKING_HOUR_OR_SAME {
override fun adjustInto(temporal: Temporal): Temporal =
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/dev/hossain/time/ZonedDateTimeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ internal fun ZonedDateTime.nextWorkingDayOrSame() = this.with(TemporalsExtension
*/
internal fun ZonedDateTime.nextWorkingHourOrSame() = this.with(TemporalsExtension.nextWorkingHourOrSame())

/**
* Provides the next non-working hour for the current date-time.
* If the current date-time is already within non-working hours, it returns the same date-time.
*
* Example:
* - Current Date Time: Monday 11 AM --> Monday 5 PM (End of the day)
* - Current Date Time: Saturday 11 AM --> Saturday 11 AM (Same - because on non-weekday)
* - Current Date Time: Sunday 11 AM --> Sunday 11 AM (Same - because on non-weekday)
* - Current Date Time: Monday 6 PM --> Monday 6 PM (Same - because it's after working hours)
* - Current Date Time: Tuesday 8 PM --> Tuesday 8 PM (Same - because it's after working hours)
* - Current Date Time: Tuesday 6 AM --> Tuesday 6 AM (Same - because it's before working hours)
*/
internal fun ZonedDateTime.nextNonWorkingHour() = this.with(TemporalsExtension.nextNonWorkingHourOrSame())

/**
Expand Down

0 comments on commit 60069f0

Please sign in to comment.