project_files/hwc/rtl/sysutils.c
author Wuzzy <almikes@aol.com>
Sat, 30 Sep 2017 23:52:08 +0200
changeset 12627 07fdda8c13a2
parent 10564 0cb20aa8877a
child 14918 68e1783762bc
permissions -rw-r--r--
TrophyRace: Fix game never eliminating any hogs after a hog skipped or ran out of time Warning: This commit _might_ invalidate past records, but I'm not sure if this is actually the case. Note that only the eliminiation part of the script is touched, not the actual race logic. Even if records are actually broken by this, I and sheepluva have decided that it's more imporant to fix this very, VERY stupid and old bug than to preserve records.

#include "SysUtils.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "system.h"
#include "misc.h"

TDateTime fpcrtl_date()
{
    const int num_days_between_1900_1980 = 29220;

    struct tm ref_date;
    struct tm cur_date;
    time_t local_time;
    time_t ref_time, cur_time;

    double timeDiff;
    double day_time_frac; //fraction that represents the time in one day
    int num_seconds;
    int numDays;

    // unix epoch doesn't work, choose Jan 1st 1980 instead
    ref_date.tm_year = 80;
    ref_date.tm_mon = 0;
    ref_date.tm_mday = 1;
    ref_date.tm_hour = 0;
    ref_date.tm_min = 0;
    ref_date.tm_sec = 0;
    ref_date.tm_isdst = 0;
    ref_date.tm_wday = 0; // ignored
    ref_date.tm_yday = 0; // ignored

    local_time = time(NULL);
    cur_date = *localtime(&local_time);

    cur_date.tm_hour = 0;
    cur_date.tm_min = 0;
    cur_date.tm_sec = 0;

    ref_time = mktime(&ref_date);
    cur_time = mktime(&cur_date);

    timeDiff = difftime(cur_time, ref_time);
    numDays = fpcrtl_round(timeDiff / 3600 / 24) + num_days_between_1900_1980 + 1;

    fpcrtl_printf("[date] tim diff: %f\n", timeDiff);
    fpcrtl_printf("[date] num days between 1980 and today:  %d\n", fpcrtl_round(timeDiff/3600/24));
    fpcrtl_printf("[date] current date: %s\n", asctime(&cur_date));
    fpcrtl_printf("[date] reference date: %s\n", asctime(&ref_date));
    fpcrtl_printf("[date] num days: %d\n", numDays);

    return numDays;
}

TDateTime fpcrtl_time()
{
    struct tm cur_date;
    time_t local_time;
    time_t cur_time;

    double day_time_frac; //fraction that represents the time in one day
    int num_seconds;

    local_time = time(NULL);
    cur_date = *localtime(&local_time);

    num_seconds = cur_date.tm_hour * 3600 + cur_date.tm_min * 60 + cur_date.tm_sec;
    day_time_frac = num_seconds / 3600.0 / 24.0;

    fpcrtl_printf("%f\n", day_time_frac);

    return day_time_frac;
}

TDateTime fpcrtl_now()
{
    return fpcrtl_date() + fpcrtl_time();
}

/*
 * XXX: dummy
 */
string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime)
{
    string255 result = STRINIT("2012 01-01");
    return result;
}

string255 fpcrtl_trim(string255 s)
{
    int left, right;

    if(s.len == 0){
        return s;
    }

    for(left = 0; left < s.len; left++)
    {
        if(s.str[left] != ' '){
            break;
        }
    }

    for(right = s.len - 1; right >= 0; right--)
    {
        if(s.str[right] != ' '){
            break;
        }
    }

    if(left > right){
        s.len = 0;
        s.str[0] = 0;
        return s;
    }

    s.len = right - left + 1;
    memmove(s.str, s.str + left, s.len);

    s.str[s.len] = 0;

    return s;
}

Integer fpcrtl_strToInt(string255 s)
{
    s.str[s.len] = 0;
    return atoi(s.str);
}

string255 fpcrtl_extractFileDir(string255 f)
{
    const char sep[] = {'\\', '/', ':'};
    LongInt i,j;

    i = f.len - 1;
    while(i >= 0){
        for(j = 0; j < sizeof(sep); j++){
            if(f.str[i] == sep[j]){
                goto FPCRTL_EXTRACTFILEDIR_END;
            }
        }
        i--;
    }
FPCRTL_EXTRACTFILEDIR_END:
    return fpcrtl_copy(f, 1, i);
}

//function ExtractFileName(const FileName: string): string;
//var
//  i : longint;
//  EndSep : Set of Char;
//begin
//  I := Length(FileName);
//  EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
//  while (I > 0) and not (FileName[I] in EndSep) do
//    Dec(I);
//  Result := Copy(FileName, I + 1, MaxInt);
//end;

string255 fpcrtl_extractFileName(string255 f)
{
    const char sep[] = {'\\', '/', ':'};
    LongInt i,j;

    i = f.len - 1;
    while(i >= 0){
        for(j = 0; j < sizeof(sep); j++){
            if(f.str[i] == sep[j]){
                goto FPCRTL_EXTRACTFILENAME_END;
            }
        }
        i--;
    }
FPCRTL_EXTRACTFILENAME_END:
    return fpcrtl_copy(f, i + 2, 256);
}

string255 fpcrtl_strPas(PChar p)
{
    return fpcrtl_pchar2str(p);
}