Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0703814

Browse files
committed
From: "D'Arcy J.M. Cain" <darcy@druid.net>
Subject: [HACKERS] backend/utils/adt/timestamp.cBack to this timezone stuff. The struct tm has a field (tm_gmtoff) whichis the offset from UTC (GMT is archaic BTW) in seconds. Is this thevalue you are looking for when you use timezone? Note that this appliesto NetBSD but it does not appear to be in either ANSI C or POSIX. Thislooks like one of those things that is just going to have to be handcoded for each platform.Why not just store the values in UTC and use localtime instead ofgmtime 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. Youshould use time_t here.The input function seems unduly restrictive. Somewhere in the sourcesthere is an input function that allows words for months. Can't we dothe same here?There is a standard function, difftime, for subtracting two times. Itdeals with cases where time_t is not integral. There is, however, asmall performance hit since it returns a double and I don't believethere is any system currently which uses anything but an integral fortime_t. Still, this is technically the correct and portable thing to do.The returns from the various comparisons should probably be a bool.
1 parentc2e73db commit0703814

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(constchar*timestamp_str)
99
{
1010
structtminput_time;
1111
int4result;
@@ -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
returnresult;
3130
}
3231

3332
char*
34-
timestamp_out(int4timestamp)
33+
timestamp_out(time_ttimestamp)
3534
{
3635
char*result;
3736
structtm*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
returnresult;
4645
}
4746

48-
int4
47+
time_t
4948
now(void)
5049
{
51-
structtmignore;
5250
time_tsec;
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(int4t1,int4t2)
56+
bool
57+
timestampeq(time_tt1,time_tt2)
6658
{
67-
returnt1==t2;
59+
returndifftime(t1,t2)==0;
6860
}
6961

70-
int4
71-
timestampne(int4t1,int4t2)
62+
bool
63+
timestampne(time_tt1,time_tt2)
7264
{
73-
returnt1!=t2;
65+
returndifftime(t1,t2)!=0;
7466
}
7567

76-
int4
77-
timestamplt(int4t1,int4t2)
68+
bool
69+
timestamplt(time_tt1,time_tt2)
7870
{
79-
returnt1<t2;
71+
returndifftime(t1,t2)>0;
8072
}
8173

82-
int4
83-
timestampgt(int4t1,int4t2)
74+
bool
75+
timestampgt(time_tt1,time_tt2)
8476
{
85-
returnt1>t2;
77+
returndifftime(t1,t2)<0;
8678
}
8779

88-
int4
89-
timestample(int4t1,int4t2)
80+
bool
81+
timestample(time_tt1,time_tt2)
9082
{
91-
returnt1 <=t2;
83+
returndifftime(t1,t2) >=0;
9284
}
9385

94-
int4
95-
timestampge(int4t1,int4t2)
86+
bool
87+
timestampge(time_tt1,time_tt2)
9688
{
97-
returnt1 >=t2;
89+
returndifftime(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
externchar*tidout(ItemPointeritemPtr);
408408

409409
/* timestamp.c */
410-
externint4timestamp_in(char*timestamp_str);
411-
externchar*timestamp_out(int4timestamp);
412-
externint4now(void);
413-
int4timestampeq(int4t1,int4t2);
414-
int4timestampne(int4t1,int4t2);
415-
int4timestamplt(int4t1,int4t2);
416-
int4timestampgt(int4t1,int4t2);
417-
int4timestample(int4t1,int4t2);
418-
int4timestampge(int4t1,int4t2);
410+
externtime_ttimestamp_in(constchar*timestamp_str);
411+
externchar*timestamp_out(time_ttimestamp);
412+
externtime_tnow(void);
413+
booltimestampeq(time_tt1,time_tt2);
414+
booltimestampne(time_tt1,time_tt2);
415+
booltimestamplt(time_tt1,time_tt2);
416+
booltimestampgt(time_tt1,time_tt2);
417+
booltimestample(time_tt1,time_tt2);
418+
booltimestampge(time_tt1,time_tt2);
419419

420420
/* varchar.c */
421421
externchar*bpcharin(char*s,intdummy,inttyplen);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp