ecpg-pgtypes.zh.md 25.8 KB
Newer Older
李少辉-开发者's avatar
李少辉-开发者 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
## 36.6. pgtypes Library

[36.6.1. Character Strings](ecpg-pgtypes.html#ECPG-PGTYPES-CSTRINGS)

[36.6.2. The numeric Type](ecpg-pgtypes.html#ECPG-PGTYPES-NUMERIC)

[36.6.3. The date Type](ecpg-pgtypes.html#ECPG-PGTYPES-DATE)

[36.6.4. The timestamp Type](ecpg-pgtypes.html#ECPG-PGTYPES-TIMESTAMP)

[36.6.5. The interval Type](ecpg-pgtypes.html#ECPG-PGTYPES-INTERVAL)

[36.6.6. The decimal Type](ecpg-pgtypes.html#ECPG-PGTYPES-DECIMAL)

[36.6.7. errno Values of pgtypeslib](ecpg-pgtypes.html#ECPG-PGTYPES-ERRNO)

[36.6.8. Special Constants of pgtypeslib](ecpg-pgtypes.html#ECPG-PGTYPES-CONSTANTS)

The pgtypes library maps PostgreSQL database types to C equivalents that can be used in C programs. It also offers functions to do basic calculations with those types within C, i.e., without the help of the PostgreSQL server. See the following example:

```
EXEC SQL BEGIN DECLARE SECTION;
   date date1;
   timestamp ts1, tsout;
   interval iv1;
   char *out;
EXEC SQL END DECLARE SECTION;

PGTYPESdate_today(&date1);
EXEC SQL SELECT started, duration INTO :ts1, :iv1 FROM datetbl WHERE d=:date1;
PGTYPEStimestamp_add_interval(&ts1, &iv1, &tsout);
out = PGTYPEStimestamp_to_asc(&tsout);
printf("Started + duration: %s\n", out);
PGTYPESchar_free(out);
```

### 36.6.1. Character Strings

Some functions such as`PGTYPESnumeric_to_asc`return a pointer to a freshly allocated character string. These results should be freed with`PGTYPESchar_free`instead of`free`. (This is important only on Windows, where memory allocation and release sometimes need to be done by the same library.)

### 36.6.2. The numeric Type

The numeric type offers to do calculations with arbitrary precision. See[Section 8.1](datatype-numeric.html)for the equivalent type in the PostgreSQL server. Because of the arbitrary precision this variable needs to be able to expand and shrink dynamically. That's why you can only create numeric variables on the heap, by means of the`PGTYPESnumeric_new`and`PGTYPESnumeric_free`functions. The decimal type, which is similar but limited in precision, can be created on the stack as well as on the heap.

The following functions can be used to work with the numeric type:

`PGTYPESnumeric_new`

Request a pointer to a newly allocated numeric variable.

```
numeric *PGTYPESnumeric_new(void);
```

`PGTYPESnumeric_free`

释放一个数字类型,释放其所有内存。

```
void PGTYPESnumeric_free(numeric *var);
```

`PGTYPESnumeric_from_asc`

从其字符串表示法解析数字类型。

```
numeric *PGTYPESnumeric_from_asc(char *str, char **endptr);
```

有效格式例如:`-2`,`.794`,`+3.44`,`592.49E07`要么`-32.84e-4`.如果可以成功解析该值,则返回一个有效指针,否则返回 NULL 指针。目前 ECPG 总是解析完整的字符串,因此目前不支持将第一个无效字符的地址存储在`*endptr`.您可以安全地设置`结束点`为空。

`PGTYPESnumeric_to_asc`

返回指向由分配的字符串的指针`malloc`包含数字类型的字符串表示`数`.

```
char *PGTYPESnumeric_to_asc(numeric *num, int dscale);
```

数值将打印为`缩放`十进制数字,必要时进行四舍五入。结果必须被释放`PGTYPESchar_free()`.

`PGTYPESnumeric_add`

将两个数值变量添加到第三个变量中。

```
int PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result);
```

函数添加变量`变量1``变量2`进入结果变量`结果`.该函数在成功时返回 0,在出错时返回 -1。

`PGTYPESnumeric_sub`

减去两个数值变量并在第三个变量中返回结果。

```
int PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result);
```

函数减去变量`变量2`从变量`变量1`.运算结果存储在变量中`结果`.该函数在成功时返回 0,在出错时返回 -1。

`PGTYPESnumeric_mul`

将两个数值变量相乘并在第三个变量中返回结果。

```
int PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result);
```

该函数将变量相乘`变量1``变量2`.运算结果存储在变量中`结果`.该函数在成功时返回 0,在出错时返回 -1。

`PGTYPESnumeric_div`

将两个数值变量相除并在第三个变量中返回结果。

```
int PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result);
```

函数除变量`变量1`经过`变量2`.运算结果存储在变量中`结果`.该函数在成功时返回 0,在出错时返回 -1。

`PGTYPESnumeric_cmp`

比较两个数值变量。

```
int PGTYPESnumeric_cmp(numeric *var1, numeric *var2)
```

此函数比较两个数值变量。万一出错,`INT_MAX`被退回。成功时,该函数返回三个可能的结果之一:

-   1,如果`变量1`大于`变量2`

-   \-1,如果`变量1`小于`变量2`

-   0,如果`变量1``变量2`相等

`PGTYPESnumeric_from_int`

将 int 变量转换为数值变量。

```
int PGTYPESnumeric_from_int(signed int int_val, numeric *var);
```

此函数接受带符号的 int 类型的变量并将其存储在数值变量中`变量`.成功时返回 0,失败时返回 -1。

`PGTYPESnumeric_from_long`

将 long int 变量转换为数值变量。

```
int PGTYPESnumeric_from_long(signed long int long_val, numeric *var);
```

该函数接受一个有符号长整数类型的变量并将其存储在数值变量中`变量`.成功时返回 0,失败时返回 -1。

`PGTYPESnumeric_copy`

将一个数值变量复制到另一个变量中。

```
int PGTYPESnumeric_copy(numeric *src, numeric *dst);
```

此函数复制变量的值`源代码`指向变量`夏令时`指着。成功时返回 0,发生错误时返回 -1。

`PGTYPESnumeric_from_double`

将 double 类型的变量转换为数值。

```
int  PGTYPESnumeric_from_double(double d, numeric *dst);
```

该函数接受一个 double 类型的变量并将结果存储在`夏令时`指着。成功时返回 0,发生错误时返回 -1。

`PGTYPESnumeric_to_double`

将数字类型的变量转换为双精度。

```
int PGTYPESnumeric_to_double(numeric *nv, double *dp)
```

该函数将数值从变量转换为`nv`指向双变量`dp`指着。成功时返回 0,如果发生错误(包括溢出)则返回 -1。溢出时,全局变量`错误号`将设置为`PGTYPES_NUM_OVERFLOW`此外。

`PGTYPESnumeric_to_int`

将 numeric 类型的变量转换为 int。

```
int PGTYPESnumeric_to_int(numeric *nv, int *ip);
```

该函数将数值从变量转换为`nv`指向整数变量`ip`指着。成功时返回 0,如果发生错误(包括溢出)则返回 -1。溢出时,全局变量`错误号`将设置为`PGTYPES_NUM_OVERFLOW`此外。

`PGTYPESnumeric_to_long`

将数值类型的变量转换为长整型。

```
int PGTYPESnumeric_to_long(numeric *nv, long *lp);
```

该函数将数值从变量转换为`nv`指向长整型变量`lp`指着。成功时返回 0,如果发生错误(包括溢出)则返回 -1。溢出时,全局变量`错误号`将设置为`PGTYPES_NUM_OVERFLOW`此外。

`PGTYPESnumeric_to_decimal`

将数字类型的变量转换为十进制。

```
int PGTYPESnumeric_to_decimal(numeric *src, decimal *dst);
```

该函数将数值从变量转换为`源代码`指向十进制变量`夏令时`指着。成功时返回 0,如果发生错误(包括溢出)则返回 -1。溢出时,全局变量`错误号`将设置为`PGTYPES_NUM_OVERFLOW`此外。

`PGTypesUnmeric_from_decimal`

将十进制类型的变量转换为数字。

```
int PGTYPESnumeric_from_decimal(decimal *src, numeric *dst);
```

该函数用于转换变量的十进制值`src`指向`dst`指向。成功时返回0,发生错误时返回-1。由于decimal类型是作为数值类型的有限版本实现的,因此此转换不会发生溢出。

### 36.6.3.日期类型

C中的日期类型使程序能够处理SQL类型的日期数据。看见[第8.5节](datatype-datetime.html)用于PostgreSQL server中的等效类型。

以下函数可用于处理日期类型:

`PGTYPESdate_from_时间戳`

从时间戳中提取日期部分。

```
date PGTYPESdate_from_timestamp(timestamp dt);
```

函数接收一个时间戳作为其唯一的参数,并从该时间戳返回提取的日期部分。

`PGTYPESdate_来自asc`

从文本表示中解析日期。

```
date PGTYPESdate_from_asc(char *str, char **endptr);
```

该函数接收一个C字符\*一串`str`以及指向C字符的指针\*一串`endptr`。目前,ECPG总是解析完整的字符串,因此它目前不支持将第一个无效字符的地址存储在`*endptr`.你可以安全设置`endptr`为空。

请注意,该函数始终假定MDY格式的日期,并且在ECPG中当前没有变量可以更改该日期。

[表36.2](ecpg-pgtypes.html#ECPG-PGTYPESDATE-FROM-ASC-TABLE)显示允许的输入格式。

**表36.2。有效的输入格式`PGTYPESdate_来自asc`**

| 输入 | 后果 |
| --- | --- |
| `1999年1月8日` | `1999年1月8日` |
| `1999-01-08` | `1999年1月8日` |
| `1/8/1999` | `1999年1月8日` |
| `1/18/1999` | `1999年1月18日` |
| `01/02/03` | `2003年2月1日` |
| `1999-1-08` | `1999年1月8日` |
| `1999年1月8日` | `1999年1月8日` |
| `08-Jan-1999` | `1999年1月8日` |
| `99-01-08` | `1999年1月8日` |
| `1999年1月8日` | `1999年1月8日` |
| `08-01-06` | `2006年1月8日` |
| `1999年1月8日` | `1999年1月8日` |
| `19990108` | `ISO 8601;1999年1月8日` |
| `990108` | `ISO 8601;1999年1月8日` |
| `1999.008` | `年复一日` |
| `J2451187` | `朱利安日` |
| `公元前99年1月8日` | `普通时代前99年` |

`PGTYPESdate_至_asc`

返回日期变量的文本表示形式。

```
char *PGTYPESdate_to_asc(date dDate);
```

函数接收日期`达特`作为它唯一的参数。它将在表单中输出日期`1999-01-18`,即在`YYYY-MM-DD`总体安排结果必须用`PGTYPESchar_free()`.

`PGTYPESdate_julmdy`

从日期类型的变量中提取日期、月份和年份的值。

```
void PGTYPESdate_julmdy(date d, int *mdy);
```

函数接收日期`d`以及指向3个整数值数组的指针`麦迪`.变量名表示顺序:`mdy[0]`将设置为包含月份的编号,`mdy[1]`将设置为当天的值`mdy[2]`将包含全年。

`PGTYPESdate_mdyjul`

从指定日期的日期、月份和年份的3个整数数组中创建日期值。

```
void PGTYPESdate_mdyjul(int *mdy, date *jdate);
```

函数接收3个整数的数组(`麦迪`)作为它的第一个参数和第二个参数,一个指向date类型的变量的指针,该变量应该保存操作的结果。

`PGTYPESdate_dayofweek`

为日期值返回一个代表一周中某一天的数字。

```
int PGTYPESdate_dayofweek(date d);
```

函数接收日期变量`d`作为其唯一参数,并返回一个整数,该整数指示该日期的星期几。

-   0-周日

-   1-星期一

-   2-星期二

-   3-星期三

-   4-周四

-   5-周五

-   6-周六

`今天是你的生日`

获取当前日期。

```
void PGTYPESdate_today(date *d);
```

函数接收指向日期变量的指针(`d`)将其设置为当前日期。

`PGTYPESdate_fmt_asc`

使用格式掩码将日期类型的变量转换为其文本表示形式。

```
int PGTYPESdate_fmt_asc(date dDate, char *fmtstring, char *outbuf);
```

函数接收要转换的日期(`达特`),格式掩码(`fmtstring`)以及保存日期文本表示形式的字符串(`爆发`).

成功时,将返回0,如果发生错误,则返回负值。

以下文字是可以使用的字段说明符:

-   `dd`-一个月中某一天的数字。

-   `嗯`-一年中月份的编号。

-   `yy`-一年中的数字为两位数。

-   `年份`-以四位数表示的年份编号。

-   `ddd`-一天的名字(缩写)。

-   `嗯`-月份的名称(缩写)。

    所有其他字符以1:1的比例复制到输出字符串中。

[表36.3](ecpg-pgtypes.html#ECPG-PGTYPESDATE-FMT-ASC-EXAMPLE-TABLE)表示几种可能的格式。这将让你了解如何使用这个功能。所有输出行均基于同一日期:1959年11月23日。

**表36.3。有效的输入格式`PGTYPESdate_fmt_asc`**

| 总体安排 | 后果 |
| ---- | --- |
| `嗯` | `112359` |
| `ddmmyy` | `231159` |
| `yymmdd` | `591123` |
| `yy/mm/dd` | `59/11/23` |
| `yy mm dd` | `59 11 23` |
| `yy.mm.dd` | `59.11.23` |
| `.mm.yyyy.dd.` | `.11.1959.23.` |
| `mmm. dd, yyyy` | `Nov. 23, 1959` |
| `mmm dd yyyy` | `Nov 23 1959` |
| `yyyy dd mm` | `1959 23 11` |
| `ddd, mmm. dd, yyyy` | `Mon, Nov. 23, 1959` |
| `(ddd) mmm. dd, yyyy` | `(Mon) Nov. 23, 1959` |

`PGTYPESdate_defmt_asc`

Use a format mask to convert a C`char*`string to a value of type date.

```
int PGTYPESdate_defmt_asc(date *d, char *fmt, char *str);
```

The function receives a pointer to the date value that should hold the result of the operation (`d`), the format mask to use for parsing the date (`fmt`) and the C char\*包含日期文本表示形式的字符串(`str`).文本表示形式应与格式掩码匹配。但是,不需要将字符串与格式掩码进行1:1映射。该函数只分析顺序并查找文字`yy``年份`这表明了今年的形势,`嗯`表示月份的位置,以及`dd`指示当天的位置。

[表36.4](ecpg-pgtypes.html#ECPG-RDEFMTDATE-EXAMPLE-TABLE)表示几种可能的格式。这将让你了解如何使用这个功能。

**表36.4。有效的输入格式`rdefmtdate`**

| 总体安排 | 一串 | 后果 |
| ---- | --- | --- |
| `ddmmyy` | `21-2-54` | `1954-02-21` |
| `ddmmyy` | `2-12-54` | `1954-12-02` |
| `ddmmyy` | `20111954` | `1954-11-20` |
| `ddmmyy` | `130464` | `1964-04-13` |
| `mmm.dd.yyyy` | `MAR-12-1967` | `1967-03-12` |
| `yy/mm/dd` | `1954, February 3rd` | `1954-02-03` |
| `mmm.dd.yyyy` | `041269` | `1969-04-12` |
| `yy/mm/dd` | `In the year 2525, in the month of July, mankind will be alive on the 28th day` | `2525-07-28` |
| `dd-mm-yy` | `I said on the 28th of July in the year 2525` | `2525-07-28` |
| `mmm.dd.yyyy` | `9/14/58` | `1958-09-14` |
| `yy/mm/dd` | `47/03/29` | `1947-03-29` |
| `mmm.dd.yyyy` | `oct 28 1975` | `1975-10-28` |
| `mmddyy` | `Nov 14th, 1985` | `1985-11-14` |

### 36.6.4. The timestamp Type

The timestamp type in C enables your programs to deal with data of the SQL type timestamp. See[第8.5节](datatype-datetime.html)用于PostgreSQL server中的等效类型。

以下函数可用于处理时间戳类型:

`PGTYPEStimestamp_来自asc`

将时间戳从其文本表示形式解析为时间戳变量。

```
timestamp PGTYPEStimestamp_from_asc(char *str, char **endptr);
```

函数接收要分析的字符串(`str`)以及指向C字符的指针\* (`endptr`)。目前,ECPG总是解析完整的字符串,因此它目前不支持将第一个无效字符的地址存储在`*endptr`.你可以安全设置`endptr`为空。

函数在成功时返回解析的时间戳。一旦出错,`PGTYPESInvalidTimestamp`返回并`呃不`即将`PGTYPES_TS_BAD_时间戳`看见[`PGTYPESInvalidTimestamp`](ecpg-pgtypes.html#PGTYPESINVALIDTIMESTAMP)有关此值的重要说明。

通常,输入字符串可以包含允许的日期规范、空白字符和允许的时间规范的任意组合。请注意,ECPG不支持时区。它可以解析它们,但不会像PostgreSQL server那样应用任何计算。时区说明符被悄悄地丢弃。

[表36.5](ecpg-pgtypes.html#ECPG-PGTYPESTIMESTAMP-FROM-ASC-EXAMPLE-TABLE)包含几个输入字符串的示例。

**表36.5。有效的输入格式`PGTYPEStimestamp_来自asc`**

| 输入 | 后果 |
| --- | --- |
| `1999-01-08 04:05:06` | `1999-01-08 04:05:06` |
| `太平洋标准时间1999年1月8日04:05:06` | `1999-01-08 04:05:06` |
| `1999年1月8日04:05:06.789-8` | `1999-01-08 04:05:06.789(忽略时区说明符)` |
| `J2451187 04:05-08:00` | `1999-01-08 04:05:00(忽略时区说明符)` |

`PGTYPEStimestamp_至_asc`

将日期转换为C字符\*一串

```
char *PGTYPEStimestamp_to_asc(timestamp tstamp);
```

函数接收时间戳`坦普`并返回一个已分配的字符串,该字符串包含时间戳的文本表示形式。结果必须用`PGTYPESchar_free()`.

`PGTYPEStimestamp_电流`

检索当前时间戳。

```
void PGTYPEStimestamp_current(timestamp *ts);
```

函数检索当前时间戳并将其保存到`ts`指向。

`PGTYPEStimestamp_fmt_asc`

将时间戳变量转换为C字符\*使用格式掩码。

```
int PGTYPEStimestamp_fmt_asc(timestamp *ts, char *output, int str_len, char *fmtstr);
```

函数接收指向要转换的时间戳的指针作为其第一个参数(`ts`),指向输出缓冲区的指针(`输出`),为输出缓冲区分配的最大长度(`斯特鲁伦`)以及用于转换的格式掩码(`fmtstr`).

成功后,如果发生错误,函数将返回0和负值。

您可以对格式掩码使用以下格式说明符。格式说明符与`strftime`函数在libc中。任何非格式说明符都将被复制到输出缓冲区。

-   `%A`-由工作日全名的国家代表性替换。

-   `%a`-由缩写的工作日名称的国家代表代替。

-   `%B`-由全国代表性的月份全名代替。

-   `%b`-替换为缩写月份名称的国家代表。

-   `%C`-被(年份/100)替换为十进制数;一位数前加一个零。

-   `%c`-由时间和日期的国家代表代替。

-   `%D`-相当于`%m/%d/%y`.

-   `%d`-以十进制数字(01–31)的形式替换为月份的日期。

-   `%E*` `%O*`-POSIX本地扩展。序列`%欧共体` `%欧共体` `%前任` `%前任` `%嗯` `%嗯` `%来自` `%Oe` `%哦` `%哎` `%嗯` `%嗯` `%操作系统` `%欧` `%欧` `%奥夫` `%哦` `%哦` `%哦`都应该提供其他的表述。

    另外`%OB`实现用于表示替代月份名称(单独使用,未提及日期)。

-   `%e`-被月日替换为十进制数(1-31);单个数字前面有一个空格。

-   `%F`-相当于`%Y-%m-%d`.

-   `%G`-被一年替换为带世纪的十进制数字。今年是一周中大部分时间的一年(周一是一周的第一天)。

-   `%g`-替换为中的同一年`%G`,但作为不带世纪的十进制数(00–99)。

-   `%H`-被小时(24小时时钟)替换为十进制数字(00–23)。

-   `%h`-和`%b`.

-   `%我`-被小时(12小时时钟)替换为十进制数字(01–12)。

-   `%j`-被一年中的某一天替换为十进制数(001–366)。

-   `%k`-被小时(24小时时钟)替换为十进制数字(0-23);单个数字前面有一个空格。

-   `%l`-被小时(12小时时钟)替换为十进制数字(1-12);单个数字前面有一个空格。

-   `%M`-被分钟替换为十进制数字(00–59)。

-   `%m`-被月份替换为十进制数字(01–12)。

-   `%n`-被一条新线取代。

-   `%O*`-和`%E*`.

-   `%p`-由“前梅里迪姆”或“后梅里迪姆”的国家代表(视情况而定)取代。

-   `%R`-相当于`%H:%M`.

-   `%r`-相当于`%I:%M:%S%p`.

-   `%S`-替换为第二个十进制数(00–60)。

-   `%s`-替换为自纪元UTC起的秒数。

-   `%T`-相当于`%H:%M:%S`

-   `%t`-替换为选项卡。

-   `%U`-被一年中的周数(星期日为一周的第一天)替换为十进制数(00–53)。

-   `%u`-被工作日(周一为一周的第一天)替换为十进制数字(1-7)。

-   `%五`-被一年中的周数(星期一为一周的第一天)替换为十进制数(01–53)。如果包含1月1日的一周在新的一年中有四天或更多天,则为第1周;否则这是上一年的最后一周,下一周是第1周。

-   `%五`-相当于`%e-%b-%Y`.

-   `%W`-被一年中的周数(星期一为一周的第一天)替换为十进制数(00–53)。

-   `%w`-被工作日(星期日为一周的第一天)替换为十进制数(0–6)。

-   `%X`-被当时的国家代表所取代。

-   `%x`-替换为日期的国家代表。

-   `%Y`-替换为以世纪为十进制数的年份。

-   `%y`-被无世纪的年份替换为十进制数(00–99)。

-   `%Z`-将替换为时区名称。

-   `%z`-替换为UTC的时区偏移量;前面的加号代表UTC以东,减号代表UTC以西,小时和分钟后面各有两个数字,中间没有分隔符(通用格式为[RFC 822](https://tools.ietf.org/html/rfc822)日期标题)。

-   `%+`-替换为日期和时间的国家代表。

-   `%-*`-GNU libc扩展。执行数字输出时,不要进行任何填充。

-   $\_\*-GNU libc扩展。显式指定填充空间。

-   `%0*`-GNU libc扩展。为填充显式指定零。

-   `%%`-被替换为`%`.

`PGTYPEStimestamp_sub`

从另一个时间戳中减去一个时间戳,并将结果保存在interval类型的变量中。

```
int PGTYPEStimestamp_sub(timestamp *ts1, timestamp *ts2, interval *iv);
```

该函数将减去`ts2`从timestamp变量中指向`ts1`指向并将结果存储在`四、`指向。

成功后,如果发生错误,函数将返回0和负值。

`PGTYPEStimestamp_defmt_asc`

使用格式掩码从文本表示中解析时间戳值。

```
int PGTYPEStimestamp_defmt_asc(char *str, char *fmt, timestamp *d);
```

函数接收变量中时间戳的文本表示`str`以及在变量中使用的格式掩码`fmt`。结果将存储在`d`指向。

如果格式化掩码`fmt`如果为空,函数将返回默认的格式掩码,即`%Y-%m-%d%H:%m:%S`.

这是与之相反的功能[`PGTYPEStimestamp_fmt_asc`](ecpg-pgtypes.html#PGTYPESTIMESTAMPFMTASC)。请参阅此处的文档,以了解可能的格式掩码条目。

`PGTYPEStimestamp_添加_间隔`

将间隔变量添加到时间戳变量。

```
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout);
```

函数接收指向时间戳变量的指针`锡`以及一个指向区间变量的指针`跨度`。它将间隔添加到时间戳,并将生成的时间戳保存在`吹牛`指向。

成功后,如果发生错误,函数将返回0和负值。

`PGTYPEStimestamp_sub_间隔`

从时间戳变量中减去间隔变量。

```
int PGTYPEStimestamp_sub_interval(timestamp *tin, interval *span, timestamp *tout);
```

该函数减去`跨度`从timestamp变量中指向`锡`指向并将结果保存到`吹牛`指向。

成功后,如果发生错误,函数将返回0和负值。

### 36.6.5.区间型

C中的interval类型使程序能够处理SQL类型interval的数据。看见[第8.5节](datatype-datetime.html)用于PostgreSQL server中的等效类型。

以下功能可用于处理间隔类型:

`PGTYPESinterval_new`

返回指向新分配的间隔变量的指针。

```
interval *PGTYPESinterval_new(void);
```

`PGTypesInTurval_free`

释放之前分配的间隔变量的内存。

```
void PGTYPESinterval_free(interval *intvl);
```

`PGTYPES从asc接收`

从文本表示中解析一个区间。

```
interval *PGTYPESinterval_from_asc(char *str, char **endptr);
```

The function parses the input string`str`and returns a pointer to an allocated interval variable. At the moment ECPG always parses the complete string and so it currently does not support to store the address of the first invalid character in`*endptr`. You can safely set`endptr`to NULL.

`PGTYPESinterval_to_asc`

Convert a variable of type interval to its textual representation.

```
char *PGTYPESinterval_to_asc(interval *span);
```

The function converts the interval variable that`span`points to into a C char\*. The output looks like this example:`@ 1 day 12 hours 59 mins 10 secs`. The result must be freed with`PGTYPESchar_free()`.

`PGTYPESinterval_copy`

Copy a variable of type interval.

```
int PGTYPESinterval_copy(interval *intvlsrc, interval *intvldest);
```

The function copies the interval variable that`intvlsrc`points to into the variable that`intvldest`points to. Note that you need to allocate the memory for the destination variable before.

### 36.6.6. The decimal Type

The decimal type is similar to the numeric type. However it is limited to a maximum precision of 30 significant digits. In contrast to the numeric type which can be created on the heap only, the decimal type can be created either on the stack or on the heap (by means of the functions`PGTYPESdecimal_new`and`PGTYPESdecimal_free`)。在中描述的Informix兼容模式中,还有许多其他函数处理十进制类型[第36.15节](ecpg-informix-compat.html).

以下函数可用于处理decimal类型,并且不仅包含在`利比卡特`图书馆

`PGTYPES CIMAL_新`

请求指向新分配的十进制变量的指针。

```
decimal *PGTYPESdecimal_new(void);
```

`PGTypesCIMAL_免费`

释放十进制类型,释放其所有内存。

```
void PGTYPESdecimal_free(decimal *var);
```

### 36.6.7.pgtypeslib的errno值

`PGTYPES_NUM_BAD_NUMERIC`

一个参数应该包含一个数值变量(或指向一个数值变量),但实际上它在内存中的表示是无效的。

`PGTYPES_NUM_溢出`

发生溢出。由于数值类型可以处理几乎任意的精度,因此将数值变量转换为其他类型可能会导致溢出。

`PGTYPES_NUM_下溢`

出现下溢。由于数值类型可以处理几乎任意的精度,因此将数值变量转换为其他类型可能会导致下溢。

`PGTYPES_NUM_DIVIDE_ZERO`

已尝试用零除。

`PGTYPES_DATE_BAD_DATE`

传递给服务器的日期字符串无效`PGTYPESdate_来自asc`作用

`PGTYPES_DATE_ERR_EARGS`

已将无效参数传递给`PGTYPESdate_defmt_asc`作用

`PGTYPES_DATE_ERR_ENOSHORTDATE`

服务器在输入字符串中找到无效令牌`PGTYPESdate_defmt_asc`作用

`PGTYPES_INTVL_BAD_间隔`

传递给服务器的间隔字符串无效`PGTYPES从asc接收`函数,或向`PGTYPESinterval_to_asc`作用

`PGTYPES_DATE_ERR_ENOTDMY`

项目中的日/月/年分配不匹配`PGTYPESdate_defmt_asc`作用

`PGTYPES_DATE_BAD_DAY`

用户发现一个无效的月天数值`PGTYPESdate_defmt_asc`作用

`PGTYPES_DATE_BAD_MONTH`

用户发现一个无效的月份值`PGTYPESdate_defmt_asc`作用

`PGTYPES_TS_BAD_时间戳`

传递给的时间戳字符串无效`PGTYPEStimestamp_来自asc`函数,或将无效的时间戳值传递给`PGTYPEStimestamp_至_asc`作用

`PGTYPES_TS_ERR_EINFTIME`

在无法处理的上下文中遇到无限时间戳值。

### 36.6.8.pgtypeslib的特殊常数

`PGTYPESInvalidTimestamp`

timestamp类型的值表示无效的时间戳。这是由函数返回的`PGTYPEStimestamp_来自asc`解析错误。请注意,由于`时间戳`数据类型,`PGTYPESInvalidTimestamp`同时也是一个有效的时间戳。设定为`1899-12-31 23:59:59`。为了检测错误,请确保您的应用程序不仅测试`PGTYPESInvalidTimestamp`也是为了`呃,不0`每次打电话给`PGTYPEStimestamp_来自asc`.