summaryrefslogtreecommitdiff
path: root/contrib/datetime/datetime_functions.c
blob: a59bd20db6434ae8af5f7cbfe5f2b20e4327a449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * 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"


TimeADT    *
time_difference(TimeADT * time1, TimeADT * time2)
{
	TimeADT    *result = (TimeADT *) palloc(sizeof(TimeADT));

	*result = *time1 - *time2;
	return (result);
}

TimeADT    *
currenttime()
{
	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);
}

DateADT
currentdate()
{
	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);
}

int4
hours(TimeADT * time)
{
	return (*time / (60 * 60));
}

int4
minutes(TimeADT * time)
{
	return (((int) (*time / 60)) % 60);
}

int4
seconds(TimeADT * time)
{
	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);
}