C語言實現(xiàn)時區(qū)轉(zhuǎn)換函數(shù)的實例
C語言實現(xiàn)時區(qū)轉(zhuǎn)換函數(shù)的實例
時區(qū)轉(zhuǎn)換函數(shù)
功能:
把時區(qū)1的時間轉(zhuǎn)換成時區(qū)2的時間
參數(shù):
arg1 -- 輸入時間
arg2 -- 時區(qū)1(也是arg1當前時間所在的時區(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 中實現(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); } /** 實現(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就可以,表示當前會話時區(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ù)實現(xiàn),如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
下一篇:C++數(shù)據(jù)精度問題(對浮點數(shù)保存指定位小數(shù))
本文標題:C語言實現(xiàn)時區(qū)轉(zhuǎn)換函數(shù)的實例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1260.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置