1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/glean/bindings/Datetime.h"
#include "jsapi.h"
#include "js/Date.h"
#include "nsString.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/dom/GleanMetricsBinding.h"
#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
#include "mozilla/glean/fog_ffi_generated.h"
#include "prtime.h"
namespace mozilla::glean {
namespace impl {
void DatetimeMetric::Set(const PRExplodedTime* aValue) const {
PRExplodedTime exploded;
if (!aValue) {
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &exploded);
} else {
exploded = *aValue;
}
auto id = ScalarIdForMetric(mId);
if (id) {
const uint32_t buflen = 64; // More than enough for now.
char buf[buflen];
uint32_t written = PR_FormatTime(buf, buflen, "%FT%T%z", &exploded);
if (written > 2 && written < 64) {
// Format's still not quite there. Gotta put a `:` between timezone
// hours and minutes
buf[written] = '\0';
buf[written - 1] = buf[written - 2];
buf[written - 2] = buf[written - 3];
buf[written - 3] = ':';
Telemetry::ScalarSet(id.extract(), NS_ConvertASCIItoUTF16(buf));
}
}
int32_t offset =
exploded.tm_params.tp_gmt_offset + exploded.tm_params.tp_dst_offset;
FogDatetime dt{exploded.tm_year,
static_cast<uint32_t>(exploded.tm_month + 1),
static_cast<uint32_t>(exploded.tm_mday),
static_cast<uint32_t>(exploded.tm_hour),
static_cast<uint32_t>(exploded.tm_min),
static_cast<uint32_t>(exploded.tm_sec),
static_cast<uint32_t>(exploded.tm_usec * 1000),
offset};
fog_datetime_set(mId, &dt);
}
Result<Maybe<PRExplodedTime>, nsCString> DatetimeMetric::TestGetValue(
const nsACString& aPingName) const {
nsCString err;
if (fog_datetime_test_get_error(mId, &err)) {
return Err(err);
}
if (!fog_datetime_test_has_value(mId, &aPingName)) {
return Maybe<PRExplodedTime>();
}
FogDatetime ret{0};
fog_datetime_test_get_value(mId, &aPingName, &ret);
PRExplodedTime pret{0};
pret.tm_year = static_cast<PRInt16>(ret.year);
pret.tm_month = static_cast<PRInt32>(ret.month - 1);
pret.tm_mday = static_cast<PRInt32>(ret.day);
pret.tm_hour = static_cast<PRInt32>(ret.hour);
pret.tm_min = static_cast<PRInt32>(ret.minute);
pret.tm_sec = static_cast<PRInt32>(ret.second);
pret.tm_usec = static_cast<PRInt32>(ret.nano / 1000); // truncated is fine
pret.tm_params.tp_gmt_offset = static_cast<PRInt32>(ret.offset_seconds);
return Some(std::move(pret));
}
} // namespace impl
/* virtual */
JSObject* GleanDatetime::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::GleanDatetime_Binding::Wrap(aCx, this, aGivenProto);
}
void GleanDatetime::Set(const dom::Optional<int64_t>& aValue) {
if (aValue.WasPassed()) {
PRExplodedTime exploded;
PR_ExplodeTime(aValue.Value(), PR_LocalTimeParameters, &exploded);
mDatetime.Set(&exploded);
} else {
mDatetime.Set();
}
}
void GleanDatetime::TestGetValue(JSContext* aCx, const nsACString& aPingName,
JS::MutableHandle<JS::Value> aResult,
ErrorResult& aRv) {
auto result = mDatetime.TestGetValue(aPingName);
if (result.isErr()) {
aResult.set(JS::UndefinedValue());
aRv.ThrowDataError(result.unwrapErr());
return;
}
auto optresult = result.unwrap();
if (optresult.isNothing()) {
aResult.set(JS::UndefinedValue());
} else {
double millis =
static_cast<double>(PR_ImplodeTime(optresult.ptr())) / PR_USEC_PER_MSEC;
JS::Rooted<JSObject*> root(aCx,
JS::NewDateObject(aCx, JS::TimeClip(millis)));
aResult.setObject(*root);
}
}
} // namespace mozilla::glean
|