2020-02-11 18:42:02 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-02-11 18:42:02 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-11 18:42:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-01-28 19:26:43 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-04-15 14:33:28 +00:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-11 18:42:02 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2020-08-20 14:39:56 +00:00
|
|
|
// Represents a time in local time.
|
2020-02-11 18:42:02 +00:00
|
|
|
class DateTime {
|
|
|
|
public:
|
|
|
|
time_t timestamp() const { return m_timestamp; }
|
|
|
|
|
|
|
|
unsigned year() const { return m_year; }
|
|
|
|
unsigned month() const { return m_month; }
|
|
|
|
unsigned day() const { return m_day; }
|
|
|
|
|
|
|
|
unsigned hour() const { return m_hour; }
|
|
|
|
unsigned minute() const { return m_minute; }
|
|
|
|
unsigned second() const { return m_second; }
|
2020-03-10 21:41:01 +00:00
|
|
|
unsigned weekday() const;
|
|
|
|
unsigned days_in_month() const;
|
|
|
|
unsigned day_of_year() const;
|
|
|
|
bool is_leap_year() const;
|
2020-02-11 18:42:02 +00:00
|
|
|
|
2021-10-30 07:06:27 +00:00
|
|
|
void set_time(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
|
2023-11-17 18:23:08 +00:00
|
|
|
void set_time_only(int hour, int minute, Optional<int> second = {});
|
2023-11-17 18:23:43 +00:00
|
|
|
void set_date(Core::DateTime const& other);
|
2024-04-01 18:45:41 +00:00
|
|
|
|
|
|
|
enum class LocalTime {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<String> to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
|
|
|
|
ByteString to_byte_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
|
2020-02-11 18:42:02 +00:00
|
|
|
|
2021-10-30 07:06:27 +00:00
|
|
|
static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
|
2020-02-11 18:42:02 +00:00
|
|
|
static DateTime now();
|
2020-02-11 18:48:46 +00:00
|
|
|
static DateTime from_timestamp(time_t);
|
2023-11-07 16:47:23 +00:00
|
|
|
static Optional<DateTime> parse(StringView format, StringView string);
|
2020-02-11 18:42:02 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator<(DateTime const& other) const { return m_timestamp < other.m_timestamp; }
|
2023-11-17 20:51:04 +00:00
|
|
|
bool operator>(DateTime const& other) const { return m_timestamp > other.m_timestamp; }
|
|
|
|
bool operator<=(DateTime const& other) const { return m_timestamp <= other.m_timestamp; }
|
|
|
|
bool operator>=(DateTime const& other) const { return m_timestamp >= other.m_timestamp; }
|
2022-09-12 11:26:02 +00:00
|
|
|
bool operator==(DateTime const& other) const { return m_timestamp == other.m_timestamp; }
|
2020-05-07 04:41:07 +00:00
|
|
|
|
2020-02-11 18:42:02 +00:00
|
|
|
private:
|
|
|
|
time_t m_timestamp { 0 };
|
2021-08-19 16:16:03 +00:00
|
|
|
int m_year { 0 };
|
|
|
|
int m_month { 0 };
|
|
|
|
int m_day { 0 };
|
|
|
|
int m_hour { 0 };
|
|
|
|
int m_minute { 0 };
|
|
|
|
int m_second { 0 };
|
2020-02-11 18:42:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-04-15 14:33:28 +00:00
|
|
|
|
2022-09-12 11:26:02 +00:00
|
|
|
namespace AK {
|
|
|
|
template<>
|
|
|
|
struct Formatter<Core::DateTime> : StandardFormatter {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Core::DateTime const& value)
|
|
|
|
{
|
|
|
|
// Can't use DateTime::to_string() here: It doesn't propagate allocation failure.
|
|
|
|
return builder.builder().try_appendff("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}"sv,
|
|
|
|
value.year(), value.month(), value.day(),
|
|
|
|
value.hour(), value.minute(), value.second());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-15 14:33:28 +00:00
|
|
|
namespace IPC {
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, Core::DateTime const&);
|
2022-11-15 16:24:59 +00:00
|
|
|
|
|
|
|
template<>
|
2022-12-23 01:40:33 +00:00
|
|
|
ErrorOr<Core::DateTime> decode(Decoder&);
|
2021-04-15 14:33:28 +00:00
|
|
|
|
|
|
|
}
|