Commit bf5e4131 authored by Morten Welinder's avatar Morten Welinder
Browse files

DATE2UNIX and UNIX2DATE.

parent 7fc45bbc
2002-01-03 Morten Welinder <terra@diku.dk>
* src/datetime.c (datetime_serial_to_timet): New function.
2002-01-03 Jody Goldberg <jody@gnome.org>
* src/xml-io.c (xml_read_style_condition_chain) : no need to unref
......
......@@ -8,6 +8,9 @@ Jody:
* Some missing '...' in menu names
* Rename 'Summary' menu -> 'Properties'
Morten:
* DATE2UNIX and UNIX2DATE.
--------------------------------------------------------------------------
Gnumeric 1.0.0
......
2002-01-03 Morten Welinder <terra@diku.dk>
* src/datetime.c (datetime_serial_to_timet): New function.
2002-01-03 Jody Goldberg <jody@gnome.org>
* src/xml-io.c (xml_read_style_condition_chain) : no need to unref
......
2002-01-03 Morten Welinder <terra@diku.dk>
* src/datetime.c (datetime_serial_to_timet): New function.
2002-01-03 Jody Goldberg <jody@gnome.org>
* src/xml-io.c (xml_read_style_condition_chain) : no need to unref
......
2002-01-03 Morten Welinder <terra@diku.dk>
* src/datetime.c (datetime_serial_to_timet): New function.
2002-01-03 Jody Goldberg <jody@gnome.org>
* src/xml-io.c (xml_read_style_condition_chain) : no need to unref
......
......@@ -93,6 +93,67 @@ gnumeric_date (FunctionEvalInfo *ei, Value **argv)
/***************************************************************************/
static char *help_unix2date = {
N_("@FUNCTION=UNIX2DATE\n"
"@SYNTAX=UNIX2DATE(unixtime)\n"
"@DESCRIPTION="
"UNIX2DATE converts a unix time into a spreadsheet date and time.\n"
"\n"
"A unix time is the number of seconds since midnight January 1, 1970.\n"
"\n"
"@EXAMPLES=\n"
"\n"
"@SEEALSO=NOW, DATE, DATE2UNIX")
};
static Value *
gnumeric_unix2date (FunctionEvalInfo *ei, Value **argv)
{
gnum_float futime = value_get_as_float (argv [0]);
time_t utime = (time_t)futime;
/* Check for overflow. */
if (fabs (futime - utime) >= 1.0)
return value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_float (datetime_timet_to_serial_raw (utime) +
(futime - utime));
}
/***************************************************************************/
static char *help_date2unix = {
N_("@FUNCTION=DATE2UNIX\n"
"@SYNTAX=DATE2UNIX(serial)\n"
"@DESCRIPTION="
"DATE2UNIX converts a spreadsheet date and time serial number "
"into a unix time.\n"
"\n"
"A unix time is the number of seconds since midnight January 1, 1970.\n"
"\n"
"@EXAMPLES=\n"
"\n"
"@SEEALSO=NOW, DATE, UNIX2DATE")
};
static Value *
gnumeric_date2unix (FunctionEvalInfo *ei, Value **argv)
{
gnum_float fserial = value_get_as_float (argv [0]);
int serial = (int)fserial;
time_t utime = datetime_serial_to_timet (serial);
/* Check for overflow. */
if (fabs (fserial - serial) >= 1.0 || utime == (time_t)-1)
return value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_float (utime + (fserial - serial));
}
/***************************************************************************/
static char *help_datevalue = {
N_("@FUNCTION=DATEVALUE\n"
"@SYNTAX=DATEVALUE(date_str)\n"
......@@ -1050,6 +1111,15 @@ date_functions_init(void)
&help_date, gnumeric_date);
auto_format_function_result (def, AF_DATE);
def = function_add_args (cat, "unix2date", "f",
"unixtime",
&help_unix2date, gnumeric_unix2date);
auto_format_function_result (def, AF_DATE);
def = function_add_args (cat, "date2unix", "f",
"serial",
&help_date2unix, gnumeric_date2unix);
def = function_add_args (cat, "datevalue", "S",
"date_str",
&help_datevalue, gnumeric_datevalue);
......
......@@ -135,6 +135,23 @@ datetime_timet_to_serial (time_t t)
/* ------------------------------------------------------------------------- */
time_t
datetime_serial_to_timet (int serial)
{
GDate* gd = datetime_serial_to_g (serial);
struct tm tm;
if (!gd)
return (time_t)-1;
g_date_to_struct_tm (gd, &tm);
g_date_free (gd);
return mktime (&tm);
}
/* ------------------------------------------------------------------------- */
GDate *
datetime_value_to_g (const Value *v)
{
......
......@@ -24,6 +24,7 @@ int datetime_timet_to_serial (time_t t);
GDate* datetime_value_to_g (const Value *v);
int datetime_g_to_serial (GDate *date);
GDate* datetime_serial_to_g (int serial);
time_t datetime_serial_to_timet (int serial);
int datetime_serial_raw_to_serial (gnum_float raw);
/* These are time-only assuming a 24h day. It probably loses completely on */
......
2002-01-03 Morten Welinder <terra@diku.dk>
* fn-date.c (gnumeric_date2unix): New function.
(gnumeric_unix2date): New function.
2001-12-31 Jody Goldberg <jody@gnome.org>
* Release 1.0.0
......
......@@ -93,6 +93,67 @@ gnumeric_date (FunctionEvalInfo *ei, Value **argv)
/***************************************************************************/
static char *help_unix2date = {
N_("@FUNCTION=UNIX2DATE\n"
"@SYNTAX=UNIX2DATE(unixtime)\n"
"@DESCRIPTION="
"UNIX2DATE converts a unix time into a spreadsheet date and time.\n"
"\n"
"A unix time is the number of seconds since midnight January 1, 1970.\n"
"\n"
"@EXAMPLES=\n"
"\n"
"@SEEALSO=NOW, DATE, DATE2UNIX")
};
static Value *
gnumeric_unix2date (FunctionEvalInfo *ei, Value **argv)
{
gnum_float futime = value_get_as_float (argv [0]);
time_t utime = (time_t)futime;
/* Check for overflow. */
if (fabs (futime - utime) >= 1.0)
return value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_float (datetime_timet_to_serial_raw (utime) +
(futime - utime));
}
/***************************************************************************/
static char *help_date2unix = {
N_("@FUNCTION=DATE2UNIX\n"
"@SYNTAX=DATE2UNIX(serial)\n"
"@DESCRIPTION="
"DATE2UNIX converts a spreadsheet date and time serial number "
"into a unix time.\n"
"\n"
"A unix time is the number of seconds since midnight January 1, 1970.\n"
"\n"
"@EXAMPLES=\n"
"\n"
"@SEEALSO=NOW, DATE, UNIX2DATE")
};
static Value *
gnumeric_date2unix (FunctionEvalInfo *ei, Value **argv)
{
gnum_float fserial = value_get_as_float (argv [0]);
int serial = (int)fserial;
time_t utime = datetime_serial_to_timet (serial);
/* Check for overflow. */
if (fabs (fserial - serial) >= 1.0 || utime == (time_t)-1)
return value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_float (utime + (fserial - serial));
}
/***************************************************************************/
static char *help_datevalue = {
N_("@FUNCTION=DATEVALUE\n"
"@SYNTAX=DATEVALUE(date_str)\n"
......@@ -1050,6 +1111,15 @@ date_functions_init(void)
&help_date, gnumeric_date);
auto_format_function_result (def, AF_DATE);
def = function_add_args (cat, "unix2date", "f",
"unixtime",
&help_unix2date, gnumeric_unix2date);
auto_format_function_result (def, AF_DATE);
def = function_add_args (cat, "date2unix", "f",
"serial",
&help_date2unix, gnumeric_date2unix);
def = function_add_args (cat, "datevalue", "S",
"date_str",
&help_datevalue, gnumeric_datevalue);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment