blob: ebe722882dee3740f95b285d7aa4e7e932dfa640 [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
Daniel Lange94ad1112021-09-22 11:33:00 +02004Released under the GNU GPLv2+, see the COPYING file
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +02005in 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>
mayurdahibhate1b74dfe2021-04-29 20:42:43 +053013#include <sys/time.h>
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020014
Benny Baumann0f526292020-09-19 13:55:23 +020015#include "CRT.h"
16#include "Object.h"
Nathan Scottb74673f2023-08-31 11:56:43 +100017#include "ProcessTable.h"
Benny Baumann0f526292020-09-19 13:55:23 +020018
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020019
Christian Göttschedc6523b2020-10-05 13:54:33 +020020static const int DateTimeMeter_attributes[] = {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020021 DATETIME
22};
23
Christian Göttsche2d1042a2020-10-06 13:13:16 +020024static void DateTimeMeter_updateValues(Meter* this) {
Nathan Scott0bdade12023-05-02 09:02:22 +100025 const Machine* host = this->host;
Nathan Scott356488a2021-03-30 15:55:48 +110026
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020027 struct tm result;
Nathan Scott0bdade12023-05-02 09:02:22 +100028 const struct tm* lt = localtime_r(&host->realtime.tv_sec, &result);
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020029 int year = lt->tm_year + 1900;
Benny Baumann61e14d42020-10-31 23:28:02 +010030 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020031 this->total = 366;
Christian Göttsche635d4cf2021-02-28 22:47:45 +010032 } else {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020033 this->total = 365;
34 }
35 this->values[0] = lt->tm_yday;
Christian Göttsche2d1042a2020-10-06 13:13:16 +020036 strftime(this->txtBuffer, sizeof(this->txtBuffer), "%F %H:%M:%S", lt);
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020037}
38
Christian Göttscheba282cf2020-10-05 13:19:50 +020039const MeterClass DateTimeMeter_class = {
Michael F. Schönitzerd93cac12020-10-05 13:52:58 +020040 .super = {
41 .extends = Class(Meter),
42 .delete = Meter_delete
43 },
44 .updateValues = DateTimeMeter_updateValues,
45 .defaultMode = TEXT_METERMODE,
46 .maxItems = 1,
47 .total = 365,
48 .attributes = DateTimeMeter_attributes,
49 .name = "DateTime",
50 .uiName = "Date and Time",
51 .caption = "Date & Time: ",
52};