File:  [local] / rpl / src / instructions_j1.c
Revision 1.66: download - view: text, annotated - select for diffs - revision graph
Fri Jan 10 11:15:46 2020 UTC (4 years, 3 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_32, HEAD
Modification du copyright.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.32
    4:   Copyright (C) 1989-2020 Dr. BERTRAND Joël
    5: 
    6:   This file is part of RPL/2.
    7: 
    8:   RPL/2 is free software; you can redistribute it and/or modify it
    9:   under the terms of the CeCILL V2 License as published by the french
   10:   CEA, CNRS and INRIA.
   11:  
   12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
   13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
   15:   for more details.
   16:  
   17:   You should have received a copy of the CeCILL License
   18:   along with RPL/2. If not, write to info@cecill.info.
   19: ================================================================================
   20: */
   21: 
   22: 
   23: #include "rpl-conv.h"
   24: 
   25: 
   26: /*
   27: ================================================================================
   28:   Fonction 'jdate'
   29: ================================================================================
   30:   Entrées : pointeur sur une structure struct_processus
   31: --------------------------------------------------------------------------------
   32:   Sorties :
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: void
   39: instruction_jdate(struct_processus *s_etat_processus)
   40: {
   41:     integer8                a;
   42:     integer8                b;
   43:     integer8                c;
   44:     integer8                d;
   45:     integer8                e;
   46:     integer8                m;
   47:     integer8                jour_julien;
   48: 
   49:     long                    nombre_elements;
   50: 
   51:     real8                   heure_julienne;
   52: 
   53:     struct_liste_chainee    *l_element_courant;
   54: 
   55:     struct_objet            *s_objet;
   56:     struct_objet            *s_objet_argument;
   57: 
   58:     struct timeval          horodatage;
   59: 
   60:     struct tm               stm;
   61: 
   62:     (*s_etat_processus).erreur_execution = d_ex;
   63: 
   64:     if ((*s_etat_processus).affichage_arguments == 'Y')
   65:     {
   66:         printf("\n  JDATE ");
   67: 
   68:         if ((*s_etat_processus).langue == 'F')
   69:         {
   70:             printf("(conversion d'un jour julien vers un calendrier grégorien"
   71:                     " proleptique)\n\n");
   72:         }
   73:         else
   74:         {
   75:             printf("(conversion from julian day to proleptic gregorian"
   76:                     " calendar)\n\n");
   77:         }
   78: 
   79:         printf("    1: %s\n", d_LST);
   80:         printf("->  1: %s\n\n", d_LST);
   81: 
   82:         if ((*s_etat_processus).langue == 'F')
   83:         {
   84:             printf("  Utilisation :\n\n");
   85:             printf("    { IP FP } JDATE avec IP > 0, 0 <= FP < 1\n\n");
   86:             printf("  Note :\n\n");
   87:             printf("    La conversion n'est assurée que si la date est "
   88:                     "représentable en temps Unix.\n");
   89:         }
   90:         else
   91:         {
   92:             printf("  Usage:\n\n");
   93:             printf("    { IP FP } JDATE with IP > 0, 0 <= FP < 1\n\n");
   94:             printf("  Note :\n\n");
   95:             printf("    Conversion is only done if date can be computed in "
   96:                     "Unix time.\n");
   97:         }
   98: 
   99:         return;
  100:     }
  101:     else if ((*s_etat_processus).test_instruction == 'Y')
  102:     {
  103:         (*s_etat_processus).nombre_arguments = 1;
  104:         return;
  105:     }
  106: 
  107:     if (test_cfsf(s_etat_processus, 31) == d_vrai)
  108:     {
  109:         if (empilement_pile_last(s_etat_processus, 1) == d_erreur)
  110:         {
  111:             return;
  112:         }
  113:     }
  114: 
  115:     if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  116:             &s_objet_argument) == d_erreur)
  117:     {
  118:         (*s_etat_processus).erreur_execution = d_ex_manque_argument;
  119:         return;
  120:     }
  121: 
  122:     if ((*s_objet_argument).type == LST)
  123:     {
  124:         l_element_courant = (*s_objet_argument).objet;
  125:         nombre_elements = 0;
  126: 
  127:         while(l_element_courant != NULL)
  128:         {
  129:             nombre_elements++;
  130:             l_element_courant = (*l_element_courant).suivant;
  131:         }
  132: 
  133:         if (nombre_elements != 2)
  134:         {
  135:             (*s_etat_processus).erreur_execution = d_ex_argument_invalide;
  136: 
  137:             liberation(s_etat_processus, s_objet_argument);
  138:             return;
  139:         }
  140: 
  141:         l_element_courant = (*s_objet_argument).objet;
  142: 
  143:         if ((*(*l_element_courant).donnee).type == INT)
  144:         {
  145:             jour_julien = (*((integer8 *) (*(*l_element_courant)
  146:                     .donnee).objet));
  147: 
  148:             if (jour_julien < 0)
  149:             {
  150:                 (*s_etat_processus).erreur_execution = d_ex_argument_invalide;
  151: 
  152:                 liberation(s_etat_processus, s_objet_argument);
  153:                 return;
  154:             }
  155:         }
  156:         else
  157:         {
  158:             (*s_etat_processus).erreur_execution = d_ex_argument_invalide;
  159: 
  160:             liberation(s_etat_processus, s_objet_argument);
  161:             return;
  162:         }
  163: 
  164:         l_element_courant = (*l_element_courant).suivant;
  165: 
  166:         if ((*(*l_element_courant).donnee).type == INT)
  167:         {
  168:             heure_julienne = (real8) (*((integer8 *) (*(*l_element_courant)
  169:                     .donnee).objet));
  170:         }
  171:         else if ((*(*l_element_courant).donnee).type == REL)
  172:         {
  173:             heure_julienne = (*((real8 *) (*(*l_element_courant)
  174:                     .donnee).objet));
  175:         }
  176:         else
  177:         {
  178:             (*s_etat_processus).erreur_execution = d_ex_argument_invalide;
  179: 
  180:             liberation(s_etat_processus, s_objet_argument);
  181:             return;
  182:         }
  183: 
  184:         if ((heure_julienne < 0) || (heure_julienne >= 1))
  185:         {
  186:             (*s_etat_processus).erreur_execution = d_ex_argument_invalide;
  187: 
  188:             liberation(s_etat_processus, s_objet_argument);
  189:             return;
  190:         }
  191: 
  192:         a = jour_julien + 32045;
  193:         b = ((4 * (a + 36524)) / 146097) - 1;
  194:         c = a - ((146097 * b) / 4);
  195:         d = ((4 * (c + 365)) / 1461) - 1;
  196:         e = c - ((1461 * d) / 4);
  197:         m = ((5 * (e - 1)) + 2) / 153;
  198: 
  199:         stm.tm_mday = (int) (e - (((153 * m) + 2) / 5));
  200:         stm.tm_mon = (int) (m + 3 - ((12 * (m / 10)) + 1));
  201:         stm.tm_year = (int) ((100 * b) + (m / 10) + d - (4800 + 1900));
  202: 
  203:         heure_julienne *= 24;
  204:         stm.tm_hour = (int) floor(heure_julienne);
  205:         heure_julienne -= stm.tm_hour;
  206:         stm.tm_hour += 12;
  207: 
  208:         heure_julienne *= 60;
  209:         stm.tm_min = (int) floor(heure_julienne);
  210:         heure_julienne -= stm.tm_min;
  211: 
  212:         heure_julienne *= 60;
  213:         stm.tm_sec = (int) floor(heure_julienne);
  214:         heure_julienne -= stm.tm_sec;
  215: 
  216:         horodatage.tv_sec = mktime(&stm);
  217:         horodatage.tv_usec = (suseconds_t) (heure_julienne * 1000000);
  218: 
  219:         if (horodatage.tv_sec == -1)
  220:         {
  221:             liberation(s_etat_processus, s_objet_argument);
  222: 
  223:             (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
  224:             return;
  225:         }
  226: 
  227:         if ((s_objet = formateur_date(s_etat_processus, &horodatage)) == NULL)
  228:         {
  229:             liberation(s_etat_processus, s_objet_argument);
  230:             return;
  231:         }
  232: 
  233:         liberation(s_etat_processus, s_objet_argument);
  234:     }
  235:     else
  236:     {
  237:         liberation(s_etat_processus, s_objet_argument);
  238: 
  239:         (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
  240:         return;
  241:     }
  242: 
  243:     if (empilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  244:             s_objet) == d_erreur)
  245:     {
  246:         return;
  247:     }
  248: 
  249:     return;
  250: }
  251: 
  252: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>