summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-28 14:26:43 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-28 20:19:05 +0000
commitb387ac56b6fbac59db3bc127dfbbeaf4f596a852 (patch)
treee8a5c4aa8917d77fe1ccbfa574efd973df4fe221 /Userland/Libraries/LibCore
parent9c80430eea071be981e793fd5949c22f708b2cf6 (diff)
downloadserenity-b387ac56b6fbac59db3bc127dfbbeaf4f596a852.zip
LibCore: Change DateTime format strings to be StringViews
There's no need to allocate a String for these. Note the "string" parameter of DateTime::parse is left as a String for now; the parser is currently using strtol which needs a NUL-terminated string. This method can likely be rewritten with GenericLexer.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/DateTime.cpp4
-rw-r--r--Userland/Libraries/LibCore/DateTime.h5
2 files changed, 5 insertions, 4 deletions
diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp
index dce43d57b5..3a7fdfe345 100644
--- a/Userland/Libraries/LibCore/DateTime.cpp
+++ b/Userland/Libraries/LibCore/DateTime.cpp
@@ -83,7 +83,7 @@ void DateTime::set_time(int year, int month, int day, int hour, int minute, int
m_second = tm.tm_sec;
}
-String DateTime::to_string(const String& format) const
+String DateTime::to_string(StringView format) const
{
const char wday_short_names[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
@@ -261,7 +261,7 @@ String DateTime::to_string(const String& format) const
return builder.build();
}
-Optional<DateTime> DateTime::parse(const String& format, const String& string)
+Optional<DateTime> DateTime::parse(StringView format, const String& string)
{
unsigned format_pos = 0;
unsigned string_pos = 0;
diff --git a/Userland/Libraries/LibCore/DateTime.h b/Userland/Libraries/LibCore/DateTime.h
index 30ac5227af..41d8a1ad72 100644
--- a/Userland/Libraries/LibCore/DateTime.h
+++ b/Userland/Libraries/LibCore/DateTime.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/String.h>
+#include <AK/StringView.h>
#include <LibIPC/Forward.h>
#include <time.h>
@@ -30,12 +31,12 @@ public:
bool is_leap_year() const;
void set_time(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
- String to_string(const String& format = "%Y-%m-%d %H:%M:%S") const;
+ String to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv) const;
static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
static DateTime now();
static DateTime from_timestamp(time_t);
- static Optional<DateTime> parse(const String& format, const String& string);
+ static Optional<DateTime> parse(StringView format, const String& string);
bool operator<(const DateTime& other) const { return m_timestamp < other.m_timestamp; }