Asked  7 Months ago    Answers:  5   Viewed   42 times

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

 Answers

25

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

Example (copied from en.cppreference.com):

#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
 
Shobit
answered 7 Months ago
43

You could use:

import java.util.Calendar

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

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

Edit:
Check Calendar class documentation

Tuesday, June 1, 2021
 
DMTintner
answered 7 Months ago
61

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
 
Easen
answered 4 Months ago
54

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:

TimeZoneInfo.IsInvalidTime
Friday, October 29, 2021
 
fhonics
answered 1 Month ago
79

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
 
superfell
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 :
 
Share