2020-03-29 23:21:56 +00:00
|
|
|
/*
|
2021-04-22 20:51:19 +00:00
|
|
|
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
2020-03-29 23:21:56 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-29 23:21:56 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-20 20:29:27 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-29 23:21:56 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2020-04-17 17:07:59 +00:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-03-29 23:21:56 +00:00
|
|
|
#include <LibJS/Runtime/Date.h>
|
2020-04-17 17:07:59 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-29 23:21:56 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-03-16 14:02:16 +00:00
|
|
|
Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 milliseconds, bool is_invalid)
|
2020-04-17 17:07:59 +00:00
|
|
|
{
|
2021-03-16 14:02:16 +00:00
|
|
|
return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype());
|
2020-04-17 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 14:02:16 +00:00
|
|
|
Date::Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype)
|
2020-06-23 15:21:53 +00:00
|
|
|
: Object(prototype)
|
2020-04-18 08:27:57 +00:00
|
|
|
, m_datetime(datetime)
|
2020-03-29 23:21:56 +00:00
|
|
|
, m_milliseconds(milliseconds)
|
2021-03-16 14:02:16 +00:00
|
|
|
, m_is_invalid(is_invalid)
|
2020-03-29 23:21:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Date::~Date()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-23 17:30:18 +00:00
|
|
|
tm Date::to_utc_tm() const
|
2020-08-20 20:29:27 +00:00
|
|
|
{
|
|
|
|
time_t timestamp = m_datetime.timestamp();
|
|
|
|
struct tm tm;
|
|
|
|
gmtime_r(×tamp, &tm);
|
2020-08-23 17:30:18 +00:00
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_date() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_mday;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_day() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_wday;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_full_year() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_year + 1900;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_hours() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_hour;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_minutes() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Date::utc_month() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_mon;
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:01:14 +00:00
|
|
|
int Date::utc_seconds() const
|
|
|
|
{
|
|
|
|
return to_utc_tm().tm_sec;
|
|
|
|
}
|
|
|
|
|
2021-03-14 15:40:41 +00:00
|
|
|
String Date::gmt_date_string() const
|
|
|
|
{
|
|
|
|
// Mon, 18 Dec 1995 17:28:35 GMT
|
|
|
|
// FIXME: Note that we're totally cheating with the timezone part here..
|
|
|
|
return datetime().to_string("%a, %e %b %Y %T GMT");
|
|
|
|
}
|
|
|
|
|
2020-08-23 17:30:18 +00:00
|
|
|
String Date::iso_date_string() const
|
|
|
|
{
|
|
|
|
auto tm = to_utc_tm();
|
2020-08-20 20:29:27 +00:00
|
|
|
int year = tm.tm_year + 1900;
|
|
|
|
int month = tm.tm_mon + 1;
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
if (year < 0)
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("-{:06}", -year);
|
2020-08-20 20:29:27 +00:00
|
|
|
else if (year > 9999)
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("+{:06}", year);
|
2020-08-20 20:29:27 +00:00
|
|
|
else
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:04}", year);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append('-');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:02}", month);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append('-');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_mday);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append('T');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_hour);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append(':');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_min);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append(':');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_sec);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append('.');
|
2021-05-07 09:51:57 +00:00
|
|
|
builder.appendff("{:03}", m_milliseconds);
|
2020-08-20 20:29:27 +00:00
|
|
|
builder.append('Z');
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:21:56 +00:00
|
|
|
}
|