|
| 1 | +/* |
| 2 | + * Copyright 2021, Tarantool AUTHORS, please see AUTHORS file. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or |
| 5 | + * without modification, are permitted provided that the following |
| 6 | + * conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above |
| 9 | + * copyright notice, this list of conditions and the |
| 10 | + * following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above |
| 13 | + * copyright notice, this list of conditions and the following |
| 14 | + * disclaimer in the documentation and/or other materials |
| 15 | + * provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 19 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 | + * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | + * SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#include <string.h> |
| 33 | + |
| 34 | +#include "trivia/util.h" |
| 35 | +#include "datetime.h" |
| 36 | + |
| 37 | +int |
| 38 | +datetime_to_string(const struct datetime_t * date, char *buf, uint32_t len) |
| 39 | +{ |
| 40 | + char * src = buf; |
| 41 | + int offset = date->offset; |
| 42 | + int64_t secs = date->secs + offset * 60; |
| 43 | + dt_t dt = dt_from_rdn((secs / SECS_PER_DAY) + 719163); |
| 44 | + |
| 45 | + int year, month, day, sec, ns, sign; |
| 46 | + dt_to_ymd(dt, &year, &month, &day); |
| 47 | + |
| 48 | + int hour = (secs / 3600) % 24, |
| 49 | + minute = (secs / 60) % 60; |
| 50 | + ; |
| 51 | + sec = secs % 60; |
| 52 | + ns = date->nsec; |
| 53 | + uint32_t sz; |
| 54 | + sz = snprintf(buf, len, "%04d-%02d-%02dT%02d:%02d", |
| 55 | + year, month, day, hour, minute); |
| 56 | + buf += sz; len -= sz; |
| 57 | + if (sec || ns) { |
| 58 | + sz = snprintf(buf, len, ":%02d", sec); |
| 59 | + buf += sz; len -= sz; |
| 60 | + if (ns) { |
| 61 | + if ((ns % 1000000) == 0) |
| 62 | + sz = snprintf(buf, len, ".%03d", ns / 1000000); |
| 63 | + else if ((ns % 1000) == 0) |
| 64 | + sz = snprintf(buf, len, ".%06d", ns / 1000); |
| 65 | + else |
| 66 | + sz = snprintf(buf, len, ".%09d", ns); |
| 67 | + buf += sz; len -= sz; |
| 68 | + } |
| 69 | + } |
| 70 | + if (offset == 0) { |
| 71 | + strncpy(buf, "Z", len); |
| 72 | + buf++; |
| 73 | + len--; |
| 74 | + } |
| 75 | + else { |
| 76 | + if (offset < 0) |
| 77 | + sign = '-', offset = -offset; |
| 78 | + else |
| 79 | + sign = '+'; |
| 80 | + |
| 81 | + sz = snprintf(buf, len, "%c%02d:%02d", sign, offset / 60, offset % 60); |
| 82 | + buf += sz; len -= sz; |
| 83 | + } |
| 84 | + return (buf - src); |
| 85 | +} |
0 commit comments