blob: 3fb515b5f1d8bcc502d7609332bc354be8961429 [file] [log] [blame]
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +02001/*
2htop - DateTimeMeter.c
3(C) 2004-2020 Hisham H. Muhammad, Michael Schönitzer
4Released under the GNU GPL, see the COPYING file
5in the source distribution for its full text.
6*/
7
Benny Baumann0f526292020-09-19 13:55:23 +02008#include "config.h" // IWYU pragma: keep
9
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020010#include "DateTimeMeter.h"
11
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020012#include <time.h>
13
Benny Baumann0f526292020-09-19 13:55:23 +020014#include "CRT.h"
15#include "Object.h"
16
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020017
Christian Göttschedc6523b2020-10-05 13:54:33 +020018static const int DateTimeMeter_attributes[] = {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020019 DATETIME
20};
21
Christian Göttsche2d1042a2020-10-06 13:13:16 +020022static void DateTimeMeter_updateValues(Meter* this) {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020023 time_t t = time(NULL);
24 struct tm result;
Christian Göttsched72b0a62021-01-05 23:42:55 +010025 const struct tm* lt = localtime_r(&t, &result);
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020026 int year = lt->tm_year + 1900;
Benny Baumann61e14d42020-10-31 23:28:02 +010027 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020028 this->total = 366;
Christian Göttsche635d4cf2021-02-28 22:47:45 +010029 } else {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020030 this->total = 365;
31 }
32 this->values[0] = lt->tm_yday;
Christian Göttsche2d1042a2020-10-06 13:13:16 +020033 strftime(this->txtBuffer, sizeof(this->txtBuffer), "%F %H:%M:%S", lt);
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020034}
35
Christian Göttscheba282cf2020-10-05 13:19:50 +020036const MeterClass DateTimeMeter_class = {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020037 .super = {
38 .extends = Class(Meter),
39 .delete = Meter_delete
40 },
41 .updateValues = DateTimeMeter_updateValues,
42 .defaultMode = TEXT_METERMODE,
43 .maxItems = 1,
44 .total = 365,
45 .attributes = DateTimeMeter_attributes,
46 .name = "DateTime",
47 .uiName = "Date and Time",
48 .caption = "Date & Time: ",
49};