mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
pg_password utility. Cleanup for psql passwords. New datetime contrib stuff for new version. Fix for strutils needing config.h.
This commit is contained in:
@ -9,139 +9,90 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "postgres.h"
|
||||
#include "pg_type.h"
|
||||
#include "utils/palloc.h"
|
||||
#include "utils/datetime.h"
|
||||
|
||||
typedef struct DateADT {
|
||||
char day;
|
||||
char month;
|
||||
short year;
|
||||
} DateADT;
|
||||
|
||||
typedef struct TimeADT {
|
||||
short hr;
|
||||
short min;
|
||||
float sec;
|
||||
} TimeADT;
|
||||
|
||||
TimeADT *
|
||||
time_difference(TimeADT *time1, TimeADT *time2)
|
||||
TimeADT *time_difference(TimeADT * time1, TimeADT * time2)
|
||||
{
|
||||
TimeADT *time = (TimeADT*)palloc(sizeof(TimeADT));
|
||||
|
||||
time->sec = time1->sec - time2->sec;
|
||||
time->min = time1->min - time2->min;
|
||||
time->hr = time1->hr - time2->hr;
|
||||
|
||||
if (time->sec < 0) {
|
||||
time->sec += 60.0;
|
||||
time->min--;
|
||||
} else if (time->sec >= 60.0) {
|
||||
time->sec -= 60.0;
|
||||
time->min++;
|
||||
}
|
||||
|
||||
if (time->min < 0) {
|
||||
time->min += 60;
|
||||
time->hr--;
|
||||
} else if (time->min >= 60) {
|
||||
time->min -= 60;
|
||||
time->hr++;
|
||||
}
|
||||
|
||||
if (time->hr < 0) {
|
||||
time->hr += 24;
|
||||
} else if (time->hr >= 24) {
|
||||
time->hr -= 24;
|
||||
}
|
||||
|
||||
return (time);
|
||||
}
|
||||
|
||||
TimeADT *
|
||||
currentTime()
|
||||
{
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
TimeADT *result = (TimeADT*)palloc(sizeof(TimeADT));
|
||||
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
result->sec = tm->tm_sec;
|
||||
result->min = tm->tm_min;
|
||||
result->hr = tm->tm_hour;
|
||||
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
*result = *time1 - *time2;
|
||||
return (result);
|
||||
}
|
||||
|
||||
int4
|
||||
currentDate()
|
||||
TimeADT *currenttime()
|
||||
{
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
int4 result;
|
||||
DateADT *date = (DateADT*)&result;
|
||||
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
||||
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
date->day = tm->tm_mday;
|
||||
date->month = tm->tm_mon+1;
|
||||
date->year = tm->tm_year+1900;
|
||||
|
||||
*result = ((((tm->tm_hour*60)+tm->tm_min)*60)+tm->tm_sec);
|
||||
return (result);
|
||||
}
|
||||
|
||||
int4
|
||||
hours(TimeADT *time)
|
||||
DateADT currentdate()
|
||||
{
|
||||
return (time->hr);
|
||||
time_t current_time;
|
||||
struct tm *tm;
|
||||
DateADT result;
|
||||
current_time = time(NULL);
|
||||
tm = localtime(¤t_time);
|
||||
|
||||
result = date2j(tm->tm_year,tm->tm_mon + 1,tm->tm_mday) -
|
||||
date2j(100,1,1);
|
||||
return (result);
|
||||
}
|
||||
int4 hours(TimeADT * time)
|
||||
{
|
||||
return(*time / (60*60));
|
||||
}
|
||||
|
||||
int4
|
||||
minutes(TimeADT *time)
|
||||
int4 minutes(TimeADT * time)
|
||||
{
|
||||
return (time->min);
|
||||
return(((int) (*time / 60)) % 60);
|
||||
}
|
||||
|
||||
int4
|
||||
seconds(TimeADT *time)
|
||||
int4 seconds(TimeADT * time)
|
||||
{
|
||||
int seconds = (int)time->sec;
|
||||
return(((int) *time) % 60);
|
||||
}
|
||||
int4 day(DateADT *date)
|
||||
{
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_mday);
|
||||
}
|
||||
int4 month(DateADT *date)
|
||||
{
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_mon);
|
||||
}
|
||||
int4 year(DateADT *date)
|
||||
{
|
||||
struct tm tm;
|
||||
|
||||
j2date( (*date + date2j(2000,1,1)),
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
||||
|
||||
return (tm.tm_year);
|
||||
}
|
||||
int4 asminutes(TimeADT * time)
|
||||
{
|
||||
int seconds = (int) *time;
|
||||
|
||||
return (seconds / 60);
|
||||
}
|
||||
int4 asseconds(TimeADT * time)
|
||||
{
|
||||
int seconds = (int) *time;
|
||||
|
||||
return (seconds);
|
||||
}
|
||||
|
||||
int4
|
||||
day(int4 val)
|
||||
{
|
||||
DateADT *date = (DateADT*)&val;
|
||||
return (date->day);
|
||||
}
|
||||
|
||||
int4
|
||||
month(int4 val)
|
||||
{
|
||||
DateADT *date = (DateADT*)&val;
|
||||
return (date->month);
|
||||
}
|
||||
|
||||
int4
|
||||
year(int4 val)
|
||||
{
|
||||
DateADT *date = (DateADT*)&val;
|
||||
return (date->year);
|
||||
}
|
||||
|
||||
int4
|
||||
asMinutes(TimeADT *time)
|
||||
{
|
||||
int seconds = (int)time->sec;
|
||||
return (time->min + 60*time->hr);
|
||||
}
|
||||
|
||||
int4
|
||||
asSeconds(TimeADT *time)
|
||||
{
|
||||
int seconds = (int)time->sec;
|
||||
return (seconds + 60*time->min + 3600*time->hr);
|
||||
}
|
||||
|
||||
|
@ -1,25 +1,16 @@
|
||||
From: Massimo Dal Zotto <dz@cs.unitn.it>
|
||||
Date: Tue, 14 May 1996 14:31:18 +0200 (MET DST)
|
||||
Subject: [PG95]: new postgres functions
|
||||
Date & time functions for use in Postgres version 6.1 & up
|
||||
|
||||
- -----BEGIN PGP SIGNED MESSAGE-----
|
||||
This functions replaces the original ones from the postgresql.org
|
||||
because the date & time structures has changed.
|
||||
|
||||
Some time ago I read in the mailing list requests of people looking
|
||||
for more time and date functions. I have now written some of them:
|
||||
There is a Makefile that should be "ajusted"
|
||||
for directories where postgres home after install.
|
||||
|
||||
time_difference(time1, time2) ,also defined as operator '-'
|
||||
hour(time)
|
||||
minutes(time)
|
||||
seconds(time)
|
||||
asMinutes(time)
|
||||
asSeconds(time)
|
||||
currentTime()
|
||||
currentDate()
|
||||
In this case: /usr/postgres.
|
||||
|
||||
The file can be compiled as shared library and loaded as dynamic module
|
||||
without need to recompile the backend. This can also be taken as an example
|
||||
of the extensibility of postgres (user-defined functions, operators, etc).
|
||||
I would be nice to see more of these user contributed modules posted to this
|
||||
list and hopefully accessible from the Postgres home page.
|
||||
Hope this can help,
|
||||
|
||||
|
||||
Sergio Lenzi (lenzi@bsi.com.br)
|
||||
|
||||
|
||||
|
@ -1,69 +1,75 @@
|
||||
|
||||
-- SQL code to load and define 'datetime' functions
|
||||
|
||||
-- load the new functions
|
||||
|
||||
load '/home/dz/lib/postgres/datetime_functions.so';
|
||||
|
||||
-- define the new functions in postgres
|
||||
func=$1
|
||||
cat <<% > datetime_functions.sql
|
||||
drop function time_difference(time,time);
|
||||
drop function currentdate();
|
||||
drop function currenttime();
|
||||
drop function hours(time);
|
||||
drop function minutes(time);
|
||||
drop function seconds(time);
|
||||
drop function day(date);
|
||||
drop function month(date);
|
||||
drop function year(date);
|
||||
drop function asminutes(time);
|
||||
drop function asseconds(time);
|
||||
drop operator - (time,time);
|
||||
|
||||
create function time_difference(time,time)
|
||||
returns time
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function currentDate()
|
||||
create function currentdate()
|
||||
returns date
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function currentTime()
|
||||
create function currenttime()
|
||||
returns time
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function hours(time)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function minutes(time)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function seconds(time)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function day(date)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function month(date)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function year(date)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function asMinutes(time)
|
||||
create function asminutes(time)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create function asSeconds(time)
|
||||
create function asseconds(time)
|
||||
returns int4
|
||||
as '/home/dz/lib/postgres/datetime_functions.so'
|
||||
as '$func'
|
||||
language 'c';
|
||||
|
||||
create operator - (
|
||||
leftarg=time,
|
||||
rightarg=time,
|
||||
procedure=time_difference);
|
||||
|
||||
%
|
||||
|
Reference in New Issue
Block a user