1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:58:12 +00:00

LibJS: Implement Date.prototype.toISOString()

This commit is contained in:
Nico Weber 2020-08-20 16:29:27 -04:00 committed by Andreas Kling
parent 1eac1b360b
commit a6b68451dc
5 changed files with 55 additions and 0 deletions

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <LibCore/DateTime.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
@ -48,4 +49,36 @@ Date::~Date()
{
}
String Date::iso_date_string() const
{
time_t timestamp = m_datetime.timestamp();
struct tm tm;
gmtime_r(&timestamp, &tm);
int year = tm.tm_year + 1900;
int month = tm.tm_mon + 1;
StringBuilder builder;
if (year < 0)
builder.appendf("-%06d", -year);
else if (year > 9999)
builder.appendf("+%06d", year);
else
builder.appendf("%04d", year);
builder.append('-');
builder.appendf("%02d", month);
builder.append('-');
builder.appendf("%02d", tm.tm_mday);
builder.append('T');
builder.appendf("%02d", tm.tm_hour);
builder.append(':');
builder.appendf("%02d", tm.tm_min);
builder.append(':');
builder.appendf("%02d", tm.tm_sec);
builder.append('.');
builder.appendf("%03d", m_milliseconds);
builder.append('Z');
return builder.build();
}
}