Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Get Start Day of Week?

Tags:

c++

date

I have adjusted the first day of week at Region and Local settings in a control panel (Windows 7), and now I'm writing a C++ function that must returns the first day of week which I have adjusted. Any Windows API or standard c++ function that I can use it ?

Thanks enter image description here

like image 453
Bassam Najeeb Avatar asked Dec 08 '25 04:12

Bassam Najeeb


2 Answers

(Sorry what I had written was totally wrong. I have updated the post.)

The right function to use is EnumCalendarInfoExEx:

#include <Windows.h>
#include <strsafe.h>
#include <iostream>

using namespace std;

BOOL CALLBACK EnumCalendarInfoProcExEx(LPWSTR lpszInfo, CALID calendar, LPWSTR lpReserved, LPARAM lParam)
{
    StringCchCopy(reinterpret_cast<LPWSTR>(lParam), 64, lpszInfo);
    return FALSE;
}

int main()
{
    WCHAR szDay[64];
    BOOL bResult = ::EnumCalendarInfoExEx(
        &EnumCalendarInfoProcExEx,
        LOCALE_NAME_USER_DEFAULT,
        ENUM_ALL_CALENDARS,
        nullptr,
        CAL_SDAYNAME1,
        reinterpret_cast<LPARAM>(szDay)
        );

    if (!bResult)
    {
        wcout << L"Error" << endl;
        return 0;
    }

    wcout << szDay << endl;
    return 0;
}

A couple of things to watch out for:

  • Even though the first day of the week is configured as Sunday in Control Panel on my PC, this returns Monday...
  • Apparently there can be multiple calendars for the user's locale. The above code only gets the first day for the first calendar.
like image 173
user1610015 Avatar answered Dec 10 '25 19:12

user1610015


I try use a GetLocaleInfoEx windows API and it worked well :)

int GetSystemStartDayOfWeek()
{
    int   ret;
    DWORD StartDayOfWeek;

    ret = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
        LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
        (LPTSTR)&StartDayOfWeek,
        sizeof(StartDayOfWeek) / sizeof(TCHAR));

    return StartDayOfWeek;
}
like image 25
Bassam Najeeb Avatar answered Dec 10 '25 20:12

Bassam Najeeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!