Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sts=2 et sw=2 tw=80:
3
* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef builtin_intl_RelativeTimeFormat_h
8
#define builtin_intl_RelativeTimeFormat_h
9
10
#include "mozilla/Attributes.h"
11
12
#include <stdint.h>
13
14
#include "builtin/SelfHostingDefines.h"
15
#include "gc/Barrier.h"
16
#include "js/Class.h"
17
#include "vm/NativeObject.h"
18
#include "vm/Runtime.h"
19
20
struct UFormattedValue;
21
struct URelativeDateTimeFormatter;
22
23
namespace js {
24
25
class RelativeTimeFormatObject : public NativeObject {
26
public:
27
static const JSClass class_;
28
static const JSClass& protoClass_;
29
30
static constexpr uint32_t INTERNALS_SLOT = 0;
31
static constexpr uint32_t URELATIVE_TIME_FORMAT_SLOT = 1;
32
static constexpr uint32_t SLOT_COUNT = 2;
33
34
static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
35
"INTERNALS_SLOT must match self-hosting define for internals "
36
"object slot");
37
38
// Estimated memory use for URelativeDateTimeFormatter.
39
static constexpr size_t EstimatedMemoryUse = 278;
40
41
URelativeDateTimeFormatter* getRelativeDateTimeFormatter() const {
42
const auto& slot = getFixedSlot(URELATIVE_TIME_FORMAT_SLOT);
43
if (slot.isUndefined()) {
44
return nullptr;
45
}
46
return static_cast<URelativeDateTimeFormatter*>(slot.toPrivate());
47
}
48
49
void setRelativeDateTimeFormatter(URelativeDateTimeFormatter* rtf) {
50
setFixedSlot(URELATIVE_TIME_FORMAT_SLOT, PrivateValue(rtf));
51
}
52
53
private:
54
static const JSClassOps classOps_;
55
static const ClassSpec classSpec_;
56
57
static void finalize(JSFreeOp* fop, JSObject* obj);
58
};
59
60
/**
61
* Returns a relative time as a string formatted according to the effective
62
* locale and the formatting options of the given RelativeTimeFormat.
63
*
64
* |t| should be a number representing a number to be formatted.
65
* |unit| should be "second", "minute", "hour", "day", "week", "month",
66
* "quarter", or "year".
67
* |numeric| should be "always" or "auto".
68
*
69
* Usage: formatted = intl_FormatRelativeTime(relativeTimeFormat, t,
70
* unit, numeric, formatToParts)
71
*/
72
extern MOZ_MUST_USE bool intl_FormatRelativeTime(JSContext* cx, unsigned argc,
73
JS::Value* vp);
74
75
namespace intl {
76
77
using FieldType = js::ImmutablePropertyNamePtr JSAtomState::*;
78
79
#ifndef U_HIDE_DRAFT_API
80
MOZ_MUST_USE bool FormattedRelativeTimeToParts(
81
JSContext* cx, const UFormattedValue* formattedValue, double timeValue,
82
FieldType relativeTimeUnit, MutableHandleValue result);
83
#endif
84
85
} // namespace intl
86
} // namespace js
87
88
#endif /* builtin_intl_RelativeTimeFormat_h */