<commit_before>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "vm/globals.h"
#if defined(HOST_OS_FUCHSIA)

#include "vm/os.h"

#include <errno.h>
#include <fdio/util.h>
#include <zircon/process.h>
#include <zircon/syscalls.h>
#include <zircon/syscalls/object.h>
#include <zircon/types.h>

#include "lib/app/cpp/environment_services.h"
#include "lib/time_service/fidl/time_service.fidl.h"

#include "platform/assert.h"
#include "vm/zone.h"

static constexpr char kTimeServiceName[] = "time_service::TimeService";

namespace dart {

#ifndef PRODUCT

DEFINE_FLAG(bool,
            generate_perf_events_symbols,
            false,
            "Generate events symbols for profiling with perf");

#endif  // !PRODUCT

const char* OS::Name() {
  return "fuchsia";
}

intptr_t OS::ProcessId() {
  return static_cast<intptr_t>(getpid());
}

static zx_status_t GetTimeServicePtr(
    time_service::TimeServiceSyncPtr* time_svc) {
  zx::channel service_root = app::subtle::CreateStaticServiceRootHandle();
  zx::channel time_svc_channel = GetSynchronousProxy(time_svc).TakeChannel();
  return fdio_service_connect_at(service_root.get(), kTimeServiceName,
                                 time_svc_channel.release());
}

static zx_status_t GetLocalAndDstOffsetInSeconds(int64_t seconds_since_epoch,
                                                 int32_t* local_offset,
                                                 int32_t* dst_offset) {
  time_service::TimeServiceSyncPtr time_svc;
  zx_status_t status = GetTimeServicePtr(&time_svc);
  if (status == ZX_OK) {
    time_svc->GetTimezoneOffsetMinutes(seconds_since_epoch * 1000, local_offset,
                                       dst_offset);
    *local_offset *= 60;
    *dst_offset *= 60;
  }
  return status;
}

const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
  time_service::TimeServiceSyncPtr time_svc;
  if (GetTimeServicePtr(&time_svc) == ZX_OK) {
    f1dl::String res;
    time_svc->GetTimezoneId(&res);
    char* tz_name = Thread::Current()->zone()->Alloc<char>(res.size() + 1);
    memmove(tz_name, res.get().c_str(), res.size());
    tz_name[res.size()] = '\0';
    return tz_name;
  }
  return "";
}

int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
  int32_t local_offset, dst_offset;
  zx_status_t status = GetLocalAndDstOffsetInSeconds(
      seconds_since_epoch, &local_offset, &dst_offset);
  return status == ZX_OK ? local_offset + dst_offset : 0;
}

int OS::GetLocalTimeZoneAdjustmentInSeconds() {
  int32_t local_offset, dst_offset;
  zx_status_t status = GetLocalAndDstOffsetInSeconds(<commit_msg>[fuchsia] Update to the FIDL2 String API<commit_after>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "vm/globals.h"
#if defined(HOST_OS_FUCHSIA)

#include "vm/os.h"

#include <errno.h>
#include <fdio/util.h>
#include <zircon/process.h>
#include <zircon/syscalls.h>
#include <zircon/syscalls/object.h>
#include <zircon/types.h>

#include "lib/app/cpp/environment_services.h"
#include "lib/time_service/fidl/time_service.fidl.h"

#include "platform/assert.h"
#include "vm/zone.h"

static constexpr char kTimeServiceName[] = "time_service::TimeService";

namespace dart {

#ifndef PRODUCT

DEFINE_FLAG(bool,
            generate_perf_events_symbols,
            false,
            "Generate events symbols for profiling with perf");

#endif  // !PRODUCT

const char* OS::Name() {
  return "fuchsia";
}

intptr_t OS::ProcessId() {
  return static_cast<intptr_t>(getpid());
}

static zx_status_t GetTimeServicePtr(
    time_service::TimeServiceSyncPtr* time_svc) {
  zx::channel service_root = app::subtle::CreateStaticServiceRootHandle();
  zx::channel time_svc_channel = GetSynchronousProxy(time_svc).TakeChannel();
  return fdio_service_connect_at(service_root.get(), kTimeServiceName,
                                 time_svc_channel.release());
}

static zx_status_t GetLocalAndDstOffsetInSeconds(int64_t seconds_since_epoch,
                                                 int32_t* local_offset,
                                                 int32_t* dst_offset) {
  time_service::TimeServiceSyncPtr time_svc;
  zx_status_t status = GetTimeServicePtr(&time_svc);
  if (status == ZX_OK) {
    time_svc->GetTimezoneOffsetMinutes(seconds_since_epoch * 1000, local_offset,
                                       dst_offset);
    *local_offset *= 60;
    *dst_offset *= 60;
  }
  return status;
}

const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
  time_service::TimeServiceSyncPtr time_svc;
  if (GetTimeServicePtr(&time_svc) == ZX_OK) {
    f1dl::String res;
    time_svc->GetTimezoneId(&res);
    char* tz_name = Thread::Current()->zone()->Alloc<char>(res->size() + 1);
    memmove(tz_name, res->data(), res->size());
    tz_name[res->size()] = '\0';
    return tz_name;
  }
  return "";
}

int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
  int32_t local_offset, dst_offset;
  zx_status_t status = GetLocalAndDstOffsetInSeconds(
      seconds_since_epoch, &local_offset, &dst_offset);
  return status == ZX_OK ? local_offset + dst_offset : 0;
}

int OS::GetLocalTimeZoneAdjustmentInSeconds() {
  int32_t local_offset, dst_offset;
  zx_status_t status = GetLocalAndDstOffsetInSeconds(
{'fraction_non_alphanumeric': 0.11324479540559942, 'fraction_numerical': 0.01094759511844939, 'mean_word_length': 3.7228813559322034, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5797864', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1867, 'n_words': 373}