DateTime.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ErrorOr<String> to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv) const;
  30. ByteString to_byte_string(StringView format = "%Y-%m-%d %H:%M:%S"sv) const;
  31. static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
  32. static DateTime now();
  33. static DateTime from_timestamp(time_t);
  34. static Optional<DateTime> parse(StringView format, StringView string);
  35. bool operator<(DateTime const& other) const { return m_timestamp < other.m_timestamp; }
  36. bool operator>(DateTime const& other) const { return m_timestamp > other.m_timestamp; }
  37. bool operator<=(DateTime const& other) const { return m_timestamp <= other.m_timestamp; }
  38. bool operator>=(DateTime const& other) const { return m_timestamp >= other.m_timestamp; }
  39. bool operator==(DateTime const& other) const { return m_timestamp == other.m_timestamp; }
  40. private:
  41. time_t m_timestamp { 0 };
  42. int m_year { 0 };
  43. int m_month { 0 };
  44. int m_day { 0 };
  45. int m_hour { 0 };
  46. int m_minute { 0 };
  47. int m_second { 0 };
  48. };
  49. }
  50. namespace AK {
  51. template<>
  52. struct Formatter<Core::DateTime> : StandardFormatter {
  53. ErrorOr<void> format(FormatBuilder& builder, Core::DateTime const& value)
  54. {
  55. // Can't use DateTime::to_string() here: It doesn't propagate allocation failure.
  56. return builder.builder().try_appendff("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}"sv,
  57. value.year(), value.month(), value.day(),
  58. value.hour(), value.minute(), value.second());
  59. }
  60. };
  61. }
  62. namespace IPC {
  63. template<>
  64. ErrorOr<void> encode(Encoder&, Core::DateTime const&);
  65. template<>
  66. ErrorOr<Core::DateTime> decode(Decoder&);
  67. }