Home | History | Annotate | Download | only in net
      1 //
      2 // Copyright (C) 2012 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #ifndef SHILL_NET_SHILL_TIME_H_
     18 #define SHILL_NET_SHILL_TIME_H_
     19 
     20 #include <sys/time.h>
     21 #include <time.h>
     22 
     23 #include <string>
     24 
     25 #include <base/lazy_instance.h>
     26 
     27 #include "shill/net/shill_export.h"
     28 
     29 namespace shill {
     30 
     31 // Timestamp encapsulates a |monotonic| and a |boottime| clock that can be used
     32 // to compare the relative order and distance of events as well as a
     33 // |wall_clock| time that can be used for presenting the time in human-readable
     34 // format. Note that the monotonic clock does not necessarily advance during
     35 // suspend, while boottime clock does include any time that the system is
     36 // suspended.
     37 struct SHILL_EXPORT Timestamp {
     38   Timestamp() : monotonic{} {}
     39   Timestamp(const struct timeval& in_monotonic,
     40             const struct timeval& in_boottime,
     41             const std::string& in_wall_clock)
     42       : monotonic(in_monotonic),
     43         boottime(in_boottime),
     44         wall_clock(in_wall_clock) {}
     45 
     46   struct timeval monotonic;
     47   struct timeval boottime;
     48   std::string wall_clock;
     49 };
     50 
     51 // A "sys/time.h" abstraction allowing mocking in tests.
     52 class SHILL_EXPORT Time {
     53  public:
     54   virtual ~Time();
     55 
     56   static Time* GetInstance();
     57 
     58   // Returns CLOCK_MONOTONIC time, or 0 if a failure occurred.
     59   virtual bool GetSecondsMonotonic(time_t* seconds);
     60 
     61   // Returns CLOCK_BOOTTIME time, or 0 if a failure occurred.
     62   virtual bool GetSecondsBoottime(time_t* seconds);
     63 
     64   // On success, sets |tv| to CLOCK_MONOTONIC time, and returns 0.
     65   virtual int GetTimeMonotonic(struct timeval* tv);
     66 
     67   // On success, sets |tv| to CLOCK_BOOTTIME time, and returns 0.
     68   virtual int GetTimeBoottime(struct timeval* tv);
     69 
     70   // gettimeofday
     71   virtual int GetTimeOfDay(struct timeval* tv, struct timezone* tz);
     72 
     73   // Returns a snapshot of the current time.
     74   virtual Timestamp GetNow();
     75 
     76   virtual time_t GetSecondsSinceEpoch() const;
     77 
     78   static std::string FormatTime(const struct tm& date_time, suseconds_t usec);
     79 
     80  protected:
     81   Time();
     82 
     83  private:
     84   friend struct base::DefaultLazyInstanceTraits<Time>;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(Time);
     87 };
     88 
     89 }  // namespace shill
     90 
     91 #endif  // SHILL_NET_SHILL_TIME_H_
     92