41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
|
|
|
namespace UtopiaCanteenSystem.Helpers;
|
|
|
|
/// <summary>
|
|
/// Helper for scan session rules: limit one scan per minute (1-minute session).
|
|
/// </summary>
|
|
public static class ScanSessionHelper
|
|
{
|
|
/// <summary>
|
|
/// Checks if a new scan is allowed: no other scan in the same minute (UTC).
|
|
/// </summary>
|
|
/// <param name="lastScanTimeUtc">Last scan time in UTC; null if none.</param>
|
|
/// <param name="nowUtc">Current time in UTC.</param>
|
|
/// <returns>True if a new scan is allowed.</returns>
|
|
public static bool IsNewScanAllowed(DateTime? lastScanTimeUtc, DateTime nowUtc)
|
|
{
|
|
if (lastScanTimeUtc == null)
|
|
return true;
|
|
|
|
// Same minute (year, month, day, hour, minute) = not allowed
|
|
var last = lastScanTimeUtc.Value;
|
|
if (last.Year == nowUtc.Year &&
|
|
last.Month == nowUtc.Month &&
|
|
last.Day == nowUtc.Day &&
|
|
last.Hour == nowUtc.Hour &&
|
|
last.Minute == nowUtc.Minute)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Truncates the given UTC time to the start of its minute (session window).
|
|
/// </summary>
|
|
public static DateTime TruncateToMinute(DateTime utc)
|
|
{
|
|
return new DateTime(utc.Year, utc.Month, utc.Day, utc.Hour, utc.Minute, 0, DateTimeKind.Utc);
|
|
}
|
|
}
|