postgresql/contrib/datetime/datetime_functions.c

119 lines
1.7 KiB
C
Raw Normal View History

1996-08-19 00:14:33 +02: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"
#include "utils/datetime.h"
1996-08-19 00:14:33 +02:00
TimeADT *
time_difference(TimeADT *time1, TimeADT *time2)
1996-08-19 00:14:33 +02:00
{
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
*result = *time1 - *time2;
return (result);
1996-08-19 00:14:33 +02:00
}
TimeADT *
currenttime()
1996-08-19 00:14:33 +02:00
{
time_t current_time;
struct tm *tm;
TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT));
current_time = time(NULL);
tm = localtime(&current_time);
*result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
return (result);
1996-08-19 00:14:33 +02:00
}
DateADT
currentdate()
1996-08-19 00:14:33 +02:00
{
time_t current_time;
struct tm *tm;
DateADT result;
current_time = time(NULL);
tm = localtime(&current_time);
result = date2j(tm->tm_year, tm->tm_mon + 1, tm->tm_mday) -
date2j(100, 1, 1);
return (result);
1996-08-19 00:14:33 +02:00
}
int4
hours(TimeADT *time)
1996-08-19 00:14:33 +02:00
{
return (*time / (60 * 60));
1996-08-19 00:14:33 +02:00
}
int4
minutes(TimeADT *time)
1996-08-19 00:14:33 +02:00
{
return (((int) (*time / 60)) % 60);
1996-08-19 00:14:33 +02:00
}
int4
seconds(TimeADT *time)
1996-08-19 00:14:33 +02:00
{
return (((int) *time) % 60);
1996-08-19 00:14:33 +02:00
}
int4
day(DateADT *date)
1996-08-19 00:14:33 +02:00
{
struct tm tm;
1996-08-19 00:14:33 +02:00
j2date((*date + date2j(2000, 1, 1)),
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
1996-08-19 00:14:33 +02:00
return (tm.tm_mday);
1996-08-19 00:14:33 +02:00
}
int4
month(DateADT *date)
1996-08-19 00:14:33 +02:00
{
struct tm tm;
j2date((*date + date2j(2000, 1, 1)),
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
return (tm.tm_mon);
1996-08-19 00:14:33 +02:00
}
int4
year(DateADT *date)
{
struct tm tm;
j2date((*date + date2j(2000, 1, 1)),
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
1996-08-19 00:14:33 +02:00
return (tm.tm_year);
}
int4
asminutes(TimeADT *time)
1996-08-19 00:14:33 +02:00
{
int seconds = (int) *time;
return (seconds / 60);
1996-08-19 00:14:33 +02:00
}
int4
asseconds(TimeADT *time)
{
int seconds = (int) *time;
1996-08-19 00:14:33 +02:00
return (seconds);
}