<commit_before>#include "Time.hpp"

#include <chrono>
#include <iostream>
#include <iomanip>
#include <ratio>
#include <sstream>

#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif

#include <cassert>

using namespace Util;

#ifdef _MSC_VER
std::chrono::system_clock::time_point ClockImpl::sSystemStart;
Timestamp ClockImpl::sStart;
int64_t ClockImpl::sFreq = ClockImpl::init();

Timestamp ClockImpl::now()
{
    QueryPerformanceFrequency((LARGE_INTEGER*)&sFreq);
    sFreq /= 1000;

    int64_t count;
    QueryPerformanceCounter((LARGE_INTEGER*)&count);

    return time_point(duration(count / sFreq));
}

time_t ClockImpl::to_time_t(const time_point& tp)
{
    using std::chrono::system_clock;
    using std::chrono::duration_cast;
    return system_clock::to_time_t(system_clock::time_point(sSystemStart + duration_cast<system_clock::duration>(tp - sStart)));
}

int64_t ClockImpl::init()
{
    QueryPerformanceFrequency((LARGE_INTEGER*)&sFreq);
    sFreq /= 1000;

    sStart = now();
    sSystemStart = std::chrono::system_clock::now();

    return sFreq;
}

#endif


std::ostream& operator<<(std::ostream& os, const Timestamp& out)
{
    std::time_t time = ClockImpl::to_time_t(out);

#ifdef _MSC_VER
    tm tm_t;
    assert(localtime_s(&tm_t, &time) == 0);

    char temp[256];
    auto len = strftime(temp, 256, "%Y-%m-%d %H:%M:%S", &tm_t);

    os << std::string(temp, len);
#else
    typedef std::ostreambuf_iterator<char, std::char_traits<char>> Iter;
    typedef std::time_put<char, Iter> TimePut;
    Iter begin = Iter(os.rdbuf());
    Iter end = std::use_facet<TimePut>(os.getloc()).put(begin, os, os.fill(), std::localtime(&time), "%F %T", "%F %T" + std::char_traits<char>::length("%F %T"));<commit_msg>Improvements to the Windows clock<commit_after>#include "Time.hpp"

#include <chrono>
#include <iostream>
#include <iomanip>
#include <ratio>
#include <sstream>

#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif

#include <cassert>

using namespace Util;

#ifdef _MSC_VER
std::chrono::system_clock::time_point ClockImpl::sSystemStart;
Timestamp ClockImpl::sStart;
int64_t ClockImpl::sFreq = ClockImpl::init();

Timestamp ClockImpl::now()
{
    int64_t count = 0;
    HANDLE currentThread = GetCurrentThread();
    DWORD_PTR previousMask = SetThreadAffinityMask(currentThread, 1);

    QueryPerformanceCounter((LARGE_INTEGER*)&count);

    SetThreadAffinityMask(currentThread, previousMask);

    return time_point(duration(count / sFreq));
}

time_t ClockImpl::to_time_t(const time_point& tp)
{
    using std::chrono::system_clock;
    using std::chrono::duration_cast;
    return system_clock::to_time_t(system_clock::time_point(sSystemStart + duration_cast<system_clock::duration>(tp - sStart)));
}

int64_t ClockImpl::init()
{
    HANDLE currentThread = GetCurrentThread();
    DWORD_PTR previousMask = SetThreadAffinityMask(currentThread, 1);

    QueryPerformanceFrequency((LARGE_INTEGER*)&sFreq);

    SetThreadAffinityMask(currentThread, previousMask);
    sFreq /= 1000;

    sStart = now();
    sSystemStart = std::chrono::system_clock::now();

    return sFreq;
}

#endif


std::ostream& operator<<(std::ostream& os, const Timestamp& out)
{
    std::time_t time = ClockImpl::to_time_t(out);

#ifdef _MSC_VER
    tm tm_t;
    assert(localtime_s(&tm_t, &time) == 0);

    char temp[256];
    auto len = strftime(temp, 256, "%Y-%m-%d %H:%M:%S", &tm_t);

    os << std::string(temp, len);
#else
    typedef std::ostreambuf_iterator<char, std::char_traits<char>> Iter;
    typedef std::time_put<char, Iter> TimePut;
    Iter begin = Iter(os.rdbuf());
    Iter end = std::use_facet<TimePut>(os.getloc()).put(begin, os, os.fill(), std::localtime(&time), "%F %T", "%F %T" + std::char_traits<char>::length("%F %T"));
{'fraction_non_alphanumeric': 0.15635604280866613, 'fraction_numerical': 0.011746280344557557, 'mean_word_length': 5.641247833622184, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3472131', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1375, 'n_words': 265}