/*
 * Copyright 2015 Telenav Inc. All rights reserved.
 */
#ifndef TN_FOUNDATION_SIMPLE_TIME_HPP
#define TN_FOUNDATION_SIMPLE_TIME_HPP

#include <ctime>
#include "foundation/cxx11compat.hpp"
#include "foundation/mutex.hpp"
#include "foundation/condition_variable.hpp"

namespace tn {
    namespace simple_time {

        /**
         * Return the time in seconds since Epoch (Jan 1, 1970) with floating-point
         * precision. 
         */
        double system_time() TN_NOEXCEPT;

        /**
         * Return monotonically-increasing time in seconds since an unspecified point in time 
         * time. It is suitable for interval measurements because it won't go back
         * even on leap seconds or when system clock is changed. It may wrap if 
         * the interval is very long (i.e. longer than about a month). 
         */
        double steady_time() TN_NOEXCEPT;

        /** 
         * Convert time in seconds as returned by system_time to to time_t
         */
        inline time_t to_time_t(double seconds) TN_NOEXCEPT { return (std::time_t)seconds; }

        /**
         * Sleep current thread for a given number of seconds. E.g. sleep_for(0.01) sleeps for
         * 100 milliseconds.
         */
         void sleep_for(double seconds);

         /**
          * Block current thread till condition variable is notified or a given 
          * number of seconds passes
          *
          * @return true if the given number of seconds passed (i.e. timeout), false
          *     if the variable was notified
          */ 
         bool wait_for(condition_variable& cv, unique_lock<mutex>& lock, double seconds);


    } // namespace simple_time
} // namespace tn

#endif /* TN_FOUNDATION_SIMPLE_TIME_HPP */
