blob: fc9e0c89d4c643919cddb44b132952d6d431b9a3 [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öttschee1ce1412020-11-24 18:31:03 +010022static void DateTimeMeter_updateValues(Meter* this, char* buffer, size_t size) {
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;
33 strftime(buffer, size, "%F %H:%M:%S", lt);
34}
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};