tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

time.cc (2931B)


      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 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style license that can be
      5 // found in the LICENSE file.
      6 
      7 #include "base/time.h"
      8 #include "base/string_util.h"
      9 #include "base/sys_string_conversions.h"
     10 #include "prtime.h"
     11 
     12 #include "base/logging.h"
     13 
     14 namespace base {
     15 
     16 // TimeDelta ------------------------------------------------------------------
     17 
     18 int TimeDelta::InDays() const {
     19  return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
     20 }
     21 
     22 int TimeDelta::InHours() const {
     23  return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
     24 }
     25 
     26 int TimeDelta::InMinutes() const {
     27  return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
     28 }
     29 
     30 double TimeDelta::InSecondsF() const {
     31  return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
     32 }
     33 
     34 int64_t TimeDelta::InSeconds() const {
     35  return delta_ / Time::kMicrosecondsPerSecond;
     36 }
     37 
     38 double TimeDelta::InMillisecondsF() const {
     39  return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
     40 }
     41 
     42 int64_t TimeDelta::InMilliseconds() const {
     43  return delta_ / Time::kMicrosecondsPerMillisecond;
     44 }
     45 
     46 int64_t TimeDelta::InMicroseconds() const { return delta_; }
     47 
     48 // Time -----------------------------------------------------------------------
     49 
     50 // static
     51 Time Time::FromTimeT(time_t tt) {
     52  if (tt == 0) return Time();  // Preserve 0 so we can tell it doesn't exist.
     53  return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
     54 }
     55 
     56 time_t Time::ToTimeT() const {
     57  if (us_ == 0) return 0;  // Preserve 0 so we can tell it doesn't exist.
     58  return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
     59 }
     60 
     61 // static
     62 Time Time::FromDoubleT(double dt) {
     63  return Time((dt * static_cast<double>(kMicrosecondsPerSecond)) +
     64              kTimeTToMicrosecondsOffset);
     65 }
     66 
     67 double Time::ToDoubleT() const {
     68  if (us_ == 0) return 0;  // Preserve 0 so we can tell it doesn't exist.
     69  return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
     70          static_cast<double>(kMicrosecondsPerSecond));
     71 }
     72 
     73 Time Time::LocalMidnight() const {
     74  Exploded exploded;
     75  LocalExplode(&exploded);
     76  exploded.hour = 0;
     77  exploded.minute = 0;
     78  exploded.second = 0;
     79  exploded.millisecond = 0;
     80  return FromLocalExploded(exploded);
     81 }
     82 
     83 // static
     84 bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
     85  DCHECK((time_string != NULL) && (parsed_time != NULL));
     86  std::string ascii_time_string = SysWideToUTF8(time_string);
     87  if (ascii_time_string.length() == 0) return false;
     88  PRTime result_time = 0;
     89  PRStatus result =
     90      PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, &result_time);
     91  if (PR_SUCCESS != result) return false;
     92  result_time += kTimeTToMicrosecondsOffset;
     93  *parsed_time = Time(result_time);
     94  return true;
     95 }
     96 
     97 }  // namespace base