2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_subr_time.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gbx_subr.h"
|
2010-08-28 14:33:05 +02:00
|
|
|
#include "gbx_local.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_date.h"
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_timer(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
double result = 0.0;
|
|
|
|
|
|
|
|
DATE_timer(&result, TRUE);
|
|
|
|
|
|
|
|
SP->type = T_FLOAT;
|
|
|
|
SP->_float.value = result;
|
|
|
|
SP++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_now(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
DATE_now(SP);
|
|
|
|
SP++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_year(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
DATE_SERIAL *date;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
|
|
|
VALUE_conv(PARAM, T_DATE);
|
|
|
|
|
|
|
|
date = DATE_split(PARAM);
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
switch(code & 0xF)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
case 1: val = date->year; break;
|
|
|
|
case 2: val = date->month; break;
|
|
|
|
case 3: val = date->day; break;
|
|
|
|
case 4: val = date->hour; break;
|
|
|
|
case 5: val = date->min; break;
|
|
|
|
case 6: val = date->sec; break;
|
|
|
|
case 7: val = date->weekday; break;
|
|
|
|
case 8: val = date->msec; break;
|
|
|
|
default: val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PARAM->type = T_INTEGER;
|
|
|
|
PARAM->_integer.value = val;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
SUBR_LEAVE() /* Not necessary */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_date(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
DATE_SERIAL date;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
if (NPARAM <= 1)
|
|
|
|
{
|
|
|
|
if (NPARAM == 0)
|
|
|
|
DATE_now(PARAM);
|
|
|
|
else
|
|
|
|
VALUE_conv(PARAM, T_DATE);
|
|
|
|
|
|
|
|
date = *DATE_split(PARAM);
|
|
|
|
date.hour = 0;
|
|
|
|
date.min = 0;
|
|
|
|
date.sec = 0;
|
|
|
|
date.msec = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(PARAM);
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
VALUE_conv_integer(&PARAM[2]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
CLEAR(&date);
|
|
|
|
date.year = PARAM->_integer.value;
|
|
|
|
date.month = PARAM[1]._integer.value;
|
|
|
|
date.day = PARAM[2]._integer.value;
|
|
|
|
|
|
|
|
if (NPARAM >= 4)
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(&PARAM[3]);
|
2007-12-30 17:41:49 +01:00
|
|
|
date.hour = PARAM[3]._integer.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NPARAM >= 5)
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(&PARAM[4]);
|
2007-12-30 17:41:49 +01:00
|
|
|
date.min = PARAM[4]._integer.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NPARAM >= 6)
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(&PARAM[5]);
|
2007-12-30 17:41:49 +01:00
|
|
|
date.sec = PARAM[5]._integer.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATE_make(&date, RETURN))
|
|
|
|
THROW(E_DATE);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_time(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
DATE_SERIAL date;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
if (NPARAM <= 1)
|
|
|
|
{
|
|
|
|
if (NPARAM == 0)
|
|
|
|
DATE_now(PARAM);
|
|
|
|
|
|
|
|
date = *DATE_split(PARAM);
|
|
|
|
date.year = 0;
|
|
|
|
}
|
|
|
|
else if (NPARAM == 3)
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(PARAM);
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
VALUE_conv_integer(&PARAM[2]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
CLEAR(&date);
|
|
|
|
date.hour = PARAM->_integer.value;
|
|
|
|
date.min = PARAM[1]._integer.value;
|
|
|
|
date.sec = PARAM[2]._integer.value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
THROW(E_NEPARAM);
|
|
|
|
|
|
|
|
if (DATE_make(&date, RETURN))
|
|
|
|
THROW(E_DATE);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_date_op(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
SUBR_ENTER_PARAM(3);
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
switch (code & 0xF)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
case 0: /* DateAdd */
|
|
|
|
|
|
|
|
VALUE_conv(PARAM, T_DATE);
|
|
|
|
*RETURN = *PARAM;
|
|
|
|
DATE_add(RETURN, SUBR_get_integer(&PARAM[1]), SUBR_get_integer(&PARAM[2]));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* DateDiff */
|
|
|
|
|
|
|
|
VALUE_conv(PARAM, T_DATE);
|
|
|
|
VALUE_conv(&PARAM[1], T_DATE);
|
|
|
|
|
|
|
|
/* Dates are inverted! */
|
|
|
|
RETURN->_integer.value = DATE_diff(&PARAM[1], PARAM, SUBR_get_integer(&PARAM[2]));
|
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_week(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
bool plain = FALSE;
|
2010-08-28 14:33:05 +02:00
|
|
|
int start = LOCAL_get_first_day_of_week();
|
2007-12-30 17:41:49 +01:00
|
|
|
DATE_SERIAL ds;
|
|
|
|
VALUE date, first;
|
|
|
|
int day, n;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
if (NPARAM >= 1)
|
|
|
|
{
|
|
|
|
VALUE_conv(PARAM, T_DATE);
|
|
|
|
date = *PARAM;
|
|
|
|
|
|
|
|
if (NPARAM >= 2)
|
|
|
|
{
|
|
|
|
start = SUBR_get_integer(&PARAM[1]);
|
|
|
|
if (start < 0 || start > 6)
|
|
|
|
THROW(E_ARG);
|
|
|
|
|
|
|
|
if (NPARAM == 3)
|
2009-09-10 21:30:41 +02:00
|
|
|
plain = SUBR_get_boolean(&PARAM[2]);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DATE_now(&date);
|
|
|
|
|
|
|
|
/* Split it */
|
|
|
|
ds = *DATE_split(&date);
|
|
|
|
/* Set to 1 Jan of the current year */
|
|
|
|
ds.month = 1;
|
|
|
|
ds.day = 1;
|
|
|
|
ds.hour = 0;
|
|
|
|
ds.min = 0;
|
|
|
|
ds.sec = 0;
|
|
|
|
/* Convert to date & time */
|
|
|
|
DATE_make(&ds, &first);
|
|
|
|
/* Get the weekday of this 1 Jan */
|
|
|
|
day = DATE_split(&first)->weekday;
|
|
|
|
|
|
|
|
/* number of beginning days to ignore */
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (day != start)
|
|
|
|
{
|
|
|
|
day++;
|
|
|
|
if (day > 6)
|
|
|
|
day = 0;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plain)
|
|
|
|
{
|
|
|
|
if (n >= 4)
|
|
|
|
n -= 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
RETURN->_integer.value = (date._date.date - first._date.date - n + 7) / 7;
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|