Nikolay Sivov : vbscript: Implement Weekday().

Alexandre Julliard julliard at winehq.org
Thu May 19 16:15:13 CDT 2022


Module: wine
Branch: master
Commit: da966b4d464e07cce58f5187439181abd2d52dde
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=da966b4d464e07cce58f5187439181abd2d52dde

Author: Nikolay Sivov <nsivov at codeweavers.com>
Date:   Thu May 19 20:17:03 2022 +0300

vbscript: Implement Weekday().

Signed-off-by: Nikolay Sivov <nsivov at codeweavers.com>
Signed-off-by: Jacek Caban <jacek at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/vbscript/global.c      | 46 ++++++++++++++++++++++++++++++++++++---
 dlls/vbscript/tests/api.vbs | 53 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/dlls/vbscript/global.c b/dlls/vbscript/global.c
index 1d3929ba249..84d3344918a 100644
--- a/dlls/vbscript/global.c
+++ b/dlls/vbscript/global.c
@@ -1982,10 +1982,50 @@ static HRESULT Global_Month(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt,
     return FAILED(hres) ? hres : return_short(res, st.wMonth);
 }
 
-static HRESULT Global_Weekday(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
+static HRESULT Global_Weekday(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    HRESULT hres = S_OK;
+    int first_day = 0;
+    SYSTEMTIME st;
+
+    TRACE("(%s)\n", debugstr_variant(args));
+
+    assert(args_cnt == 1 || args_cnt == 2);
+
+    /* [vbSunday = 1, vbSaturday = 7] -> wDayOfWeek [0, 6] */
+    if (args_cnt == 2)
+    {
+        if (V_VT(args + 1) == VT_NULL)
+            return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
+
+        hres = to_int(args + 1, &first_day);
+        if (SUCCEEDED(hres))
+        {
+            if (!first_day)
+            {
+                /* vbUseSystemDayOfWeek */
+                GetLocaleInfoW(This->ctx->lcid, LOCALE_RETURN_NUMBER | LOCALE_IFIRSTDAYOFWEEK, (LPWSTR)&first_day,
+                        sizeof(&first_day) / sizeof(WCHAR));
+                first_day = (first_day + 1) % 7;
+            }
+            else if (first_day >= 1 && first_day <= 7)
+            {
+                first_day--;
+            }
+            else
+                return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
+        }
+    }
+
+    if (FAILED(hres))
+        return hres;
+
+    if (V_VT(args) == VT_NULL)
+        return return_null(res);
+
+    if (FAILED(hres = to_system_time(args, &st))) return hres;
+
+    return return_short(res, 1 + (7 - first_day + st.wDayOfWeek) % 7);
 }
 
 static HRESULT Global_Year(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
diff --git a/dlls/vbscript/tests/api.vbs b/dlls/vbscript/tests/api.vbs
index a765eb1fcda..d27c373a964 100644
--- a/dlls/vbscript/tests/api.vbs
+++ b/dlls/vbscript/tests/api.vbs
@@ -2036,4 +2036,57 @@ call testDateAdd(DateSerial(2000, 1, 1), "ww", -1, DateSerial(1999, 12, 25))
 call testDateAdd(DateSerial(2000, 1, 1), "Ww", -1, DateSerial(1999, 12, 25))
 call testDateAddError()
 
+sub testWeekday(d, firstday, wd)
+    dim x, x2
+    x = Weekday(d, firstday)
+    call ok(x = wd, "weekday = " & x & " expected " & wd)
+    call ok(getVT(x) = "VT_I2*", "getVT = " & getVT(x))
+    if firstday = vbSunday then
+        x = Weekday(d)
+        call ok(x = wd, "weekday = " & x & " expected " & wd)
+    end if
+    x = Weekday(d, vbUseSystemDayOfWeek)
+    x2 = Weekday(d, firstDayOfWeek)
+    call ok(x = x2, "weekday = " & x & " expected " & x2)
+end sub
+
+sub testWeekdayError()
+    on error resume next
+    dim x
+    call Err.clear()
+    call Weekday(DateSerial(1000, 1, 1), 10)
+    call ok(Err.number = 5, "Err.number = " & Err.number)
+    call Err.clear()
+    call Weekday(DateSerial(1000, 1, 1), -1)
+    call ok(Err.number = 5, "Err.number = " & Err.number)
+    call Err.clear()
+    call Weekday(null, -1)
+    call ok(Err.number = 5, "Err.number = " & Err.number)
+    call Err.clear()
+    call Weekday(DateSerial(1000, 1, 1), null)
+    call ok(Err.number = 94, "Err.number = " & Err.number)
+    call Err.clear()
+    x = Weekday(null, vbSunday)
+    call ok(Err.number = 0, "Err.number = " & Err.number)
+    call ok(getVT(x) = "VT_NULL*", "getVT = " & getVT(x))
+    call Err.clear()
+    call Weekday(null, null)
+    call ok(Err.number = 94, "Err.number = " & Err.number)
+    call Err.clear()
+    call Weekday(null, "a")
+    call ok(Err.number = 13, "Err.number = " & Err.number)
+    call Err.clear()
+    call Weekday(DateSerial(1000, 1, 1), "a")
+    call ok(Err.number = 13, "Err.number = " & Err.number)
+end sub
+
+call testWeekday(DateSerial(2000, 1, 1), vbSunday, 7)
+call testWeekday(DateSerial(2000, 1, 1), vbMonday, 6)
+call testWeekday(DateSerial(2000, 1, 1), vbTuesday, 5)
+call testWeekday(DateSerial(2000, 1, 1), vbWednesday, 4)
+call testWeekday(DateSerial(2000, 1, 1), vbThursday, 3)
+call testWeekday(DateSerial(2000, 1, 1), vbFriday, 2)
+call testWeekday(DateSerial(2000, 1, 1), vbSaturday, 1)
+call testWeekdayError()
+
 Call reportSuccess()




More information about the wine-cvs mailing list