<commit_before>/*
 * Copyright Copyright 2012, System Insights, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/* Dlib library */
#include "../lib/dlib/all/source.cpp"

#include "globals.hpp"
#include <time.h>
#include <stdio.h>
#if 0
#ifdef _WINDOWS
#include <psapi.h>
#endif
#endif

using namespace std;

string int64ToString(uint64_t i)
{
  ostringstream stm;
  stm << i;
  return stm.str();
}

string intToString(unsigned int i)
{
  ostringstream stm;
  stm << i;
  return stm.str();
}



string floatToString(double f)
{
  char s[32];
  sprintf(s, "%.7g", f);
  return (string) s;
}

string toUpperCase(string& text)
{
  for (unsigned int i = 0; i < text.length(); i++)
  {
    text[i] = toupper(text[i]);
  }
  
  return text;
}

bool isNonNegativeInteger(const string& s)
{
  for (unsigned int i = 0; i < s.length(); i++)
  {
    if (!isdigit(s[i]))
    {
      return false;
    }
  }
  
  return true;
}

string getCurrentTime(TimeFormat format)
{
#ifdef _WINDOWS
  SYSTEMTIME st;
  char timestamp[64];
  GetSystemTime(&st);
  sprintf(timestamp, "%4d-%02d-%02dT%02d:%02d:%02d", st.wYear, st.wMonth,
    st.wDay, st.wHour, st.wMinute, st.wSecond);
  
  if (format == GMT_UV_SEC)
  {
    sprintf(timestamp + strlen(timestamp), ".%03dZ", st.wMilliseconds);
  }
  else
  {
    strcat(timestamp, "Z");
  }
  
  return timestamp;
#else
  char timeBuffer[50];
  struct tm * timeinfo;
  struct timeval tv;
  struct timezone tz;

  gettimeofday(&tv, &tz);
  timeinfo = (format == LOCAL) ? localtime(&tv.tv_sec) : gmtime(&tv.tv_sec);
  
  switch (format)
  {
    case HUM_READ:
      strftime(timeBuffer, 50, "%a, %d %b %Y %H:%M:%S %Z", timeinfo);
      break;
    case GMT:
      strftime(timeBuffer, 50, "%Y-%m-%dT%H:%M:%SZ", timeinfo);
      break;
    case GMT_UV_SEC:
      strftime(timeBuffer, 50, "%Y-%m-%dT%H:%M:%S", timeinfo);
      break;
    case LOCAL:
      strftime(timeBuffer, 50, "%Y-%m-%dT%H:%M:%S%z", timeinfo);
      break;
  }
  
  
  if (format == GMT_UV_SEC)
  {
    sprintf(timeBuffer + strlen(timeBuffer), ".%06dZ", tv.tv_usec);
  }
  
  return string(timeBuffer);
#endif
}

uint64_t getCurrentTimeInMicros()
{
  uint64_t now;
#ifdef _WINDOWS
  SYSTEMTIME st;
  GetSystemTime(&st);
  now = st.wSecond * 1000000;
  now += st.wMilliseconds;
#else
  struct timeval tv;<commit_msg>Fixed windows header date. Now RFC 822 format.<commit_after>/*
 * Copyright Copyright 2012, System Insights, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/* Dlib library */
#include "../lib/dlib/all/source.cpp"

#include "globals.hpp"
#include <time.h>
#include <stdio.h>
#if 0
#ifdef _WINDOWS
#include <psapi.h>
#endif
#endif

#ifdef _WINDOWS
#define localtime_r(t, tm) localtime_s(tm, t)
#define gmtime_r(t, tm) gmtime_s(tm, t)

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif

#endif

using namespace std;

string int64ToString(uint64_t i)
{
  ostringstream stm;
  stm << i;
  return stm.str();
}

string intToString(unsigned int i)
{
  ostringstream stm;
  stm << i;
  return stm.str();
}



string floatToString(double f)
{
  char s[32];
  sprintf(s, "%.7g", f);
  return (string) s;
}

string toUpperCase(string& text)
{
  for (unsigned int i = 0; i < text.length(); i++)
  {
    text[i] = toupper(text[i]);
  }
  
  return text;
}

bool isNonNegativeInteger(const string& s)
{
  for (unsigned int i = 0; i < s.length(); i++)
  {
    if (!isdigit(s[i]))
    {
      return false;
    }
  }
  
  return true;
}

string getCurrentTime(TimeFormat format)
{
  char timestamp[64];
  struct tm timeinfo;
  time_t sec;
  long usec;

  if (format == GMT_UV_SEC)
  {
#ifdef _WINDOWS
    FILETIME ft;
    unsigned __int64 tmpres = 0;
    static int tzflag;

    GetSystemTimeAsFileTime(&ft);
    
    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;
    
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS;
    sec = (long)(tmpres / 1000000UL);
    usec = (long)(tmpres % 1000000UL);  
#else
    struct timeval tv;
    gettimeofday(&tv, NULL);
    sec = tv.tv_sec;
    usec = tv.tv_usec;
#endif
  }
  else
  {
    sec = time(NULL);
  }

  if (format == LOCAL)
    localtime_r(&sec, &timeinfo);
  else
    gmtime_r(&sec, &timeinfo);

  switch (format)
  {
    case HUM_READ:
      strftime(timestamp, 50, "%a, %d %b %Y %H:%M:%S %Z", &timeinfo);
      break;
    case GMT:
      strftime(timestamp, 50, "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
      break;
    case GMT_UV_SEC:
      strftime(timestamp, 50, "%Y-%m-%dT%H:%M:%S", &timeinfo);
      break;
    case LOCAL:
      strftime(timestamp, 50, "%Y-%m-%dT%H:%M:%S%z", &timeinfo);
      break;
  }
  
  if (format == GMT_UV_SEC)
  {
    sprintf(timestamp + strlen(timestamp), ".%06dZ", usec);
  }
  else
  {
    strcat(timestamp, "Z");
  }
  
  return string(timestamp);
}

uint64_t getCurrentTimeInMicros()
{
  uint64_t now;
#ifdef _WINDOWS
  SYSTEMTIME st;
  GetSystemTime(&st);
  now = st.wSecond * 1000000;
  now += st.wMilliseconds;
#else
  struct timeval tv;
{'fraction_non_alphanumeric': 0.13801452784503632, 'fraction_numerical': 0.025343018563357547, 'mean_word_length': 3.189317106152806, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5240393', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2293, 'n_words': 613}