xref: /plan9/sys/src/cmd/gs/src/zdevcal.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 /* Copyright (C) 1995, 1998 Aladdin Enterprises.  All rights reserved.
2 
3    This file is part of Aladdin Ghostscript.
4 
5    Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
6    or distributor accepts any responsibility for the consequences of using it,
7    or for whether it serves any particular purpose or works at all, unless he
8    or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
9    License (the "License") for full details.
10 
11    Every copy of Aladdin Ghostscript must include a copy of the License,
12    normally in a plain ASCII text file named PUBLIC.  The License grants you
13    the right to copy, modify and redistribute Aladdin Ghostscript, but only
14    under certain conditions described in the License.  Among other things, the
15    License requires that the copyright notice and this notice be preserved on
16    all copies.
17  */
18 
19 /*$Id: zdevcal.c,v 1.1 2000/03/09 08:40:44 lpd Exp $ */
20 /* %Calendar% IODevice */
21 #include "time_.h"
22 #include "ghost.h"
23 #include "gxiodev.h"
24 #include "istack.h"
25 #include "iparam.h"
26 
27 /* ------ %Calendar% ------ */
28 
29 private iodev_proc_get_params(calendar_get_params);
30 const gx_io_device gs_iodev_calendar = {
31     "%Calendar%", "Special",
32     { iodev_no_init, iodev_no_open_device, iodev_no_open_file,
33       iodev_no_fopen, iodev_no_fclose,
34       iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,
35       iodev_no_enumerate_files, NULL, NULL,
36       calendar_get_params, iodev_no_put_params
37     }
38 };
39 
40 /* Get the date and time. */
41 private int
42 calendar_get_params(gx_io_device * iodev, gs_param_list * plist)
43 {
44     int code;
45     time_t t;
46     struct tm *pltime;
47     struct tm ltime;
48     static const gs_param_item_t items[] = {
49 	{"Year", gs_param_type_int, offset_of(struct tm, tm_year)},
50 	{"Month", gs_param_type_int, offset_of(struct tm, tm_mon)},
51 	{"Day", gs_param_type_int, offset_of(struct tm, tm_mday)},
52 	{"Weekday", gs_param_type_int, offset_of(struct tm, tm_wday)},
53 	{"Hour", gs_param_type_int, offset_of(struct tm, tm_hour)},
54 	{"Minute", gs_param_type_int, offset_of(struct tm, tm_min)},
55 	{"Second", gs_param_type_int, offset_of(struct tm, tm_sec)},
56 	gs_param_item_end
57     };
58     bool running;
59 
60     if (time(&t) == -1 || (pltime = localtime(&t)) == 0) {
61 	ltime.tm_sec = ltime.tm_min = ltime.tm_hour =
62 	    ltime.tm_mday = ltime.tm_mon = ltime.tm_year = 0;
63 	running = false;
64     } else {
65 	ltime = *pltime;
66 	ltime.tm_year += 1900;
67 	ltime.tm_mon++;		/* 1-origin */
68 	running = true;
69     }
70     if ((code = gs_param_write_items(plist, &ltime, NULL, items)) < 0)
71 	return code;
72     return param_write_bool(plist, "Running", &running);
73 }
74