/*
 * CM_WEEKDAY.pgc - cm 모듈 서비스 CM_WEEKDAY (one service = one file).
 * Split out of the former inline cm_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by cm_svr at tpsvrinit.
 */
#include "CM_WEEKDAY.h"

/* ----- service-local pure helper(s) ----- */
/* Sakamoto: 0=Sun..6=Sat */
static int cm_dow(int y, int m, int d)
{
    static int t[] = {0,3,2,5,0,3,5,1,4,6,2,4};
    if (m < 3) y -= 1;
    return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}


/* 6. CM_WEEKDAY - 요일 계산 (순수 계산): 0=일..6=토. */
void CM_WEEKDAY(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    static const char *KOR[] = {"일","월","화","수","목","금","토"};
    char in[16];
    int y, m, d, w;
    gets_(b, T_STR1, in, sizeof(in));
    if (sscanf(in, "%d-%d-%d", &y, &m, &d) != 3) FAIL(b);
    w = cm_dow(y, m, d);
    setl(b, T_ARG1, w);
    sets_(b, T_STR3, KOR[w]);
    setl(b, T_RC, (w == 0 || w == 6) ? 1 : 0);   /* 1=주말 */
    OK(b);
}
