C語言實(shí)現(xiàn)時區(qū)轉(zhuǎn)換函數(shù)的實(shí)例
C語言實(shí)現(xiàn)時區(qū)轉(zhuǎn)換函數(shù)的實(shí)例
時區(qū)轉(zhuǎn)換函數(shù)
功能:
把時區(qū)1的時間轉(zhuǎn)換成時區(qū)2的時間
參數(shù):
arg1 -- 輸入時間
arg2 -- 時區(qū)1(也是arg1當(dāng)前時間所在的時區(qū))
arg3 -- 時區(qū)2(要轉(zhuǎn)換的時區(qū)的時間)
要求:
參數(shù)arg1類型可為timestamp
24個時區(qū)(由1-24表示)
在 pg_proc.h 中添加函數(shù)定義
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/myfuncs.c 中實(shí)現(xiàn)函數(shù)
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); } /** 實(shí)現(xiàn)時區(qū)轉(zhuǎn)換 **/ PG_RETURN_TIMESTAMP(result); }
獲取參數(shù)判斷合法性
思路:
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 定義了相關(guān)結(jié)構(gòu)體 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.h
定義了timestamp 和 pg_tm 的轉(zhuǎn)換方法
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);
timestamp2tm() 第一個參數(shù)是輸入timestamp,第三個是輸出pg_tm,第四個是輸出的小數(shù)秒,其他幾個參數(shù)與時區(qū)相關(guān),第2,5個參數(shù)也是出參,最后一個設(shè)置NULL就可以,表示當(dāng)前會話時區(qū)。
流程:
代碼:
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語言時區(qū)轉(zhuǎn)換的函數(shù)實(shí)現(xiàn),如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法
- C語言實(shí)現(xiàn)動態(tài)順序表的實(shí)現(xiàn)代碼
- C語言實(shí)現(xiàn)靜態(tài)順序表的實(shí)例詳解
- C語言中strlen() strcpy() strcat() strcmp()函數(shù)的實(shí)現(xiàn)方法
- C語言數(shù)據(jù)結(jié)構(gòu) 快速排序?qū)嵗斀?/a>
- C語言實(shí)現(xiàn)俄羅斯方塊小游戲
- C語言模式實(shí)現(xiàn)C++繼承和多態(tài)的實(shí)例代碼
- C語言實(shí)現(xiàn)查看進(jìn)程是否存在的方法示例
相關(guān)文章
C語言字符串函數(shù)操作(strlen,strcpy,strcat,strcmp)詳解
大家好,本篇文章主要講的是C語言字符串函數(shù)操作(strlen,strcpy,strcat,strcmp)詳解,感興趣的同學(xué)趕快來看一看吧2021-12-12函數(shù)外初始化與函數(shù)內(nèi)初始化詳細(xì)解析
函數(shù)內(nèi)初始化:bool FillStr(char *&szDst, int nSize);第一個參數(shù)中的&一定不能少,這是因?yàn)樵诤瘮?shù)外部我們只聲明了這個指針,具體這個指針指向內(nèi)存中的哪個地址我們并不知道,所以&是為了說明傳遞的是這個指針的引用,那么在函數(shù)內(nèi)初始化后這個指針的地址也就是外面指針的地址了2013-09-09詳解如何在VS2019和VScode中配置C++調(diào)用python接口
這篇文章主要介紹了詳解如何在VS2019和VScode中配置C++調(diào)用python接口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12C++算法之海量數(shù)據(jù)處理方法的總結(jié)分析
本篇文章是對海量數(shù)據(jù)處理方法進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下2013-05-05C語言中6組指針和自增運(yùn)算符結(jié)合方式的運(yùn)算順序問題
本文通過代碼實(shí)現(xiàn)分析了6種組合:* p++,(* p)++,* (p++),++* p,++( * p), * (++p),需要的朋友可以參考下2015-07-07C++基礎(chǔ)之this指針與另一種“多態(tài)”
this指針識別了同一個類的不同的對象,換句話說,this指針使得成員函數(shù)可以訪問同一個類的不同對象。再深入一點(diǎn),this指針使得成員函數(shù)會因?yàn)閠his指針的不同而訪問到了不同的成員變量2013-07-07