1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 /* Get date in a printable form: yyyy-mm-dd hh:mm:ss */
29 
30 #include "mysys_priv.h"
31 #include "my_sys.h"
32 #include <m_string.h>
33 
34 /*
35   get date as string
36 
37   SYNOPSIS
38     get_date()
39     to   - string where date will be written
40     flag - format of date:
41 	  If flag & GETDATE_TIME	Return date and time
42 	  If flag & GETDATE_SHORT_DATE	Return short date format YYMMDD
43 	  If flag & GETDATE_HHMMSSTIME	Return time in HHMMDD format.
44 	  If flag & GETDATE_GMT		Date/time in GMT
45 	  If flag & GETDATE_FIXEDLENGTH	Return fixed length date/time
46     date - for conversion
47     If flag & GETDATE_T_DELIMITER Append 'T' between date and time.
48     If flag & GETDATE_SHORT_DATE_FULL_YEAR Return compact date format YYYYMMDD
49 */
50 
51 
get_date(char * to,int flag,time_t date)52 void get_date(char * to, int flag, time_t date)
53 {
54    struct tm *start_time;
55    time_t skr;
56   struct tm tm_tmp;
57 
58    skr=date ? (time_t) date : my_time(0);
59    if (flag & GETDATE_GMT)
60      gmtime_r(&skr,&tm_tmp);
61    else
62      localtime_r(&skr,&tm_tmp);
63    start_time= &tm_tmp;
64    if (flag & GETDATE_SHORT_DATE)
65      sprintf(to,"%02d%02d%02d",
66 	     start_time->tm_year % 100,
67 	     start_time->tm_mon+1,
68 	     start_time->tm_mday);
69    else if (flag & GETDATE_SHORT_DATE_FULL_YEAR)
70      sprintf(to,"%4d%02d%02d",
71        start_time->tm_year+1900,
72        start_time->tm_mon+1,
73        start_time->tm_mday);
74    else
75      sprintf(to, ((flag & GETDATE_FIXEDLENGTH) ?
76 		  "%4d-%02d-%02d" : "%d-%02d-%02d"),
77 	     start_time->tm_year+1900,
78 	     start_time->tm_mon+1,
79 	     start_time->tm_mday);
80    if (flag & GETDATE_DATE_TIME)
81    {
82      if (flag & GETDATE_T_DELIMITER)
83        sprintf(strend(to), "T");
84      sprintf(strend(to),
85 	     ((flag & GETDATE_FIXEDLENGTH) ?
86 	      " %02d:%02d:%02d" : " %2d:%02d:%02d"),
87 	     start_time->tm_hour,
88 	     start_time->tm_min,
89 	     start_time->tm_sec);
90    }
91    else if (flag & GETDATE_HHMMSSTIME)
92    {
93      if (flag & GETDATE_T_DELIMITER)
94        sprintf(strend(to), "T");
95      sprintf(strend(to),"%02d%02d%02d",
96 	     start_time->tm_hour,
97 	     start_time->tm_min,
98 	     start_time->tm_sec);
99    }
100 } /* get_date */
101