project_files/hwc/rtl/sysutils.c
changeset 10015 4feced261c68
parent 8047 25a4daa6473c
child 10137 a4537aab4117
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
       
     1 #include "SysUtils.h"
       
     2 
       
     3 #include <time.h>
       
     4 #include <stdio.h>
       
     5 #include <stdlib.h>
       
     6 #include <string.h>
       
     7 
       
     8 #include "system.h"
       
     9 #include "misc.h"
       
    10 
       
    11 TDateTime fpcrtl_date()
       
    12 {
       
    13     const int num_days_between_1900_1980 = 29220;
       
    14 
       
    15     struct tm ref_date;
       
    16     struct tm cur_date;
       
    17     time_t local_time;
       
    18     time_t ref_time, cur_time;
       
    19 
       
    20     double timeDiff;
       
    21     double day_time_frac; //fraction that represents the time in one day
       
    22     int num_seconds;
       
    23     int numDays;
       
    24 
       
    25     // unix epoch doesn't work, choose Jan 1st 1980 instead
       
    26     ref_date.tm_year = 80;
       
    27     ref_date.tm_mon = 0;
       
    28     ref_date.tm_mday = 1;
       
    29     ref_date.tm_hour = 0;
       
    30     ref_date.tm_min = 0;
       
    31     ref_date.tm_sec = 0;
       
    32     ref_date.tm_isdst = 0;
       
    33     ref_date.tm_wday = 0; // ignored
       
    34     ref_date.tm_yday = 0; // ignored
       
    35 
       
    36     local_time = time(NULL);
       
    37     cur_date = *localtime(&local_time);
       
    38 
       
    39     cur_date.tm_hour = 0;
       
    40     cur_date.tm_min = 0;
       
    41     cur_date.tm_sec = 0;
       
    42 
       
    43     ref_time = mktime(&ref_date);
       
    44     cur_time = mktime(&cur_date);
       
    45 
       
    46     timeDiff = difftime(cur_time, ref_time);
       
    47     numDays = fpcrtl_round(timeDiff / 3600 / 24) + num_days_between_1900_1980 + 1;
       
    48 
       
    49     fpcrtl_printf("[date] tim diff: %f\n", timeDiff);
       
    50     fpcrtl_printf("[date] num days between 1980 and today:  %d\n", fpcrtl_round(timeDiff/3600/24));
       
    51     fpcrtl_printf("[date] current date: %s\n", asctime(&cur_date));
       
    52     fpcrtl_printf("[date] reference date: %s\n", asctime(&ref_date));
       
    53     fpcrtl_printf("[date] num days: %d\n", numDays);
       
    54 
       
    55     return numDays;
       
    56 }
       
    57 
       
    58 TDateTime fpcrtl_time()
       
    59 {
       
    60     struct tm cur_date;
       
    61     time_t local_time;
       
    62     time_t cur_time;
       
    63 
       
    64     double day_time_frac; //fraction that represents the time in one day
       
    65     int num_seconds;
       
    66 
       
    67     local_time = time(NULL);
       
    68     cur_date = *localtime(&local_time);
       
    69 
       
    70     num_seconds = cur_date.tm_hour * 3600 + cur_date.tm_min * 60 + cur_date.tm_sec;
       
    71     day_time_frac = num_seconds / 3600.0 / 24.0;
       
    72 
       
    73     fpcrtl_printf("%f\n", day_time_frac);
       
    74 
       
    75     return day_time_frac;
       
    76 }
       
    77 
       
    78 TDateTime fpcrtl_now()
       
    79 {
       
    80     return fpcrtl_date() + fpcrtl_time();
       
    81 }
       
    82 
       
    83 /*
       
    84  * XXX: dummy
       
    85  */
       
    86 string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime)
       
    87 {
       
    88     string255 result = STRINIT("2012 01-01");
       
    89     return result;
       
    90 }
       
    91 
       
    92 string255 fpcrtl_trim(string255 s)
       
    93 {
       
    94     int left, right;
       
    95 
       
    96     if(s.len == 0){
       
    97         return s;
       
    98     }
       
    99 
       
   100     for(left = 0; left < s.len; left++)
       
   101     {
       
   102         if(s.str[left] != ' '){
       
   103             break;
       
   104         }
       
   105     }
       
   106 
       
   107     for(right = s.len - 1; right >= 0; right--)
       
   108     {
       
   109         if(s.str[right] != ' '){
       
   110             break;
       
   111         }
       
   112     }
       
   113 
       
   114     if(left > right){
       
   115         s.len = 0;
       
   116         s.str[0] = 0;
       
   117         return s;
       
   118     }
       
   119 
       
   120     s.len = right - left + 1;
       
   121     memmove(s.str, s.str + left, s.len);
       
   122 
       
   123     s.str[s.len] = 0;
       
   124 
       
   125     return s;
       
   126 }
       
   127 
       
   128 Integer fpcrtl_strToInt(string255 s)
       
   129 {
       
   130     s.str[s.len] = 0;
       
   131     return atoi(s.str);
       
   132 }
       
   133 
       
   134 //function ExtractFileName(const FileName: string): string;
       
   135 //var
       
   136 //  i : longint;
       
   137 //  EndSep : Set of Char;
       
   138 //begin
       
   139 //  I := Length(FileName);
       
   140 //  EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
       
   141 //  while (I > 0) and not (FileName[I] in EndSep) do
       
   142 //    Dec(I);
       
   143 //  Result := Copy(FileName, I + 1, MaxInt);
       
   144 //end;
       
   145 
       
   146 string255 fpcrtl_extractFileName(string255 f)
       
   147 {
       
   148     const char sep[] = {'\\', '/', ':'};
       
   149     LongInt i,j;
       
   150 
       
   151     i = f.len - 1;
       
   152     while(i >= 0){
       
   153         for(j = 0; j < sizeof(sep); j++){
       
   154             if(f.str[i] == sep[j]){
       
   155                 goto FPCRTL_EXTRACTFILENAME_END;
       
   156             }
       
   157         }
       
   158         i--;
       
   159     }
       
   160 FPCRTL_EXTRACTFILENAME_END:
       
   161     return fpcrtl_copy(f, i + 2, 256);
       
   162 }
       
   163 
       
   164 string255 fpcrtl_strPas(PChar p)
       
   165 {
       
   166     string255 s;
       
   167     int l = strlen(p);
       
   168 
       
   169     if(l > 255){
       
   170         printf("strPas: source string length > 255\n");
       
   171         assert(0);
       
   172     }
       
   173 
       
   174     s.len = l;
       
   175     strcpy(s.str, p);
       
   176 
       
   177     return s;
       
   178 }