tor-browser

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

TemporalUnit.h (4678B)


      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_temporal_TemporalUnit_h
      8 #define builtin_temporal_TemporalUnit_h
      9 
     10 #include "mozilla/Assertions.h"
     11 
     12 #include <stdint.h>
     13 
     14 namespace js::temporal {
     15 enum class TemporalUnit {
     16  Unset,
     17  Auto,
     18  Year,
     19  Month,
     20  Week,
     21  Day,
     22  Hour,
     23  Minute,
     24  Second,
     25  Millisecond,
     26  Microsecond,
     27  Nanosecond
     28 };
     29 
     30 constexpr int64_t ToNanoseconds(TemporalUnit unit) {
     31  switch (unit) {
     32    case TemporalUnit::Day:
     33      return 86'400'000'000'000;
     34    case TemporalUnit::Hour:
     35      return 3'600'000'000'000;
     36    case TemporalUnit::Minute:
     37      return 60'000'000'000;
     38    case TemporalUnit::Second:
     39      return 1'000'000'000;
     40    case TemporalUnit::Millisecond:
     41      return 1'000'000;
     42    case TemporalUnit::Microsecond:
     43      return 1'000;
     44    case TemporalUnit::Nanosecond:
     45      return 1;
     46 
     47    case TemporalUnit::Unset:
     48    case TemporalUnit::Auto:
     49    case TemporalUnit::Year:
     50    case TemporalUnit::Month:
     51    case TemporalUnit::Week:
     52      break;
     53  }
     54  MOZ_CRASH("Unexpected temporal unit");
     55 }
     56 
     57 constexpr int64_t ToMicroseconds(TemporalUnit unit) {
     58  switch (unit) {
     59    case TemporalUnit::Day:
     60      return 86'400'000'000;
     61    case TemporalUnit::Hour:
     62      return 3'600'000'000;
     63    case TemporalUnit::Minute:
     64      return 60'000'000;
     65    case TemporalUnit::Second:
     66      return 1'000'000;
     67    case TemporalUnit::Millisecond:
     68      return 1'000;
     69    case TemporalUnit::Microsecond:
     70      return 1;
     71 
     72    case TemporalUnit::Unset:
     73    case TemporalUnit::Auto:
     74    case TemporalUnit::Year:
     75    case TemporalUnit::Month:
     76    case TemporalUnit::Week:
     77    case TemporalUnit::Nanosecond:
     78      break;
     79  }
     80  MOZ_CRASH("Unexpected temporal unit");
     81 }
     82 
     83 constexpr int64_t ToMilliseconds(TemporalUnit unit) {
     84  switch (unit) {
     85    case TemporalUnit::Day:
     86      return 86'400'000;
     87    case TemporalUnit::Hour:
     88      return 3'600'000;
     89    case TemporalUnit::Minute:
     90      return 60'000;
     91    case TemporalUnit::Second:
     92      return 1'000;
     93    case TemporalUnit::Millisecond:
     94      return 1;
     95 
     96    case TemporalUnit::Unset:
     97    case TemporalUnit::Auto:
     98    case TemporalUnit::Year:
     99    case TemporalUnit::Month:
    100    case TemporalUnit::Week:
    101    case TemporalUnit::Microsecond:
    102    case TemporalUnit::Nanosecond:
    103      break;
    104  }
    105  MOZ_CRASH("Unexpected temporal unit");
    106 }
    107 
    108 constexpr int64_t ToSeconds(TemporalUnit unit) {
    109  switch (unit) {
    110    case TemporalUnit::Day:
    111      return 86'400;
    112    case TemporalUnit::Hour:
    113      return 3'600;
    114    case TemporalUnit::Minute:
    115      return 60;
    116    case TemporalUnit::Second:
    117      return 1;
    118 
    119    case TemporalUnit::Unset:
    120    case TemporalUnit::Auto:
    121    case TemporalUnit::Year:
    122    case TemporalUnit::Month:
    123    case TemporalUnit::Week:
    124    case TemporalUnit::Millisecond:
    125    case TemporalUnit::Microsecond:
    126    case TemporalUnit::Nanosecond:
    127      break;
    128  }
    129  MOZ_CRASH("Unexpected temporal unit");
    130 }
    131 
    132 constexpr int64_t UnitsPerDay(TemporalUnit unit) {
    133  switch (unit) {
    134    case TemporalUnit::Day:
    135      return 1;
    136    case TemporalUnit::Hour:
    137      return 24;
    138    case TemporalUnit::Minute:
    139      return 1440;
    140    case TemporalUnit::Second:
    141      return 86'400;
    142    case TemporalUnit::Millisecond:
    143      return 86'400'000;
    144    case TemporalUnit::Microsecond:
    145      return 86'400'000'000;
    146    case TemporalUnit::Nanosecond:
    147      return 86'400'000'000'000;
    148 
    149    case TemporalUnit::Unset:
    150    case TemporalUnit::Auto:
    151    case TemporalUnit::Year:
    152    case TemporalUnit::Month:
    153    case TemporalUnit::Week:
    154      break;
    155  }
    156  MOZ_CRASH("Unexpected temporal unit");
    157 }
    158 
    159 constexpr const char* TemporalUnitToString(TemporalUnit unit) {
    160  switch (unit) {
    161    case TemporalUnit::Unset:
    162      return "unset";
    163    case TemporalUnit::Auto:
    164      return "auto";
    165    case TemporalUnit::Year:
    166      return "year";
    167    case TemporalUnit::Month:
    168      return "month";
    169    case TemporalUnit::Week:
    170      return "week";
    171    case TemporalUnit::Day:
    172      return "day";
    173    case TemporalUnit::Hour:
    174      return "hour";
    175    case TemporalUnit::Minute:
    176      return "minute";
    177    case TemporalUnit::Second:
    178      return "second";
    179    case TemporalUnit::Millisecond:
    180      return "millisecond";
    181    case TemporalUnit::Microsecond:
    182      return "microsecond";
    183    case TemporalUnit::Nanosecond:
    184      return "nanosecond";
    185  }
    186  MOZ_CRASH("Unexpected temporal unit");
    187 }
    188 
    189 } /* namespace js::temporal */
    190 
    191 #endif /* builtin_temporal_TemporalUnit_h */