hallo all,
i'm a new member.
i need help in my program
i want 2 know if there is a datatype called time.
my program will deal with time as below
11:33:50,965
it will read 2 absolute times for 2 actions and then return the differnce.
how can i do that.

example for the input file:
action y 11:22:50,333
action x 11:22:55,400


and it must read the 2 actions and then return the diffrence in millisecond:rolleyes:

Dani AI

Generated

Short answer: classic C/C++ doesn't have one built‑in “time” value that includes milliseconds — time_t is (on most systems) seconds-based, while modern C++ offers the chrono types (duration / time_point) for subsecond precision. For a logfile format like 11:22:50,333 the simplest, most portable solution is to parse hours/minutes/seconds + fractional seconds yourself and convert to a millisecond count (or std::chrono::milliseconds). (cppreference.com)

A small, robust approach: parse the HH:MM:SS part (e.g. with std::get_time) and then read the fractional digits after . or ,, normalize to 3 digits (milliseconds), and build a std::chrono::milliseconds value. Example (skim and adapt for your codebase):

#include <string>
#include <sstream>
#include <iomanip>
#include <chrono>
#include <stdexcept>

std::chrono::milliseconds parse_hh_mm_ss_ms(const std::string& input) {
    std::istringstream in(input);
    std::tm tm{};
    in >> std::get_time(&tm, "%H:%M:%S");
    int msec = 0;
    if (in && (in.peek() == '.' || in.peek() == ',')) {
        char sep = in.get();
        std::string frac;
        while (std::isdigit(in.peek())) frac.push_back(in.get());
        if (!frac.empty()) {
            if (frac.size() > 3) frac = frac.substr(0,3);
            while (frac.size() < 3) frac.push_back('0');
            msec = std::stoi(frac);
        }
    }
    auto dur = std::chrono::hours{tm.tm_hour} + std::chrono::minutes{tm.tm_min}
             + std::chrono::seconds{tm.tm_sec} + std::chrono::milliseconds{msec};
    return std::chrono::duration_cast<std::chrono::milliseconds>(dur);
}

std::int64_t diff_ms(const std::string& a, const std::string& b) {
    auto ta = parse_hh_mm_ss_ms(a);
    auto tb = parse_hh_mm_ss_ms(b);
    auto diff = (ta > tb) ? (ta - tb) : (tb - ta);
    return diff.count();
}

This uses std::get_time for the HH:MM:SS bit and manual fraction handling for the milliseconds. Adjust error handling and digit-normalization if your logs sometimes give microseconds or variable-length fractions. (en.cppreference.com)

Notes and alternatives: if you only need elapsed intervals inside a running program use the OS high‑resolution clocks (clock_gettime / clock_getres on POSIX, Windows high‑res APIs on Windows). If you need full timestamps with dates/timezones, use a proven library such as Boost.Date_Time or Howard Hinnant’s date/tz (or C++20 chrono calendar/time_of_day utilities when available). These libraries handle parsing, time zones and edge cases. (man7.org)

One caution: clock() (the C function) measures processor (CPU) time for the process, not wall‑clock time — it’s not suitable for measuring real elapsed time between log timestamps. , , and ’s pointers are all useful context: use time_t only for seconds, SYSTEMTIME/Win32 APIs when platform‑specific code is acceptable, and prefer std::chrono or a library for portable millisecond-accurate parsing. Also be careful about midnight wrap (or multi‑day logs): if a computed difference is negative you’ll need date context or add 24h logic. (sites.uclouvain.be)

Recommended Answers

All 5 Replies

look up <time.h> - it includes a data type time_t - however, that is only accurate to a second AFAIK. if you need anything more accurate, you'll need to create your own, or find a 3rd party library.

in MS-Windows you can call win32 api function GetSystemTime() that returns a SYSTEMTIME structure. Other operating systems probably have something similar, but I don't know what they would be.

Some stuff for dealing with dates and times is shown here and here. The milliseconds part is a bit non-standard, but you could perahps handle that separately.

// I think clock() is only available for Windows, a tick is ~ 1 ms
// if you need time like the string returned by ctime(), then you have to do some processing

#include <iostream>
#include <ctime>      // clock(), time(), ctime(), time_t 

using namespace std; 
 
int main()
{
  time_t t;

  time(&t);
  cout << "Today's date and time: " << ctime(&t) << endl;

   
  int clo(clock());         // start clock


  // put your function to be timed here


  int clo2(clock() - clo); // difference end clock - start clock

  cout << "time elapsed = " << clo2 << " ticks" << endl;

  cin.get();   // wait for key press
}

>>// I think clock() is only available for Windows,

clock() does NOT give you current date/time. It will only give you the number of milliseconds since your computer was last booted, or some rollover value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.