logo

const class

sys::Duration

sys::Obj
  sys::Duration
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   11 Dec 05  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** Duration represents a relative duration of time with nanosecond precision.
  11  **
  12  const final class Duration
  13  {
  14  
  15  //////////////////////////////////////////////////////////////////////////
  16  // Construction
  17  //////////////////////////////////////////////////////////////////////////
  18  
  19    **
  20    ** Get the current value of the system timer.  This method returns
  21    ** a relative time unrelated to system or wall-clock time.  Typically
  22    ** it is the number of nanosecond ticks which have elapsed since system
  23    ** startup.
  24    **
  25    static Duration now()
  26  
  27    **
  28    ** Create a Duration which represents the specified number of nanosecond ticks.
  29    **
  30    static Duration make(Int ticks)
  31  
  32    **
  33    ** Parse a Str into a Duration according to the Fan
  34    ** [literal format]`docLang::Literals#duration`.
  35    ** If invalid format and checked is false return null,
  36    ** otherwise throw ParseErr.  The following suffixes
  37    ** are supported:
  38    **   ns:   nanoseconds  (x 1)
  39    **   ms:   milliseconds (x 1,000,000)
  40    **   sec:  seconds      (x 1,000,000,000)
  41    **   min:  minutes      (x 60,000,000,000)
  42    **   hr:   hours        (x 3,600,000,000,000)
  43    **   day:  days         (x 86,400,000,000,000)
  44    **
  45    ** Examples:
  46    **   Duration.fromStr("4ns")
  47    **   Duration.fromStr("100ms")
  48    **   Duration.fromStr("-0.5hr")
  49    **
  50    static Duration fromStr(Str s, Bool checked := true)
  51  
  52    **
  53    ** Private constructor.
  54    **
  55    private new privateMake()
  56  
  57  //////////////////////////////////////////////////////////////////////////
  58  // Obj Overrides
  59  //////////////////////////////////////////////////////////////////////////
  60  
  61    **
  62    ** Return true if same number nanosecond ticks.
  63    **
  64    override Bool equals(Obj obj)
  65  
  66    **
  67    ** Compare based on nanosecond ticks.
  68    **
  69    override Int compare(Obj obj)
  70  
  71    **
  72    ** Return ticks().
  73    **
  74    override Int hash()
  75  
  76    **
  77    ** Return string representation of the duration which is a valid
  78    ** duration literal format suitable for decoding via `fromStr`.
  79    **
  80    override Str toStr()
  81  
  82  //////////////////////////////////////////////////////////////////////////
  83  // Methods
  84  //////////////////////////////////////////////////////////////////////////
  85  
  86    **
  87    ** Return number of nanosecond ticks.
  88    **
  89    Int ticks()
  90  
  91    **
  92    ** Negative of this.  Shortcut is -a.
  93    **
  94    Duration negate()
  95  
  96    **
  97    ** Multiply this with b.  Shortcut is a*b.
  98    **
  99    Duration star(Float b)
 100  
 101    **
 102    ** Divide this by b.  Shortcut is a/b.
 103    **
 104    Duration slash(Float b)
 105  
 106    **
 107    ** Add this with b.  Shortcut is a+b.
 108    **
 109    Duration plus(Duration b)
 110  
 111    **
 112    ** Subtract b from this.  Shortcut is a-b.
 113    **
 114    Duration minus(Duration b)
 115  
 116  //////////////////////////////////////////////////////////////////////////
 117  // Conversion
 118  //////////////////////////////////////////////////////////////////////////
 119  
 120    **
 121    ** Get this duration in milliseconds (there may be a loss of precision).
 122    **
 123    Int toMillis()
 124  
 125  }