1 | /**************************************************************************** |
2 | Copyright (C) 2013 Monty Program AB |
3 | 2016 MariaDB Corporation AB |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public |
16 | License along with this library; if not see <http://www.gnu.org/licenses> |
17 | or write to the Free Software Foundation, Inc., |
18 | 51 Franklin St., Fifth Floor, Boston, MA 02110, USA |
19 | |
20 | Part of this code includes code from the PHP project which |
21 | is freely available from http://www.php.net |
22 | *****************************************************************************/ |
23 | #include <ma_global.h> |
24 | #include <mysql.h> |
25 | #include <stdio.h> |
26 | |
27 | |
28 | size_t mariadb_time_to_string(const MYSQL_TIME *tm, char *time_str, size_t len, |
29 | unsigned int digits) |
30 | { |
31 | size_t length; |
32 | |
33 | if (!time_str || !len) |
34 | return 0; |
35 | |
36 | if (digits == AUTO_SEC_PART_DIGITS) |
37 | digits= (tm->second_part) ? SEC_PART_DIGITS : 0; |
38 | |
39 | switch(tm->time_type) { |
40 | case MYSQL_TIMESTAMP_DATE: |
41 | length= snprintf(time_str, len, "%04u-%02u-%02u" , tm->year, tm->month, tm->day); |
42 | digits= 0; |
43 | break; |
44 | case MYSQL_TIMESTAMP_DATETIME: |
45 | length= snprintf(time_str, len, "%04u-%02u-%02u %02u:%02u:%02u" , |
46 | tm->year, tm->month, tm->day, tm->hour, tm->minute, tm->second); |
47 | break; |
48 | case MYSQL_TIMESTAMP_TIME: |
49 | length= snprintf(time_str, len, "%s%02u:%02u:%02u" , |
50 | (tm->neg ? "-" : "" ), tm->hour, tm->minute, tm->second); |
51 | break; |
52 | default: |
53 | time_str[0]= '\0'; |
54 | return 0; |
55 | break; |
56 | } |
57 | if (digits && (len < length)) |
58 | { |
59 | char helper[16]; |
60 | snprintf(helper, 16, ".%%0%du" , digits); |
61 | length+= snprintf(time_str + length, len - length, helper, digits); |
62 | } |
63 | return length; |
64 | } |
65 | |
66 | |