Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0703814

Browse files
committed
From: "D'Arcy J.M. Cain" <darcy@druid.net>
Subject: [HACKERS] backend/utils/adt/timestamp.c Back to this timezone stuff. The struct tm has a field (tm_gmtoff) which is the offset from UTC (GMT is archaic BTW) in seconds. Is this the value you are looking for when you use timezone? Note that this applies to NetBSD but it does not appear to be in either ANSI C or POSIX. This looks like one of those things that is just going to have to be hand coded for each platform. Why not just store the values in UTC and use localtime instead of gmtime when retrieving the value? Also, you assume the time is returned as a 4 byte integer. In fact, there is not even any requirement that time be an integral value. You should use time_t here. The input function seems unduly restrictive. Somewhere in the sources there is an input function that allows words for months. Can't we do the same here? There is a standard function, difftime, for subtracting two times. It deals with cases where time_t is not integral. There is, however, a small performance hit since it returns a double and I don't believe there is any system currently which uses anything but an integral for time_t. Still, this is technically the correct and portable thing to do. The returns from the various comparisons should probably be a bool.
1 parent c2e73db commit 0703814

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include "postgres.h"
55
#include "utils/builtins.h"
66

7-
int4
8-
timestamp_in(char *timestamp_str)
7+
time_t
8+
timestamp_in(const char *timestamp_str)
99
{
1010
struct tm input_time;
1111
int4 result;
@@ -25,18 +25,17 @@ timestamp_in(char *timestamp_str)
2525

2626
/* use mktime(), but make this GMT, not local time */
2727
result = mktime(&input_time);
28-
result -= timezone;
2928

3029
return result;
3130
}
3231

3332
char *
34-
timestamp_out(int4 timestamp)
33+
timestamp_out(time_t timestamp)
3534
{
3635
char *result;
3736
struct tm *time;
3837

39-
time = gmtime((time_t *)&timestamp);
38+
time = localtime((time_t *)&timestamp);
4039
result = palloc(20);
4140
sprintf(result, "%04d-%02d-%02d %02d:%02d:%02d",
4241
time->tm_year+1900, time->tm_mon+1, time->tm_mday,
@@ -45,54 +44,47 @@ timestamp_out(int4 timestamp)
4544
return result;
4645
}
4746

48-
int4
47+
time_t
4948
now(void)
5049
{
51-
struct tm ignore;
5250
time_t sec;
5351

54-
/* we want the local time here. but 'timezone' doesn't get set */
55-
/* until we do a mktime(). so do one. */
56-
memset(&ignore, 0, sizeof(ignore));
57-
mktime(&ignore);
58-
5952
time(&sec);
60-
sec -= timezone;
61-
return((int4)sec);
53+
return(sec);
6254
}
6355

64-
int4
65-
timestampeq(int4 t1, int4 t2)
56+
bool
57+
timestampeq(time_t t1, time_t t2)
6658
{
67-
return t1 == t2;
59+
return difftime(t1, t2) == 0;
6860
}
6961

70-
int4
71-
timestampne(int4 t1, int4 t2)
62+
bool
63+
timestampne(time_t t1, time_t t2)
7264
{
73-
return t1 != t2;
65+
return difftime(t1, t2) != 0;
7466
}
7567

76-
int4
77-
timestamplt(int4 t1, int4 t2)
68+
bool
69+
timestamplt(time_t t1, time_t t2)
7870
{
79-
return t1 < t2;
71+
return difftime(t1, t2) > 0;
8072
}
8173

82-
int4
83-
timestampgt(int4 t1, int4 t2)
74+
bool
75+
timestampgt(time_t t1, time_t t2)
8476
{
85-
return t1 > t2;
77+
return difftime(t1, t2) < 0;
8678
}
8779

88-
int4
89-
timestample(int4 t1, int4 t2)
80+
bool
81+
timestample(time_t t1, time_t t2)
9082
{
91-
return t1 <= t2;
83+
return difftime(t1, t2) >= 0;
9284
}
9385

94-
int4
95-
timestampge(int4 t1, int4 t2)
86+
bool
87+
timestampge(time_t t1, time_t t2)
9688
{
97-
return t1 >= t2;
89+
return difftime(t1, t2) <= 0;
9890
}

src/include/utils/builtins.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: builtins.h,v 1.12 1997/03/14 23:33:18 scrappy Exp $
9+
* $Id: builtins.h,v 1.13 1997/03/25 09:25:33 scrappy Exp $
1010
*
1111
* NOTES
1212
* This should normally only be included by fmgr.h.
@@ -407,15 +407,15 @@ extern ItemPointer tidin(char *str);
407407
extern char *tidout(ItemPointer itemPtr);
408408

409409
/* timestamp.c */
410-
extern int4 timestamp_in(char *timestamp_str);
411-
extern char *timestamp_out(int4 timestamp);
412-
extern int4 now(void);
413-
int4 timestampeq(int4 t1, int4 t2);
414-
int4 timestampne(int4 t1, int4 t2);
415-
int4 timestamplt(int4 t1, int4 t2);
416-
int4 timestampgt(int4 t1, int4 t2);
417-
int4 timestample(int4 t1, int4 t2);
418-
int4 timestampge(int4 t1, int4 t2);
410+
extern time_t timestamp_in(const char *timestamp_str);
411+
extern char *timestamp_out(time_t timestamp);
412+
extern time_t now(void);
413+
bool timestampeq(time_t t1, time_t t2);
414+
bool timestampne(time_t t1, time_t t2);
415+
bool timestamplt(time_t t1, time_t t2);
416+
bool timestampgt(time_t t1, time_t t2);
417+
bool timestample(time_t t1, time_t t2);
418+
bool timestampge(time_t t1, time_t t2);
419419

420420
/* varchar.c */
421421
extern char *bpcharin(char *s, int dummy, int typlen);

0 commit comments

Comments
 (0)