1 /* Copyright (c) 2000, 2004-2007 MySQL AB, 2009 Sun Microsystems, Inc.
2    Use is subject to license terms.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    Without limiting anything contained in the foregoing, this file,
16    which is part of C Driver for MySQL (Connector/C), is also subject to the
17    Universal FOSS Exception, version 1.0, a copy of which can be found at
18    http://oss.oracle.com/licenses/universal-foss-exception.
19 
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License, version 2.0, for more details.
24 
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
28 
29 /* Get date in a printable form: yyyy-mm-dd hh:mm:ss */
30 
31 #include "mysys_priv.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 */
48 
49 
get_date(char * to,int flag,time_t date)50 void get_date(char * to, int flag, time_t date)
51 {
52    struct tm *start_time;
53    time_t skr;
54 #if defined(HAVE_LOCALTIME_R) && defined(_REENTRANT)
55   struct tm tm_tmp;
56 #endif
57 
58    skr=date ? (time_t) date : my_time(0);
59 #if defined(HAVE_LOCALTIME_R) && defined(_REENTRANT)
60    if (flag & GETDATE_GMT)
61      gmtime_r(&skr,&tm_tmp);
62    else
63      localtime_r(&skr,&tm_tmp);
64    start_time= &tm_tmp;
65 #else
66    if (flag & GETDATE_GMT)
67      start_time= gmtime(&skr);
68    else
69      start_time= localtime(&skr);
70 #endif
71    if (flag & GETDATE_SHORT_DATE)
72      sprintf(to,"%02d%02d%02d",
73 	     start_time->tm_year % 100,
74 	     start_time->tm_mon+1,
75 	     start_time->tm_mday);
76    else
77      sprintf(to, ((flag & GETDATE_FIXEDLENGTH) ?
78 		  "%4d-%02d-%02d" : "%d-%02d-%02d"),
79 	     start_time->tm_year+1900,
80 	     start_time->tm_mon+1,
81 	     start_time->tm_mday);
82    if (flag & GETDATE_DATE_TIME)
83      sprintf(strend(to),
84 	     ((flag & GETDATE_FIXEDLENGTH) ?
85 	      " %02d:%02d:%02d" : " %2d:%02d:%02d"),
86 	     start_time->tm_hour,
87 	     start_time->tm_min,
88 	     start_time->tm_sec);
89    else if (flag & GETDATE_HHMMSSTIME)
90      sprintf(strend(to),"%02d%02d%02d",
91 	     start_time->tm_hour,
92 	     start_time->tm_min,
93 	     start_time->tm_sec);
94 } /* get_date */
95