time.cc revision 7840
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#include <iostream>
327840Snate@binkert.org#include <sstream>
332SN/A
34400SN/A#include "base/time.hh"
357840Snate@binkert.org#include "config/use_posix_clock.hh"
362SN/A
372SN/Ausing namespace std;
382SN/A
397840Snate@binkert.orgvoid
407840Snate@binkert.orgTime::_set(bool monotonic)
412SN/A{
427840Snate@binkert.org#if USE_POSIX_CLOCK
437840Snate@binkert.org    ::clock_gettime(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &_time);
447840Snate@binkert.org#else
45400SN/A    timeval tv;
467840Snate@binkert.org    ::gettimeofday(&tv, NULL);
477840Snate@binkert.org    operator=(tv);
487840Snate@binkert.org#endif
49400SN/A}
50400SN/A
51400SN/Astring
527840Snate@binkert.orgTime::date(const string &format) const
53400SN/A{
547840Snate@binkert.org    time_t sec = this->sec();
55400SN/A    char buf[256];
56400SN/A
57400SN/A    if (format.empty()) {
583918Ssaidi@eecs.umich.edu#ifdef __SUNPRO_CC
597840Snate@binkert.org        ctime_r(&sec, buf, sizeof(buf));
603918Ssaidi@eecs.umich.edu#else
61400SN/A        ctime_r(&sec, buf);
623918Ssaidi@eecs.umich.edu#endif
63400SN/A        buf[24] = '\0';
64400SN/A        return buf;
652SN/A    }
662SN/A
67400SN/A    struct tm *tm = localtime(&sec);
68400SN/A    strftime(buf, sizeof(buf), format.c_str(), tm);
69400SN/A    return buf;
70400SN/A}
712SN/A
727840Snate@binkert.orgstring
737840Snate@binkert.orgTime::time() const
74400SN/A{
757840Snate@binkert.org    double time = double(*this);
767840Snate@binkert.org    double secs = fmod(time, 60.0);
777840Snate@binkert.org    double all_mins = floor(time / 60.0);
787840Snate@binkert.org    double mins = fmod(all_mins, 60.0);
797840Snate@binkert.org    double hours = floor(all_mins / 60.0);
807840Snate@binkert.org
817840Snate@binkert.org    stringstream str;
827840Snate@binkert.org
837840Snate@binkert.org    if (hours > 0.0) {
847840Snate@binkert.org        if (hours < 10.0)
857840Snate@binkert.org            str << '0';
867840Snate@binkert.org        str << hours << ':';
877840Snate@binkert.org    }
887840Snate@binkert.org
897840Snate@binkert.org    if (mins > 0.0) {
907840Snate@binkert.org        if (mins < 10.0)
917840Snate@binkert.org            str << '0';
927840Snate@binkert.org        str << mins << ':';
937840Snate@binkert.org    }
947840Snate@binkert.org
957840Snate@binkert.org    if (secs < 10.0 && !str.str().empty())
967840Snate@binkert.org        str << '0';
977840Snate@binkert.org    str << secs;
987840Snate@binkert.org
997840Snate@binkert.org    return str.str();
100400SN/A}
1012SN/A
1027840Snate@binkert.orgvoid
1037840Snate@binkert.orgsleep(const Time &time)
104400SN/A{
1057840Snate@binkert.org    timespec ts = time;
1067840Snate@binkert.org
1077840Snate@binkert.org#if USE_POSIX_CLOCK
1087840Snate@binkert.org    clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
1097840Snate@binkert.org#else
1107840Snate@binkert.org    nanosleep(&ts, NULL);
1117840Snate@binkert.org#endif
112400SN/A}
113