Groups | Search | Server Info | Login | Register


Groups > comp.databases.mysql > #7794

Looking for a MYSQL_TIME to struct tm conversion routine

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.databases.mysql
Subject Looking for a MYSQL_TIME to struct tm conversion routine
Date 2024-03-15 18:35 +0000
Organization A noiseless patient Spider
Message-ID <ut24dp$2bern$1@dont-email.me> (permalink)

Show all headers | View raw


Hi, Guys

I'm converting some code that uses the Mysql/Mariadb C api from
text queries to prepared statements, and would like my public
interfaces to use struct tm (Unix "broken-down" time) instead of
MYSQL_TIME.

ISTM that in all the years of the Mysql C API, /someone/ probably
has tackled such an interface. So, instead of "re-inventing the
wheel", I thought that I'd ask around: can you direct me to
or provide the source code for a proper MYSQL_TIME to Unix struct tm
conversion routine?

FWIW, for testing purposes, I wrote my own naive conversion
functions: timeMtoU() (which converts MYSQL_TIME to struct tm),
and timeUtoM() (which converts struct tm to MYSQL_TIME)

I include the source code for these two functions, in case
someone can suggest improvements or bugfixes.

  struct tm *timeMtoU(MYSQL_TIME *mtime, struct tm *utime)
  {
    /*
    ** Note:
    **   MYSQL_TIME year is always a positive value
    **	ranging from year 1000AD to year 9999AD, or
    **	year 0000 for special cases.
    **
    **   MYSQL_TIME month ranges from 1 (January) to 12 (December),
    **	while struct tm tm_mon ranges from 0 (January) to 11 (December)
    **	Subtract 1 from month to get tm_mon
    */
    memset(utime,0,sizeof *utime);
  
    utime->tm_sec  = mtime->second;
    utime->tm_min  = mtime->minute;
    utime->tm_hour = mtime->hour;
    utime->tm_mday = mtime->day;
    utime->tm_mon  = mtime->month - 1;
    utime->tm_year = mtime->year - 1900;

    utime->tm_isdst = -1;	/* let time functions figure it out */

    /*
    ** NB: we naively leave tm_wday and tm_yday set to 0.
    ** If the caller /requires/ a valid tm_wday and/or tm_yday
    ** it's going to have to manipulate the struct tm itself
    */

    return utime;
  }

  MYSQL_TIME *timeUtoM(struct tm *utime, MYSQL_TIME *mtime)
  {
    /*
    ** NOTE:
    **  struct tm tm_year is number of years (+ve or -ve) from
    **	the year 1900. So, the year 1899 is tm_year = -1,
    **	and year 1901 is tm_year = 1.
    **
    **  struct tm tm_mon ranges from 0 (January) to 11 (December)
    **	while MYSQL_TIME month ranges from 1 (January) to 12 (December)
    **	Add 1 to tm_mon to get month
    */
    memset(mtime,0,sizeof *mtime);

    mtime->year		= 1900 + utime->tm_year;  /* error before 1000AD */ 
    mtime->month	= utime->tm_mon + 1;
    mtime->day		= utime->tm_mday;
    mtime->hour		= utime->tm_hour;
    mtime->minute	= utime->tm_min;
    mtime->second	= utime->tm_sec;
    mtime->neg		= 0;			  /* never negative time */
    mtime->second_part	= 0;

    return mtime;
  }


Thanks in advance
-- 
Lew Pitcher
"In Skills We Trust"

Back to comp.databases.mysql | Previous | Next | Find similar


Thread

Looking for a MYSQL_TIME to struct tm conversion routine Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-03-15 18:35 +0000

csiph-web