2020-02-11 19:42:02 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-11 19:42:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-03-08 18:35:26 +08:00
|
|
|
#include <AK/String.h>
|
2021-04-15 10:33:28 -04:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-11 19:42:02 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
2020-08-20 10:39:56 -04:00
|
|
|
// Represents a time in local time.
|
2020-02-11 19:42:02 +01: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 16:41:01 -05:00
|
|
|
unsigned weekday() const;
|
|
|
|
|
unsigned days_in_month() const;
|
|
|
|
|
unsigned day_of_year() const;
|
|
|
|
|
bool is_leap_year() const;
|
2020-02-11 19:42:02 +01:00
|
|
|
|
2021-08-19 19:16:03 +03:00
|
|
|
void set_time(int year, int month = 1, int day = 0, int hour = 0, int minute = 0, int second = 0);
|
2020-03-08 18:35:26 +08:00
|
|
|
String to_string(const String& format = "%Y-%m-%d %H:%M:%S") const;
|
2020-02-11 19:42:02 +01:00
|
|
|
|
2021-08-19 19:16:03 +03:00
|
|
|
static DateTime create(int year, int month = 1, int day = 0, int hour = 0, int minute = 0, int second = 0);
|
2020-02-11 19:42:02 +01:00
|
|
|
static DateTime now();
|
2020-02-11 19:48:46 +01:00
|
|
|
static DateTime from_timestamp(time_t);
|
2021-06-01 16:52:38 +04:00
|
|
|
static Optional<DateTime> parse(const String& format, const String& string);
|
2020-02-11 19:42:02 +01:00
|
|
|
|
2021-04-18 13:43:38 +04:30
|
|
|
bool operator<(const DateTime& other) const { return m_timestamp < other.m_timestamp; }
|
2020-05-07 09:11:07 +04:30
|
|
|
|
2020-02-11 19:42:02 +01:00
|
|
|
private:
|
|
|
|
|
time_t m_timestamp { 0 };
|
2021-08-19 19:16:03 +03: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 19:42:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2021-04-15 10:33:28 -04:00
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
|
|
bool encode(IPC::Encoder&, const Core::DateTime&);
|
|
|
|
|
bool decode(IPC::Decoder&, Core::DateTime&);
|
|
|
|
|
|
|
|
|
|
}
|