C 언어 구현 시간 대 변환 함수 의 인 스 턴 스
시간 대 변환 함수
기능:
시간 대 1 을 시간 대 2 로 바 꾸 는 시간.
인자:
arg 1--입력 시간
arg 2--시간 대 1(arg 1 현재 시간 대)
arg 3--시간 대 2(전환 할 시간 대)
요청:
매개 변수 arg 1 형식 은 timestamp 입 니 다.
24 개 시간 대(1-24 로 표시)
pgproc.h 에 함수 정의 추가
src/include/catalog/pg_proc.h
DATA(insert OID = 6668 ( timezone_convert PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 1114 "1114 23 23" _null_ _null_ _null_ _null_ _null_ timezone_convert _null_ _null_ _null_ ));
DESCR("timestamp convert.");
...에 있다 src/backend/utils/adt/my funcs.c 에서 함수 구현
Datum
timezone_convert(PG_FUNCTION_ARGS)
{
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
int32 zone1 = PG_GETARG_INT32(1);
int32 zone2 = PG_GETARG_INT32(2);
Timestamp result = 0;
if (!((1 <= zone1 && zone1 <= 24) && (1 <= zone2 && zone2 <= 24)))
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range.the parameter is 1..24")));
}
if (TIMESTAMP_NOT_FINITE(timestamp))
{
PG_RETURN_TIMESTAMP(timestamp);
}
/** **/
PG_RETURN_TIMESTAMP(result);
}
매개 변수 판단 합 법성 가 져 오기생각:
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
timestamp -> day; timestamp -> hour;
hour = hour + zone2 - zone1;
hour >= 24
hour -= 24;
day += 1;
hour < 0
hour += 24;
day -= 1;
return timestamp;
src/include/pgtime.h
struct pg_tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday; /* 1..31 */
int tm_mon; /* origin 0, not 1 */
int tm_year; /* relative to 1900 */
int tm_wday; /* 0..6 (0 )*/
int tm_yday; /* 1..366 Julian date */
int tm_isdst;
long int tm_gmtoff;
const char *tm_zone;
};
/src/include/utils/timestamp.htimestamp 와 pg 를 정의 하 였 습 니 다.tm 변환 방법
extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone);
timestamp 2 tm()첫 번 째 인 자 는 timestamp 를 입력 하고 세 번 째 는 출력 pg 입 니 다.tm,네 번 째 는 출력 소수 초 입 니 다.다른 몇 개의 매개 변 수 는 시간 대 와 관련 되 고 두 번 째,5 개의 매개 변수 도 참조 입 니 다.마지막 으로 NULL 을 설정 하면 현재 세 션 시간 대 를 표시 할 수 있 습 니 다.흐름:
코드:
Datum
timezone_convert(PG_FUNCTION_ARGS)
{
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
int32 zone1 = PG_GETARG_INT32(1);
int32 zone2 = PG_GETARG_INT32(2);
struct pg_tm tt, *tm = &tt;
int day;
fsec_t fsec;
Timestamp result = 0;
if (!((1 <= zone1 && zone1 <= 24) && (1 <= zone2 && zone2 <= 24)))
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range.the parameter is 1..24")));
}
if (TIMESTAMP_NOT_FINITE(timestamp))
{
PG_RETURN_TIMESTAMP(timestamp);
}
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
}
day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
tm->tm_hour = tm->tm_hour + zone2 - zone1;
if(tm->tm_hour >= 24)
{
tm->tm_hour -= 24;
day += 1;
}
else if(tm->tm_hour < 0)
{
tm->tm_hour += 24;
day -= 1;
}
j2date(day, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
if (tm2timestamp(tm, fsec, NULL, &result) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
}
PG_RETURN_TIMESTAMP(result);
}
이상 은 C 언어 시간 대 전환 의 함수 실현 입 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 지역사회 에 가서 토론 을 하 십시오.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 구현 천둥 제거 게임 상세 정보먼저 작은 메뉴를 표시하고 게임을 할지 여부를 선택하십시오.사용자가 종료를 선택하면 프로그램 실행이 끝나고, 사용자가 게임을 선택하면 지뢰 제거 위치 좌표를 입력하라는 메시지가 표시됩니다.사용자가 입력한 좌표가 바둑...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.