aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/timestamp.c
blob: 4d3a8f9af21b59d4acebf7908286a9e62afa7574 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "postgres.h"
#include "miscadmin.h"
#include "utils/builtins.h"

/* sunos doesn't have this function */
#if defined(sunos4)
#define difftime(time1,time0)  ((time1) - (time0))
#endif

#if FALSE
/* copy the next part of the string into a buffer */
static const char *
cpstr(const char *s, char *buf)
{
	char in = 0;

	while (isspace(*s))
		s++;

	for (; *s && !isspace(*s); s++)
	{
		if (strchr("-,:/", *s))
		{
			buf[in] = 0;
			return(s + 1);
		}

		if (in < 16)
			buf[in++] = tolower(*s);
	}

	buf[in] = 0;
	return s;
}
#endif

/* assumes dd/mm/yyyy unless first item is month in word form */
time_t
timestamp_in(const char *timestamp_str)
{
    int4 result;

#if FALSE
    struct tm input_time;
	char buf[18];
	const char *p;
	static const char *mstr[] = {
		"january", "february", "march", "april", "may", "june",
		"july", "august", "september", "october", "november", "december"
	};

    memset(&input_time, 0, sizeof(input_time));
	p = cpstr(timestamp_str, buf);
	if (isdigit(buf[0]))	/* must be dd/mm/yyyy */
	{
		input_time.tm_mday = atoi(buf);
		p = cpstr(p, buf);
		if (!buf[0])
			elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
					timestamp_str);
		if (isdigit(buf[0]))
		{
			input_time.tm_mon = atoi(buf) - 1;
			if (input_time.tm_mon < 0 || input_time.tm_mon > 11)
				elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
					timestamp_str);
		}
		else
		{
			int i;
			for (i = 0; i < 12; i++)
				if (strncmp(mstr[i], buf, strlen(buf)) == 0)
					break;
			if (1 > 11)
				elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
						timestamp_str);
			input_time.tm_mon = i;
		}
	}
	else	/* must be month/dd/yyyy */
	{
		int i;
		for (i = 0; i < 12; i++)
			if (strncmp(mstr[i], buf, strlen(buf)) == 0)
				break;
		if (1 > 11)
			elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
					timestamp_str);
		input_time.tm_mon = i;
		p = cpstr(p, buf);
		input_time.tm_mday = atoi(buf);
		if (!input_time.tm_mday || input_time.tm_mday > 31)
			elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
	     timestamp_str);
    }

	p = cpstr(p, buf);
	if (!buf[0] || !isdigit(buf[0]))
		elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
				timestamp_str);
	if ((input_time.tm_year = atoi(buf)) < 1900)
		input_time.tm_year += 1900;

	/* now get the time */
	p = cpstr(p, buf);
	input_time.tm_hour = atoi(buf);
	p = cpstr(p, buf);
	input_time.tm_min = atoi(buf);
	p = cpstr(p, buf);
	input_time.tm_sec = atoi(buf);

    /* use mktime(), but make this GMT, not local time */
    result = mktime(&input_time);
#endif

    result = nabstimein( (char *) timestamp_str);

    return result;
}

char *
timestamp_out(time_t timestamp)
{
    char *result;
    int tz;
    double fsec = 0;
    struct tm tt, *tm = &tt;
    char buf[MAXDATELEN+1];
    char zone[MAXDATELEN+1], *tzn = zone;

#if FALSE
    time = localtime(&timestamp);

    sprintf(result, "%04d-%02d-%02d %02d:%02d:%02d",
	    time->tm_year+1900, time->tm_mon+1, time->tm_mday,
	    time->tm_hour, time->tm_min, time->tm_sec);
#endif
    abstime2tm( timestamp, &tz, tm, tzn);
    EncodeDateTime( tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);

    result = palloc(strlen(buf)+1);
    strcpy( result, buf);
    return result;
}

time_t
now(void)
{
    time_t sec;

    time(&sec);
    return(sec);
}

bool
timestampeq(time_t t1, time_t t2)
{
    return difftime(t1, t2) == 0;
}

bool
timestampne(time_t t1, time_t t2)
{
    return difftime(t1, t2) != 0;
}

bool
timestamplt(time_t t1, time_t t2)
{
    return difftime(t1, t2) > 0;
}

bool
timestampgt(time_t t1, time_t t2)
{
    return difftime(t1, t2) < 0;
}

bool
timestample(time_t t1, time_t t2)
{
    return difftime(t1, t2) >= 0;
}

bool
timestampge(time_t t1, time_t t2)
{
    return difftime(t1, t2) <= 0;
}

DateTime *
timestamp_datetime(time_t timestamp)
{
    DateTime *result;

    double fsec = 0;
    struct tm *tm;

    if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
	elog(WARN,"Memory allocation failed, can't convert timestamp to datetime",NULL);

    tm = localtime((time_t *) &timestamp);
    tm->tm_year += 1900;
    tm->tm_mon += 1;

    if (tm2datetime(tm, fsec, NULL, result) != 0)
	elog(WARN,"Unable to convert timestamp to datetime",timestamp_out(timestamp));

    return(result);
} /* timestamp_datetime() */