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