Asked  7 Months ago    Answers:  5   Viewed   42 times

Is there a cross-platform way to get the current date and time in C++?



In C++ 11 you can use std::chrono::system_clock::now()

Example (copied from

#include <iostream>
#include <chrono>
#include <ctime>    

int main()
    auto start = std::chrono::system_clock::now();
    // Some computation here
    auto end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "sn";

This should print something like this:

finished computation at Mon Oct  2 00:59:08 2017
elapsed time: 1.88232s
Tuesday, June 1, 2021
answered 7 Months ago

You could use:

import java.util.Calendar

Date currentTime = Calendar.getInstance().getTime();

There are plenty of constants in Calendar for everything you need.

Check Calendar class documentation

Tuesday, June 1, 2021
answered 7 Months ago

From Wikipedia's Time in Australia article:

Australian Time Zones

Australia has two eastern time zones. In the northeast, UTC+10 applies all year. In the southeast, UTC+10 applies during standard time, and UTC+11 applies during daylight time (aka summer time).

The northeast region (Queensland) uses the IANA time zone identifier "Australia/Brisbane", while the southeast region (New South Wales) uses "Australia/Sydney". These correspond to the Windows time zone identifiers: "E. Australia Standard Time" and "AUS Eastern Standard Time" respectively.

  • If you are converting for Queensland, use "E. Australia Standard Time".

  • If you are converting for New South Wales, use "AUS Eastern Standard Time".

As to the confusing nature of these identifiers, see the section about Windows time zones in the timezone tag wiki.

If you want to use the standard IANA identifiers instead, use Noda Time.

Friday, August 27, 2021
answered 4 Months ago

Try to check if the time is ambiguous or a valid time. Due to the daylight change the time you mentioned i.e, 2:41:00 AM doesn not exist since the clock was moved 1 hour ahead and hence the date is invalid or ambiguous.

2016    Sun, 13 Mar, 02:00  CST → CDT   +1 hour (DST start) UTC-5h
        Sun, 6 Nov, 02:00   CDT → CST   -1 hour (DST end)   UTC-6h

You can also refer to this blog: System.TimeZoneInfo: Working with Ambiguous and Invalid Points in Time

System.TimeZoneInfo (currently available as part of .NET Framework 3.5 Beta 1) contains methods for checking if a DateTime instance represents an ambiguous or invalid time in a specific time zone. These methods are particularly useful for validating user-supplied points in time.

Background Information

Time zones that adjust their time for Daylight Saving Time (in most cases by moving the clock time back or forward by 1 hour) have gaps and repeats in the timeline — wherever the clock time was moved forward or back by the adjustment. Let’s use Pacific Standard Time as an example. In 2007 Pacific Standard Time (PST) changes to Pacific Daylight Time (PDT) at 02:00AM (“spring forward”) on the second Sunday in March and then returns at 02:00AM (“fall back”) on the first Sunday in November

To check if the time is valid you can use:

Friday, October 29, 2021
answered 1 Month ago

If you really want to know whether two Date objects represent precisely the same time, or are before/after one another, it's quite easy: just compare the two Dates via the getTime() method, which returns an integer timestamp for the object. For example,

var date1 = myDate,
    date2 = new Date();
return (date1.getTime() < date2.getTime());

would return true if myDate is before 'now', false if it is now or in the future.

Tuesday, November 30, 2021
answered 7 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :