File: datetime.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (280 lines) | stat: -rw-r--r-- 7,771 bytes parent folder | download | duplicates (5)
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
/***************************************
  $Header: /home/amb/CVS/procmeter3/modules/datetime.c,v 1.10 2007-04-17 18:06:04 amb Exp $

  ProcMeter - A system monitoring program for Linux - Version 3.4g.

  Date and Time Information module source file.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1998,99,2002,2007 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
#include <sys/time.h>

#include "procmeter.h"

/* The interface information.  */

/*+ The current date output with year. +*/
ProcMeterOutput date_dmy_output=
{
 /* char  name[];          */ "Date_DMY",
 /* char *description;     */ "The current date in the local timezone; day of week, day of month, month, year.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 60,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current date output. +*/
ProcMeterOutput date_dm_output=
{
 /* char  name[];          */ "Date_DM",
 /* char *description;     */ "The current date in the local timezone; day of week, day of month, month.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 60,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current time output with seconds. +*/
ProcMeterOutput time_hms_output=
{
 /* char  name[];          */ "Time_HMS",
 /* char *description;     */ "The current time in the local timezone; hours, minutes and seconds.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 1,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current time output with seconds, with timezone. +*/
ProcMeterOutput time_hms_tz_output=
{
 /* char  name[];          */ "Time_HMS_TZ",
 /* char *description;     */ "The current time in the local timezone; hours, minutes, seconds and timezone.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 1,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current time output, without seconds. +*/
ProcMeterOutput time_hm_output=
{
 /* char  name[];          */ "Time_HM",
 /* char *description;     */ "The current time in the local timezone; hours and minutes.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 60,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current time output, without seconds, with timezone. +*/
ProcMeterOutput time_hm_tz_output=
{
 /* char  name[];          */ "Time_HM_TZ",
 /* char *description;     */ "The current time in the local timezone; hours, minutes and timezone.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 60,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The current uptime output +*/
ProcMeterOutput uptime_dhm_output=
{
 /* char  name[];          */ "Uptime_DHM",
 /* char *description;     */ "The amount of time that the system has been running for; days, hours and minutes.",
 /* char  type;            */ PROCMETER_TEXT,
 /* short interval;        */ 60,
 /* char  text_value[];    */ "unknown",
 /* long  graph_value;     */ -1,
 /* short graph_scale;     */ 0,
 /* char  graph_units[];   */ "n/a"
};

/*+ The outputs. +*/
ProcMeterOutput *outputs[]=
{
 &date_dmy_output,
 &date_dm_output,
 &time_hms_output,
 &time_hms_tz_output,
 &time_hm_output,
 &time_hm_tz_output,
 NULL,                          /* Insert uptime_dhm_output here if /proc/uptime exists. */
 NULL
};

/*+ The module. +*/
ProcMeterModule module=
{
 /* char name[];           */ "Date_Time",
 /* char *description;     */ "The current date and time and the amount of time the system has been running.",
};


static int twelve_hour = 0;


/*++++++++++++++++++++++++++++++++++++++
  Load the module.

  ProcMeterModule *Load Returns the module information.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterModule *Load(void)
{
 return(&module);
}


/*++++++++++++++++++++++++++++++++++++++
  Initialise the module, creating the outputs as required.

  ProcMeterOutput **Initialise Returns a NULL terminated list of outputs.

  char *options The options string for the module from the .procmeterrc file.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterOutput **Initialise(char *options)
{
 FILE *f;

 f=fopen("/proc/uptime","r");
 if(!f)
    fprintf(stderr,"ProcMeter(%s): Could not open '/proc/uptime'.\n",__FILE__);
 else
   {
    time_t uptime;

    if(fscanf(f,"%ld",&uptime)!=1)
       fprintf(stderr,"ProcMeter(%s): Could not read '/proc/uptime'.\n",__FILE__);
    else
       outputs[sizeof(outputs)/sizeof(outputs[0])-2]=&uptime_dhm_output;

    fclose(f);
   }

 if (options && strcmp(options, "12") == 0)
    twelve_hour = 1;
 
 return(outputs);
}


/*++++++++++++++++++++++++++++++++++++++
  Perform an update on one of the statistics.

  int Update Returns 0 if OK, else -1.

  time_t now The current time.

  ProcMeterOutput* output The output that the value is wanted for.
  ++++++++++++++++++++++++++++++++++++++*/

int Update(time_t now,ProcMeterOutput* output)
{
 if(output==&uptime_dhm_output)
   {
    FILE *f;
    time_t uptime;
    int hours,days,mins;

    f=fopen("/proc/uptime","r");
    if(!f)
       return(-1);

    if(fscanf(f,"%ld",&uptime)!=1)
       return(-1);

    fclose(f);

    days =uptime/(24*3600);
    hours=(uptime%(24*3600))/3600;
    mins =(uptime%3600)/60;

    sprintf(output->text_value,"%dD %2dH %2dM",days,hours,mins);
   }
 else
   {
    struct tm *tim;

    tim=localtime(&now);
    if(tim->tm_isdst<0)
       tim=gmtime(&now);

    if(output==&date_dmy_output)
      {
       strftime(output->text_value,PROCMETER_TEXT_LEN,"%a %e %b %Y",tim);
      }
    else if(output==&date_dm_output)
      {
       strftime(output->text_value,PROCMETER_TEXT_LEN,"%a %e %b",tim);
      }
    else if(output==&time_hms_output)
      {
       if(twelve_hour)
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%I:%M:%S %p",tim);
       else
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%H:%M:%S",tim);
      }
    else if(output==&time_hms_tz_output)
      {
       if(twelve_hour)
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%I:%M:%S %p %Z",tim);
       else
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%H:%M:%S %Z",tim);
      }
    else if(output==&time_hm_output)
      {
       if(twelve_hour)
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%I:%M %p",tim);
       else
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%H:%M",tim);
      }
    else if(output==&time_hm_tz_output)
      {
       if(twelve_hour)
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%I:%M %p %Z",tim);
       else
          strftime(output->text_value,PROCMETER_TEXT_LEN,"%H:%M %Z",tim);
      }
    else
       return(-1);
   }

 return(0);
}


/*++++++++++++++++++++++++++++++++++++++
  Tidy up and prepare to have the module unloaded.
  ++++++++++++++++++++++++++++++++++++++*/

void Unload(void)
{
}