1996-08-19 06:14:33 +08:00
|
|
|
/*
|
|
|
|
* datetime_functions.c --
|
|
|
|
*
|
|
|
|
* This file defines new functions for the time and date data types.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996, Massimo Dal Zotto <dz@cs.unitn.it>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "utils/palloc.h"
|
1997-08-26 03:41:52 +08:00
|
|
|
#include "utils/datetime.h"
|
1996-08-19 06:14:33 +08:00
|
|
|
|
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
TimeADT *
|
|
|
|
time_difference(TimeADT * time1, TimeADT * time2)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
|
|
|
|
|
|
|
*result = *time1 - *time2;
|
|
|
|
return (result);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
TimeADT *
|
|
|
|
currenttime()
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
time_t current_time;
|
|
|
|
struct tm *tm;
|
|
|
|
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
|
|
|
|
|
|
|
|
current_time = time(NULL);
|
|
|
|
tm = localtime(¤t_time);
|
|
|
|
*result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
|
|
|
|
return (result);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
DateADT
|
|
|
|
currentdate()
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
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);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
hours(TimeADT * time)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
return (*time / (60 * 60));
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
minutes(TimeADT * time)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
return (((int) (*time / 60)) % 60);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
seconds(TimeADT * time)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
return (((int) *time) % 60);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
day(DateADT * date)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
struct tm tm;
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
j2date((*date + date2j(2000, 1, 1)),
|
|
|
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
return (tm.tm_mday);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
month(DateADT * date)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
struct tm tm;
|
1997-08-26 03:41:52 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
j2date((*date + date2j(2000, 1, 1)),
|
|
|
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
1997-08-26 03:41:52 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
return (tm.tm_mon);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
year(DateADT * date)
|
1997-08-26 03:41:52 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
struct tm tm;
|
1997-08-26 03:41:52 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
j2date((*date + date2j(2000, 1, 1)),
|
|
|
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
return (tm.tm_year);
|
1997-08-26 03:41:52 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
asminutes(TimeADT * time)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
int seconds = (int) *time;
|
1997-08-26 03:41:52 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
return (seconds / 60);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
int4
|
|
|
|
asseconds(TimeADT * time)
|
1997-08-26 03:41:52 +08:00
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
int seconds = (int) *time;
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1997-09-07 13:04:48 +08:00
|
|
|
return (seconds);
|
1997-08-26 03:41:52 +08:00
|
|
|
}
|