Drizzled Public API Documentation

mf_getdate.cc
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* Get date in a printable form: yyyy-mm-dd hh:mm:ss */
00017 
00018 #include <config.h>
00019 
00020 #include <drizzled/internal/my_sys.h>
00021 #include <drizzled/internal/m_string.h>
00022 #include <cstdio>
00023 
00024 namespace drizzled
00025 {
00026 namespace internal
00027 {
00028 
00029 /*
00030   get date as string
00031 
00032   SYNOPSIS
00033     get_date()
00034     to   - string where date will be written
00035     flag - format of date:
00036     If flag & GETDATE_TIME  Return date and time
00037     If flag & GETDATE_SHORT_DATE  Return short date format YYMMDD
00038     If flag & GETDATE_HHMMSSTIME  Return time in HHMMDD format.
00039     If flag & GETDATE_GMT   Date/time in GMT
00040     If flag & GETDATE_FIXEDLENGTH Return fixed length date/time
00041     date - for conversion
00042 */
00043 
00044 
00045 void get_date(char * to, int flag, time_t date)
00046 {
00047    tm *start_time;
00048    time_t skr;
00049    struct tm tm_tmp;
00050 
00051    skr= date ? (time_t) date : time(0);
00052    if (flag & GETDATE_GMT)
00053      localtime_r(&skr,&tm_tmp);
00054    else
00055      gmtime_r(&skr,&tm_tmp);
00056    start_time= &tm_tmp;
00057    if (flag & GETDATE_SHORT_DATE)
00058      sprintf(to,"%02d%02d%02d",
00059        start_time->tm_year % 100,
00060        start_time->tm_mon+1,
00061        start_time->tm_mday);
00062    else
00063      sprintf(to, ((flag & GETDATE_FIXEDLENGTH) ?
00064       "%4d-%02d-%02d" : "%d-%02d-%02d"),
00065        start_time->tm_year+1900,
00066        start_time->tm_mon+1,
00067        start_time->tm_mday);
00068    if (flag & GETDATE_DATE_TIME)
00069      sprintf(strchr(to, '\0'),
00070        ((flag & GETDATE_FIXEDLENGTH) ?
00071         " %02d:%02d:%02d" : " %2d:%02d:%02d"),
00072        start_time->tm_hour,
00073        start_time->tm_min,
00074        start_time->tm_sec);
00075    else if (flag & GETDATE_HHMMSSTIME)
00076      sprintf(strchr(to, '\0'),"%02d%02d%02d",
00077        start_time->tm_hour,
00078        start_time->tm_min,
00079        start_time->tm_sec);
00080 } /* get_date */
00081 
00082 } /* namespace internal */
00083 } /* namespace drizzled */