Time in AngelScript

// AngelScript offers support for times and durations; here are some examples.

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

void main()
{
    auto print = [](const auto& x) { std::cout << x << std::endl; };

    // We'll start by getting the current time.
    auto now = std::chrono::system_clock::now();
    print(std::chrono::system_clock::to_time_t(now));

    // You can build a time point by providing the year, month, day, etc.
    // Times are always associated with a time zone.
    std::tm then = {};
    then.tm_year = 2009 - 1900;
    then.tm_mon = 11 - 1;
    then.tm_mday = 17;
    then.tm_hour = 20;
    then.tm_min = 34;
    then.tm_sec = 58;
    auto then_tp = std::chrono::system_clock::from_time_t(std::mktime(&then));
    print(std::chrono::system_clock::to_time_t(then_tp));

    // You can extract the various components of the time value as expected.
    print(then.tm_year + 1900);
    print(then.tm_mon + 1);
    print(then.tm_mday);
    print(then.tm_hour);
    print(then.tm_min);
    print(then.tm_sec);

    // The day of the week is also available (0 = Sunday, 1 = Monday, etc.).
    print(then.tm_wday);

    // These comparisons test if the first occurs before, after, or at the same time
    // as the second, respectively.
    print(then_tp < now);
    print(then_tp > now);
    print(then_tp == now);

    // We can compute the duration between two time points.
    auto diff = now - then_tp;
    print(diff.count());

    // We can compute the length of the duration in various units.
    print(std::chrono::duration_cast<std::chrono::hours>(diff).count());
    print(std::chrono::duration_cast<std::chrono::minutes>(diff).count());
    print(std::chrono::duration_cast<std::chrono::seconds>(diff).count());
    print(std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count());

    // You can use addition to advance a time by a given duration, 
    // or subtraction to move backwards by a duration.
    print(std::chrono::system_clock::to_time_t(then_tp + diff));
    print(std::chrono::system_clock::to_time_t(then_tp - diff));
}

This AngelScript code demonstrates various operations with time and duration, similar to the original example. Here’s a breakdown of the main differences and adaptations:

  1. AngelScript uses C++’s standard library for time operations, so we include the necessary headers: <chrono>, <ctime>, and <iostream>.

  2. Instead of a time package, we use std::chrono and std::tm for time operations.

  3. The print function is simulated using a lambda that wraps std::cout.

  4. Time points are created using std::chrono::system_clock::now() and std::chrono::system_clock::from_time_t().

  5. Time components are accessed through a std::tm struct instead of individual methods.

  6. Durations are represented by std::chrono::duration objects.

  7. Time comparisons and arithmetic operations are performed using standard C++ operators.

  8. Duration conversions are done using std::chrono::duration_cast.

Note that AngelScript, being closely related to C++, uses many C++ standard library functions for time operations. The exact syntax and available functions may vary depending on the specific AngelScript implementation and bindings used.