zsh-workers
 help / color / mirror / Atom feed
085e4cc267ffbbfea5d7c86aebcc83a7b1e915ef blob 7519 bytes (raw)

  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
 
/*
 * datetime.c - parameter and command interface to date and time utilities
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 2002 Peter Stephenson, Clint Adams
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Peter Stephenson, Clint Adams or the Zsh Development Group
 * be liable to any party for direct, indirect, special, incidental, or
 * consequential damages arising out of the use of this software and its
 * documentation, even if Peter Stephenson, Clint Adams and the Zsh
 * Development Group have been advised of the possibility of such damage.
 *
 * Peter Stephenson, Clint Adams and the Zsh Development Group specifically
 * disclaim any warranties, including, but not limited to, the implied
 * warranties of merchantability and fitness for a particular purpose.
 * The software provided hereunder is on an "as is" basis, and Peter
 * Stephenson, Clint Adams and the Zsh Development Group have no obligation
 * to provide maintenance, support, updates, enhancements, or modifications.
 *
 */

#include "datetime.mdh"
#include "datetime.pro"
#include <time.h>

#ifndef HAVE_MKTIME
#ifdef HAVE_TIMELOCAL
#define	mktime(x)	timelocal(x)
#define HAVE_MKTIME	1
#endif
#endif

static int
reverse_strftime(char *nam, char **argv, char *scalar, int quiet)
{
#if defined(HAVE_STRPTIME) && defined(HAVE_MKTIME)
    struct tm tm;
    zlong mytime;
    char *endp;

    /*
     * Initialise all parameters to zero; there's no floating point
     * so memset() will do the trick.  The exception is that tm_isdst
     * is set to -1 which, if not overridden, will cause mktime()
     * to use the current timezone.  This is probably the best guess;
     * it's the one that will cause dates and times output by strftime
     * without the -r option and without an explicit timezone to be
     * converted back correctly. Additionally, tm_mday is set to 1
     * as that and not 0 corresponds to the first of the month.
     */
    (void)memset(&tm, 0, sizeof(tm));
    tm.tm_isdst = -1;
    tm.tm_mday = 1;
    endp = strptime(argv[1], argv[0], &tm);

    if (!endp) {
	/* Conversion failed completely. */
	if (!quiet)
	    zwarnnam(nam, "format not matched");
	return 1;
    }

    mytime = (zlong)mktime(&tm);

    if (scalar)
	setiparam(scalar, mytime);
    else {
	char buf[DIGBUFSIZE];
	convbase(buf, mytime, 10);
	printf("%s\n", buf);
    }

    if (*endp && !quiet) {
	/*
	 * Not everything in the input string was converted.
	 * This is probably benign, since the format has been satisfied,
	 * but issue a warning unless the quiet flag is set.
	 */
	zwarnnam(nam, "warning: input string not completely matched");
    }

    return 0;
#else
    if (!quiet)
	zwarnnam(nam, "not implemented on this system");
    return 2;
#endif
}

static int
output_strftime(char *nam, char **argv, Options ops, UNUSED(int func))
{
    int bufsize, x, len;
    char *endptr = NULL, *scalar = NULL, *buffer;
    struct tm *tm;
    struct timespec ts;

    if (OPT_ISSET(ops,'s')) {
	scalar = OPT_ARG(ops, 's');
	if (!isident(scalar)) {
	    zwarnnam(nam, "not an identifier: %s", scalar);
	    return 1;
	}
    }
    if (OPT_ISSET(ops, 'r')) {
	if (!argv[1]) {
	    zwarnnam(nam, "timestring expected");
	    return 1;
	}
	return reverse_strftime(nam, argv, scalar, OPT_ISSET(ops, 'q'));
    }

    if (!argv[1]) {
	zgettime(&ts);
	tm = localtime(&ts.tv_sec);
    } else {
	errno = 0;

	ts.tv_sec = (time_t)strtoul(argv[1], &endptr, 10);
	if (errno != 0) {
	    zwarnnam(nam, "%s: %e", argv[1], errno);
	    return 1;
	} else if (*argv[1] == '\0' || *endptr != '\0') {
	    zwarnnam(nam, "%s: invalid decimal number", argv[1]);
	    return 1;
	}

	tm = localtime(&ts.tv_sec);
	if (!tm) {
	    zwarnnam(nam, "%s: unable to convert to time", argv[1]);
	    return 1;
	}

	ts.tv_nsec = 0L;
	if (argv[2]) {
	    ts.tv_nsec = (long)zstrtol(argv[2], &endptr, 10);
	    if (errno != 0) {
		zwarnnam(nam, "%s: %e", argv[2], errno);
		return 1;
	    } else if (*argv[2] == '\0' || *endptr != '\0') {
		zwarnnam(nam, "%s: invalid decimal number", argv[2]);
		return 1;
	    } else if (ts.tv_nsec < 0 || ts.tv_nsec > 999999999) {
		zwarnnam(nam, "%s: invalid nanosecond value", argv[2]);
		return 1;
	    }
	}
    }

    bufsize = strlen(argv[0]) * 8;
    buffer = zalloc(bufsize);

    len = 0;
    for (x=0; x < 4; x++) {
        if ((len = ztrftime(buffer, bufsize, argv[0], tm, ts.tv_nsec)) >= 0 ||
	    x==3)
	    break;
	buffer = zrealloc(buffer, bufsize *= 2);
    }
    if (len < 0) {
	zwarnnam(nam, "bad/unsupported format: '%s'", argv[0]);
	zfree(buffer, bufsize);
	return 1;
    }

    if (scalar) {
	setsparam(scalar, metafy(buffer, len, META_DUP));
    } else {
	fwrite(buffer, 1, len, stdout);
	if (!OPT_ISSET(ops,'n'))
	    putchar('\n');
    }
    zfree(buffer, bufsize);

    return 0;
}

static int
bin_strftime(char *nam, char **argv, Options ops, int func)
{
    int result = 1;
    char *tz = getsparam("TZ");

    startparamscope();
    if (tz && *tz) {
	Param pm = createparam("TZ", PM_LOCAL|PM_SCALAR|PM_EXPORTED);
	if (pm)
	    pm->level = locallevel; /* because createparam() doesn't */
	setsparam("TZ", ztrdup(tz));
    }
    result = output_strftime(nam, argv, ops, func);
    endparamscope();

    return result;
}

static zlong
getcurrentsecs(UNUSED(Param pm))
{
    return (zlong) time(NULL);
}

static double
getcurrentrealtime(UNUSED(Param pm))
{
    struct timespec now;
    zgettime(&now);
    return (double)now.tv_sec + (double)now.tv_nsec * 1e-9;
}

static char **
getcurrenttime(UNUSED(Param pm))
{
    char **arr;
    char buf[DIGBUFSIZE];
    struct timespec now;

    zgettime(&now);

    arr = (char **)zhalloc(3 * sizeof(*arr));
    sprintf(buf, "%ld", (long)now.tv_sec);
    arr[0] = dupstring(buf);
    sprintf(buf, "%ld", (long)now.tv_nsec);
    arr[1] = dupstring(buf);
    arr[2] = NULL;

    return arr;
}

static struct builtin bintab[] = {
    BUILTIN("strftime",    0, bin_strftime,    1,   3, 0, "nqrs:", NULL),
};

static const struct gsu_integer epochseconds_gsu =
{ getcurrentsecs, NULL, stdunsetfn };

static const struct gsu_float epochrealtime_gsu =
{ getcurrentrealtime, NULL, stdunsetfn };

static const struct gsu_array epochtime_gsu =
{ getcurrenttime, NULL, stdunsetfn };

static struct paramdef patab[] = {
    SPECIALPMDEF("EPOCHSECONDS", PM_INTEGER|PM_READONLY,
		 &epochseconds_gsu, NULL, NULL),
    SPECIALPMDEF("EPOCHREALTIME", PM_FFLOAT|PM_READONLY,
		 &epochrealtime_gsu, NULL, NULL),
    SPECIALPMDEF("epochtime", PM_ARRAY|PM_READONLY,
		 &epochtime_gsu, NULL, NULL)
};

static struct features module_features = {
    bintab, sizeof(bintab)/sizeof(*bintab),
    NULL, 0,
    NULL, 0,
    patab, sizeof(patab)/sizeof(*patab),
    0
};

/**/
int
setup_(UNUSED(Module m))
{
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(UNUSED(Module m))
{
    return 0;
}

/**/
int
cleanup_(Module m)
{
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}
debug log:

solving 085e4cc267 ...
found 085e4cc267 in https://git.vuxu.org/mirror/zsh/

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git