DateTime.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/StringView.h>
  9. #include <LibIPC/Forward.h>
  10. #include <time.h>
  11. namespace Core {
  12. // Represents a time in local time.
  13. class DateTime {
  14. public:
  15. time_t timestamp() const { return m_timestamp; }
  16. unsigned year() const { return m_year; }
  17. unsigned month() const { return m_month; }
  18. unsigned day() const { return m_day; }
  19. unsigned hour() const { return m_hour; }
  20. unsigned minute() const { return m_minute; }
  21. unsigned second() const { return m_second; }
  22. unsigned weekday() const;
  23. unsigned days_in_month() const;
  24. unsigned day_of_year() const;
  25. bool is_leap_year() const;
  26. void set_time(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
  27. void set_time_only(int hour, int minute, Optional<int> second = {});
  28. void set_date(Core::DateTime const& other);
  29. enum class LocalTime {
  30. Yes,
  31. No,
  32. };
  33. ErrorOr<String> to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
  34. ByteString to_byte_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
  35. static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
  36. static DateTime now();
  37. static DateTime from_timestamp(time_t);
  38. static Optional<DateTime> parse(StringView format, StringView string);
  39. bool operator<(DateTime const& other) const { return m_timestamp < other.m_timestamp; }
  40. bool operator>(DateTime const& other) const { return m_timestamp > other.m_timestamp; }
  41. bool operator<=(DateTime const& other) const { return m_timestamp <= other.m_timestamp; }
  42. bool operator>=(DateTime const& other) const { return m_timestamp >= other.m_timestamp; }
  43. bool operator==(DateTime const& other) const { return m_timestamp == other.m_timestamp; }
  44. private:
  45. time_t m_timestamp { 0 };
  46. int m_year { 0 };
  47. int m_month { 0 };
  48. int m_day { 0 };
  49. int m_hour { 0 };
  50. int m_minute { 0 };
  51. int m_second { 0 };
  52. };
  53. }
  54. namespace AK {
  55. template<>
  56. struct Formatter<Core::DateTime> : StandardFormatter {
  57. ErrorOr<void> format(FormatBuilder& builder, Core::DateTime const& value)
  58. {
  59. // Can't use DateTime::to_string() here: It doesn't propagate allocation failure.
  60. return builder.builder().try_appendff("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}"sv,
  61. value.year(), value.month(), value.day(),
  62. value.hour(), value.minute(), value.second());
  63. }
  64. };
  65. }
  66. namespace IPC {
  67. template<>
  68. ErrorOr<void> encode(Encoder&, Core::DateTime const&);
  69. template<>
  70. ErrorOr<Core::DateTime> decode(Decoder&);
  71. }