Skip to content
Snippets Groups Projects
Commit 93f544f4 authored by Martin Stjernholm's avatar Martin Stjernholm
Browse files

Added time and date conversion functions.

Rev: lib/modules/Sql.pmod/mysql.pike:1.5
Rev: lib/modules/Sql.pmod/sql.pike:1.25
parent 63783214
No related branches found
No related tags found
No related merge requests found
/* /*
* $Id: mysql.pike,v 1.4 1998/03/20 21:58:23 grubba Exp $ * $Id: mysql.pike,v 1.5 1998/07/03 20:03:49 mast Exp $
* *
* Glue for the Mysql-module * Glue for the Mysql-module
*/ */
//. //.
//. File: mysql.pike //. File: mysql.pike
//. RCSID: $Id: mysql.pike,v 1.4 1998/03/20 21:58:23 grubba Exp $ //. RCSID: $Id: mysql.pike,v 1.5 1998/07/03 20:03:49 mast Exp $
//. Author: Henrik Grubbström (grubba@idonex.se) //. Author: Henrik Grubbström (grubba@idonex.se)
//. //.
//. Synopsis: Implements the glue to the Mysql-module. //. Synopsis: Implements the glue to the Mysql-module.
...@@ -28,3 +28,100 @@ string quote(string s) ...@@ -28,3 +28,100 @@ string quote(string s)
({ "\\", "\"", "\0", "\'", "\n", "\r" }), ({ "\\", "\"", "\0", "\'", "\n", "\r" }),
({ "\\\\", "\\\"", "\\0", "\\\'", "\\n", "\\r" }))); ({ "\\\\", "\\\"", "\\0", "\\\'", "\\n", "\\r" })));
} }
// The following time conversion functions assumes the SQL server
// handles time in this local timezone. They map the special zero
// time/date spec to 0.
private constant timezone = localtime (0)->timezone;
//. - encode_time
//. Converts a system time value to an appropriately formatted time
//. spec for the database.
//. > time - Time to encode.
//. > date - If nonzero then time is taken as a "full" unix time spec
//. (where the date part is ignored), otherwise it's converted as a
//. seconds-since-midnight value.
string encode_time (int time, void|int date)
{
if (date) {
if (!time) return "000000";
mapping(string:int) ct = localtime (time);
return sprintf ("%02d%02d%02d", ct->hour, ct->min, ct->sec);
}
else return sprintf ("%02d%02d%02d", time / 3600 % 24, time / 60 % 60, time % 60);
}
//. - encode_date
//. Converts a system time value to an appropriately formatted
//. date-only spec for the database.
//. > time - Time to encode.
string encode_date (int time)
{
if (!time) return "00000000";
mapping(string:int) ct = localtime (time);
return sprintf ("%04d%02d%02d", ct->year + 1900, ct->mon + 1, ct->mday);
}
//. - encode_datetime
//. Converts a system time value to an appropriately formatted
//. date and time spec for the database.
//. > time - Time to encode.
string encode_datetime (int time)
{
if (!time) return "00000000000000";
mapping(string:int) ct = localtime (time);
return sprintf ("%04d%02d%02d%02d%02d%02d",
ct->year + 1900, ct->mon + 1, ct->mday,
ct->hour, ct->min, ct->sec);
}
//. - decode_time
//. Converts a database time spec to a system time value.
//. > timestr - Time spec to decode.
//. > date - Take the date part from this system time value. If zero, a
//. seconds-since-midnight value is returned.
int decode_time (string timestr, void|int date)
{
int hour = 0, min = 0, sec = 0;
if (sscanf (timestr, "%d:%d:%d", hour, min, sec) <= 1)
sscanf (timestr, "%2d%2d%2d", hour, min, sec);
if (date && (hour || min || sec)) {
mapping(string:int) ct = localtime (date);
return mktime (sec, min, hour, ct->mday, ct->mon, ct->year, ct->isdst, ct->timezone);
}
else return (hour * 60 + min) * 60 + sec;
}
//. - decode_date
//. Converts a database date-only spec to a system time value.
//. Assumes 4-digit years.
//. > datestr - Date spec to decode.
int decode_date (string datestr)
{
int year = 0, mon = 0, mday = 0, n;
n = sscanf (datestr, "%d-%d-%d", year, mon, mday);
if (n <= 1) n = sscanf (datestr, "%4d%2d%2d", year, mon, mday);
if (year || mon || mday)
return mktime (0, 0, 0, n == 3 ? mday : 1, n >= 2 && mon - 1, year - 1900,
-1, timezone);
else return 0;
}
//. - decode_datetime
//. Converts a database date and time spec to a system time value.
//. Can decode strings missing the time part.
//. > datestr - Date and time spec to decode.
int decode_datetime (string timestr)
{
array(string) a = timestr / " ";
if (sizeof (a) == 2)
return decode_date (a[0]) + decode_time (a[1]);
else {
int n = sizeof (timestr);
if (n >= 12)
return decode_date (timestr[..n-7]) + decode_time (timestr[n-6..n-1]);
else
return decode_date (timestr);
}
}
/* /*
* $Id: sql.pike,v 1.24 1998/07/03 11:42:28 grubba Exp $ * $Id: sql.pike,v 1.25 1998/07/03 20:03:51 mast Exp $
* *
* Implements the generic parts of the SQL-interface * Implements the generic parts of the SQL-interface
* *
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
//. //.
//. File: sql.pike //. File: sql.pike
//. RCSID: $Id: sql.pike,v 1.24 1998/07/03 11:42:28 grubba Exp $ //. RCSID: $Id: sql.pike,v 1.25 1998/07/03 20:03:51 mast Exp $
//. Author: Henrik Grubbstrm (grubba@idonex.se) //. Author: Henrik Grubbstrm (grubba@idonex.se)
//. //.
//. Synopsis: Implements the generic parts of the SQL-interface. //. Synopsis: Implements the generic parts of the SQL-interface.
...@@ -43,6 +43,80 @@ string quote(string s) ...@@ -43,6 +43,80 @@ string quote(string s)
return(replace(s, "\'", "\'\'")); return(replace(s, "\'", "\'\'"));
} }
//. - encode_time
//. Converts a system time value to an appropriately formatted time
//. spec for the database.
//. > time - Time to encode.
//. > date - If nonzero then time is taken as a "full" unix time spec
//. (where the date part is ignored), otherwise it's converted as a
//. seconds-since-midnight value.
string encode_time (int time, void|int date)
{
if (functionp (master_sql->encode_time))
return master_sql->encode_time (time, date);
else
throw_error ("sql->encode_time(): Not supported by this database\n");
}
//. - decode_time
//. Converts a database time spec to a system time value.
//. > timestr - Time spec to decode.
//. > date - Take the date part from this system time value. If zero, a
//. seconds-since-midnight value is returned.
int decode_time (string timestr, void|int date)
{
if (functionp (master_sql->decode_time))
return master_sql->decode_time (timestr, date);
else
throw_error ("sql->decode_time(): Not supported by this database\n");
}
//. - encode_date
//. Converts a system time value to an appropriately formatted
//. date-only spec for the database.
//. > time - Time to encode.
string encode_date (int time)
{
if (functionp (master_sql->encode_date))
return master_sql->encode_date (time);
else
throw_error ("sql->encode_date(): Not supported by this database\n");
}
//. - decode_date
//. Converts a database date-only spec to a system time value.
//. > datestr - Date spec to decode.
int decode_date (string datestr)
{
if (functionp (master_sql->decode_date))
return master_sql->decode_date (datestr);
else
throw_error ("sql->decode_date(): Not supported by this database\n");
}
//. - encode_datetime
//. Converts a system time value to an appropriately formatted
//. date and time spec for the database.
//. > time - Time to encode.
string encode_datetime (int time)
{
if (functionp (master_sql->encode_datetime))
return master_sql->encode_datetime (time);
else
throw_error ("sql->encode_datetime(): Not supported by this database\n");
}
//. - decode_datetime
//. Converts a database date and time spec to a system time value.
//. > datestr - Date and time spec to decode.
int decode_datetime (string timestr)
{
if (functionp (master_sql->decode_datetime))
return master_sql->decode_datetime (timestr);
else
throw_error ("sql->decode_datetime(): Not supported by this database\n");
}
//. - create //. - create
//. Create a new generic SQL object. //. Create a new generic SQL object.
//. > host //. > host
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment