Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Lew Pitcher Newsgroups: comp.databases.mysql Subject: Looking for a MYSQL_TIME to struct tm conversion routine Date: Fri, 15 Mar 2024 18:35:37 -0000 (UTC) Organization: A noiseless patient Spider Lines: 83 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 15 Mar 2024 18:35:37 -0000 (UTC) Injection-Info: dont-email.me; posting-host="bf9cc900155a9f125c2c234b6ef07519"; logging-data="2472823"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MbzllMFTCrBMdHHKaeGHGzml4un2DOOQ=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:fOLNWYamuG3ku2GV+FXq7/5NdRU= Xref: csiph.com comp.databases.mysql:7794 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"