/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE LIBRARY
*   FILE     : lb_date.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   :
 *              
 *========================================================================
*/

/*
 *========================================================================== 
 * Head  정의                                                                
 *==========================================================================
*/ 

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include "def_set.h"
#include "dir_path.h"
#include "file_no.h"
#include "file_lb.h"  /* 공통 Library Prototypes */

#include "errlog.h"
#include "bas_tbl.h"
#include "lb_func_list.h"

/*
 *========================================================================== 
 * 전역 변수 정의                                                            
 *==========================================================================
*/

static int DAY[2][13] = {
    { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

/*
 *========================================================================
 *
 *   Function : lb_get_nowdate()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 현재날짜를 가져온다(YYYYMMDD)
 *
 *========================================================================
*/

int lb_get_nowdate(time_buf)
char *time_buf;     /* YYYYMMDD   */
{
    long      current_time;
    struct tm *struct_time;

    time(&current_time);
    struct_time = local_time(&current_time);
    struct_time->tm_year += 1900;
    sprintf(time_buf, "%04d%02d%02d", struct_time->tm_year, struct_time->tm_mon+1, struct_time->tm_mday);
    return(0);
}

/*
 *========================================================================
 *
 *   Function : lb_oper_date()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 날짜에 임의의 날수를 +하거나 -한 날짜를 구한다
 *              char    *Date      : YYYYMMDD     
 *              char    *Cdate     : YYYYMMDD    
 *              char     Op;       : + -             
 *              int      Days      : 연산될 날의 수.
 *              int      leap      : 윤년여부     
 *              char    *Cdate     : 계산한 날짜
 *========================================================================
*/              

int lb_oper_date(ndate, oper, oday, cdate)
char *ndate;
char  oper;
int    oday;
char  *cdate;
{
    int  leap, year, mon, day;
    char chdate[9];

    if((leap = lb_date_chk(ndate)) < 0)
        return(-1);
    year = lb_data2atoi(ndate, 4);
    mon = lb_data2atoi(ndate + 4, 2);
    day = lb_data2atoi(ndate + 6, 2);
    if(oper == '+')
    {
        day += oday;
        while(day > DAY[leap][mon])
        {
            day -= DAY[leap][mon];
            if(++mon > 12)
            {
                mon = 1;
                year += 1;
                sprintf(chdate, "%04d%02d%02d", year, mon, 1);
                leap = lb_date_chk(chdate);
            }
        }
    }
    else
    {
        day -= oday;
        while(day <= 0)
        {
            if(--mon == 0)
            {
                mon = 12;
                year -= 1;
                sprintf(chdate, "%04d%02d%02d", year, mon, 1);
                leap = lb_date_chk(chdate);
            }
            day += DAY[leap][mon];
        }
    }
    sprintf(cdate, "%04d%02d%02d", year, mon, day);
    return(leap);
}

/*
 *========================================================================
 *
 *   Function : lb_get_nowdate_tm()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 현재의 일시를 가져온다(YYYYMMDDHHMMSS)
 *
 *========================================================================
*/

int lb_get_nowdate_tm(time_buf)
char time_buf[15];                  /* YYYYMMDDHHMMSS */
{
    time_t    current_time;
    struct tm *struct_time;

    current_time = time(0);
    struct_time = local_time(&current_time);
    struct_time->tm_year += 1900;
    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d", struct_time->tm_year, struct_time->tm_mon+1, struct_time->tm_mday, struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);
    return(0);
}

/*
 *========================================================================
 *
 *   Function : lb_date_chk()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 날짜의 이상유무를 Check(YYYYMMDD)
 *
 *========================================================================
*/

int lb_date_chk(chkdate)
char *chkdate;
{
    int year, mon, day, leap;

    year = lb_data2atoi(chkdate,   4);
    mon = lb_data2atoi(chkdate+4, 2);
    day = lb_data2atoi(chkdate+6, 2);
    leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
    if(mon <= 0 || mon > 12 || day <= 0 || day > DAY[leap][mon])
        return(-1);

    return(leap);
}

/*
 *========================================================================
 *
 *   Function : lb_getdate2int()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 날짜를 INT형으로 변환
 *
 *========================================================================
*/

int lb_getdate2int( void )
{
    time_t current_time;
    struct tm *struct_time;
    
    current_time = time(NULL);
    struct_time = local_time(&current_time);
    return ( struct_time->tm_year + 1900 ) * 10000 + ( struct_time->tm_mon + 1 ) * 100 + struct_time->tm_mday;
}


/*
 *========================================================================
 *
 *   Function : chk_date_tm()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 날짜를 INT형으로 변환
 *
 *========================================================================
*/

struct tm *chk_date_tm ()
{
    time_t current_time;
    static struct tm struct_time;

    time (&current_time);
    memcpy ((char *)&struct_time, (char *)localtime (&current_time), sizeof (struct_time));

    return (&struct_time);
}

/*
 *========================================================================
 *
 *   Function : lintGetTime()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 펑션 콜 요청 시각을 리턴.
 *
 *========================================================================
*/

struct tm *lintGetTime ()
{
    time_t TimeTick;
    static struct tm curTime;

    time (&TimeTick);
    memcpy ((char *)&curTime, (char *)localtime (&TimeTick), sizeof (curTime));

    return (&curTime);
}

/*
 *========================================================================
 *
 *   Function : checkDate4H()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 펑션 콜 요청 시각을 리턴.
 *
 *========================================================================
*/

int checkDate4H (hour,min)
int hour;
int min;
{
    struct tm *curTime;
    curTime = (struct tm *)lintGetTime ();

    /*-- 년 마감 (통계) --*/
    if (curTime->tm_hour == hour &&
        curTime->tm_min  == min  &&
        curTime->tm_sec  == 0)   return (TRUE);
    else return (FALSE);
}

/*
 *========================================================================
 *
 *   Function : checkTime4M()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 펑션 콜 요청 시각을 리턴.
 *
 *========================================================================
*/

int checkTime4M (min,sec)
int min;
int sec;
{
    struct tm *curTime;

    curTime = (struct tm *)lintGetTime ();
/*
    printf("checkDate4M min%d,sec%d \n",min,sec);
    printf("curTime->tm_min %d : sec%d ==> MOD min ==>%d \n",curTime->tm_min,curTime->tm_sec,MOD(curTime->tm_min,  min));
*/
    if (MOD(curTime->tm_min,  min) == 0 && curTime->tm_sec == sec) return (TRUE);
    else return (FALSE);
}

/*
 *========================================================================
 *
 *   Function : checkDate4HSEC()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 펑션 콜 요청 시각을 리턴.
 *
 *========================================================================
*/

int checkDate4HSEC (hour,min,sec)
int hour;
int min;
int sec;
{
    struct tm *curTime;
    curTime = (struct tm *)lintGetTime ();
    /*-- 일마감  --*/
    if (curTime->tm_hour == hour &&
        curTime->tm_min  == min  &&
        sec == 1)   return (TRUE);
    else return (FALSE);
}

/*
 *========================================================================
 *
 *   Function : checkTime4MSEC()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 펑션 콜 요청 시각을 리턴.
 *
 *========================================================================
*/

int checkTime4MSEC (min,sec)
int min;
int sec;
{
    struct tm *curTime;

    curTime = (struct tm *)lintGetTime ();


    printf("checkDate4M min%d,sec%d \n",min,sec);
    printf("checkTime4MSEC curTime->tm_min %d : sec%d ==> MOD min ==>%d \n",curTime->tm_min,curTime->tm_sec,MOD(curTime->tm_min,  min));

    switch(curTime->tm_min)
    {
        case 1:  if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 11: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 21: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 31: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 41: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 51: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 3:  if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 13: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 23: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 33: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 43: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        case 53: if (sec == 1) return (TRUE);
                 else          return (FALSE);
        default: return (FALSE);
    }
}
