1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*** dzone.c -- convert date/times between timezones
*
* Copyright (C) 2011-2014 Sebastian Freundt
*
* Author: Sebastian Freundt <freundt@ga-group.nl>
*
* This file is part of dateutils.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include "dt-core.h"
#include "dt-io.h"
#include "dt-core-tz-glue.h"
#include "tzraw.h"
struct ztr_s {
int32_t trns;
int32_t offs;
};
/* fwd decld in tzraw.h */
struct ztrdtl_s {
int32_t offs;
uint8_t dstp;
uint8_t abbr;
} __attribute__((packed));
const char *prog = "dzone";
static char gbuf[256U];
static const char never[] = "never";
static const char nindi[] = " -> ";
static const char pindi[] = " <- ";
static size_t
xstrlcpy(char *restrict dst, const char *src, size_t dsz)
{
size_t ssz = strlen(src);
if (ssz > dsz) {
ssz = dsz - 1U;
}
memcpy(dst, src, ssz);
dst[ssz] = '\0';
return ssz;
}
static int
dz_io_write(struct dt_dt_s d, zif_t zone, const char *name)
{
static const char fmt[] = "%FT%T%Z";
char *restrict bp = gbuf;
const char *const ep = gbuf + sizeof(gbuf);
if (LIKELY(zone != NULL)) {
d = dtz_enrichz(d, zone);
}
bp += dt_strfdt(bp, ep - bp, fmt, d);
/* append name */
if (LIKELY(name != NULL)) {
*bp++ = '\t';
bp += xstrlcpy(bp, name, ep - bp);
}
*bp++ = '\n';
__io_write(gbuf, bp - gbuf, stdout);
return (bp > gbuf) - 1;
}
static size_t
dz_strftr(char *restrict buf, size_t bsz, struct ztr_s t)
{
static const char fmt[] = "%FT%T%Z";
struct dt_dt_s d = dt_dt_initialiser();
d.typ = DT_SEXY;
d.sexy = t.trns + t.offs;
if (t.offs > 0) {
d.zdiff = (uint16_t)(t.offs / ZDIFF_RES);
} else if (t.offs < 0) {
d.neg = 1;
d.zdiff = (uint16_t)(-t.offs / ZDIFF_RES);
}
return dt_strfdt(buf, bsz, fmt, d);
}
static int
dz_write_nxtr(struct zrng_s r, zif_t z, const char *zn)
{
char *restrict bp = gbuf;
const char *const ep = gbuf + sizeof(gbuf);
size_t ntr = zif_ntrans(z);
if (r.next == INT_MAX) {
bp += xstrlcpy(bp, never, bp - ep);
} else {
bp += dz_strftr(bp, ep - bp, (struct ztr_s){r.next, r.offs});
}
/* append next indicator */
bp += xstrlcpy(bp, nindi, bp - ep);
if (r.trno + 1U < ntr) {
/* thank god there's another one */
struct ztrdtl_s zd = zif_trdtl(z, r.trno + 1);
bp += dz_strftr(bp, ep - bp, (struct ztr_s){r.next, zd.offs});
} else {
bp += xstrlcpy(bp, never, bp - ep);
}
/* append name */
if (LIKELY(zn != NULL)) {
*bp++ = '\t';
bp += xstrlcpy(bp, zn, ep - bp);
}
*bp++ = '\n';
__io_write(gbuf, bp - gbuf, stdout);
return (bp > gbuf) - 1;
}
static int
dz_write_prtr(struct zrng_s r, zif_t UNUSED(z), const char *zn)
{
char *restrict bp = gbuf;
const char *const ep = gbuf + sizeof(gbuf);
if (r.trno >= 1) {
/* there's one before that */
struct ztrdtl_s zd = zif_trdtl(z, r.trno - 1);
bp += dz_strftr(bp, ep - bp, (struct ztr_s){r.prev, zd.offs});
} else {
bp += xstrlcpy(bp, never, bp - ep);
}
/* append prev indicator */
bp += xstrlcpy(bp, pindi, bp - ep);
if (r.prev == INT_MIN) {
bp += xstrlcpy(bp, never, bp - ep);
} else {
bp += dz_strftr(bp, ep - bp, (struct ztr_s){r.prev, r.offs});
}
/* append name */
if (LIKELY(zn != NULL)) {
*bp++ = '\t';
bp += xstrlcpy(bp, zn, ep - bp);
}
*bp++ = '\n';
__io_write(gbuf, bp - gbuf, stdout);
return (bp > gbuf) - 1;
}
#include "dzone.yucc"
int
main(int argc, char *argv[])
{
yuck_t argi[1U];
int rc = 0;
zif_t fromz = NULL;
char **fmt;
size_t nfmt;
/* all them zones to consider */
struct {
zif_t zone;
const char *name;
} *z;
size_t nz;
/* all them datetimes to consider */
struct dt_dt_s *d;
size_t nd;
bool trnsp = false;
if (yuck_parse(argi, argc, argv)) {
rc = 1;
goto out;
} else if (argi->nargs == 0U) {
error("Need at least a ZONENAME or a DATE/TIME");
rc = 1;
goto out;
}
/* try and read the from and to time zones */
if (argi->from_zone_arg) {
fromz = dt_io_zone(argi->from_zone_arg);
}
if (argi->next_flag || argi->prev_flag) {
trnsp = true;
}
/* very well then */
fmt = argi->input_format_args;
nfmt = argi->input_format_nargs;
/* initially size the two beef arrays as large as there is input
* we'll then sort them by traversing the input args and ass'ing
* to the one or the other */
nz = 0U;
z = malloc(argi->nargs * sizeof(*z));
nd = 0U;
d = malloc(argi->nargs * sizeof(*d));
for (size_t i = 0U; i < argi->nargs; i++) {
const char *inp = argi->args[i];
/* try dt_strp'ing the input or assume it's a zone */
if (!dt_unk_p(d[nd] = dt_io_strpdt(inp, fmt, nfmt, fromz))) {
if (UNLIKELY(d[nd].fix) && !argi->quiet_flag) {
rc = 2;
}
nd++;
} else if ((z[nz].zone = dt_io_zone(inp)) != NULL) {
z[nz].name = inp;
nz++;
} else if (!argi->quiet_flag) {
/* just bollocks */
error("\
Cannot use `%s', it does not appear to be a zonename\n\
nor a date/time corresponding to the given input formats", inp);
rc = 2;
}
}
/* operate with default zone UTC and default time now */
if (nz == 0U) {
z[nz].zone = NULL;
z[nz].name = NULL;
nz++;
}
if (nd == 0U && !trnsp) {
d[nd++] = dt_datetime((dt_dttyp_t)DT_YMD);
} else if (nd == 0U) {
d[nd++] = dt_datetime((dt_dttyp_t)DT_SEXY);
}
/* just go through them all now */
if (LIKELY(!trnsp)) {
for (size_t i = 0U; !trnsp && i < nd; i++) {
for (size_t j = 0U; j < nz; j++) {
dz_io_write(d[i], z[j].zone, z[j].name);
}
}
} else {
/* otherwise traverse the zones and determine transitions */
for (size_t i = 0U; i < nd; i++) {
struct dt_dt_s di = dt_dtconv(DT_SEXY, d[i]);
for (size_t j = 0U; j < nz; j++) {
const zif_t zj = z[j].zone;
const char *zn = z[j].name;
struct zrng_s r;
if (UNLIKELY(zj == NULL)) {
/* don't bother */
continue;
}
/* otherwise find the range */
r = zif_find_zrng(zj, di.sexy);
if (argi->next_flag) {
dz_write_nxtr(r, zj, zn);
}
if (argi->prev_flag) {
dz_write_prtr(r, zj, zn);
}
}
}
}
/* release the zones */
dt_io_clear_zones();
/* release those arrays */
free(z);
free(d);
if (argi->from_zone_arg) {
zif_close(fromz);
}
out:
yuck_free(argi);
return rc;
}
/* dzone.c ends here */
|