chenluhua1980
2026-01-08 f6cd6b95c9aa2ad17b199c32553c0b5e06b60bea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "stdafx.h"
#include "Configuration.h"
 
#include <mutex>
#include <string>
#include <unordered_map>
 
static bool TryParseHHMM(const std::string& text, int& outMinutes)
{
    int hour = 0;
    int minute = 0;
    if (sscanf_s(text.c_str(), "%d:%d", &hour, &minute) != 2) return false;
    if (hour < 0 || hour >= 24) return false;
    if (minute < 0 || minute >= 60) return false;
    outMinutes = hour * 60 + minute;
    return true;
}
 
BOOL CConfiguration::getProductionShiftStartMinutes(int& dayStartMinutes, int& nightStartMinutes)
{
    struct CachedShift {
        BOOL ok = FALSE;
        int day = 0;
        int night = 0;
        bool inited = false;
    };
 
    static std::mutex s_mtx;
    static std::unordered_map<std::string, CachedShift> s_cache;
 
    const std::string filePath((LPCSTR)(LPCTSTR)m_strFilepath);
    {
        std::lock_guard<std::mutex> g(s_mtx);
        auto it = s_cache.find(filePath);
        if (it != s_cache.end() && it->second.inited) {
            dayStartMinutes = it->second.day;
            nightStartMinutes = it->second.night;
            return it->second.ok;
        }
    }
 
    char buf[64] = {};
    GetPrivateProfileStringA("Production", "DayShiftStart", "08:00", buf, (DWORD)sizeof(buf), m_strFilepath);
    std::string dayStr(buf);
 
    GetPrivateProfileStringA("Production", "NightShiftStart", "", buf, (DWORD)sizeof(buf), m_strFilepath);
    std::string nightStr(buf);
 
    const int kDefaultDay = 8 * 60;
    const int kDefaultNight = 20 * 60;
 
    bool okDay = TryParseHHMM(dayStr, dayStartMinutes);
    bool okNight = false;
    if (!nightStr.empty()) okNight = TryParseHHMM(nightStr, nightStartMinutes);
 
    if (!okDay) dayStartMinutes = kDefaultDay;
    if (!okNight) nightStartMinutes = (dayStartMinutes + 12 * 60) % (24 * 60);
 
    if (dayStartMinutes == nightStartMinutes) {
        dayStartMinutes = kDefaultDay;
        nightStartMinutes = kDefaultNight;
        {
            std::lock_guard<std::mutex> g(s_mtx);
            s_cache[filePath] = CachedShift{ FALSE, dayStartMinutes, nightStartMinutes, true };
        }
        return FALSE;
    }
 
    const BOOL ok = (okDay && (nightStr.empty() ? TRUE : okNight)) ? TRUE : FALSE;
    {
        std::lock_guard<std::mutex> g(s_mtx);
        s_cache[filePath] = CachedShift{ ok, dayStartMinutes, nightStartMinutes, true };
    }
    return ok;
}