Пример #1
0
/*JSON{
  "type" : "staticmethod",
  "class" : "Date",
  "name" : "parse",
  "generate" : "jswrap_date_parse",
  "params" : [
    ["str","JsVar","A String"]
  ],
  "return" : ["float","The number of milliseconds since 1970"]
}
Parse a date string and return milliseconds since 1970. Data can be either '2011-10-20T14:48:00', '2011-10-20' or 'Mon, 25 Dec 1995 13:30:00 +0430' 
*/
JsVarFloat jswrap_date_parse(JsVar *str) {
  if (!jsvIsString(str)) return 0;
  TimeInDay time;
  time.daysSinceEpoch = 0;
  time.hour = 0;
  time.min = 0;
  time.sec = 0;
  time.ms = 0;
  time.zone = 0;
  CalendarDate date = getCalendarDate(0);

  JsLex lex;
  jslInit(&lex, str);

  if (lex.tk == LEX_ID) {
    date.month = getMonth(jslGetTokenValueAsString(&lex));
    date.dow = getDay(jslGetTokenValueAsString(&lex));
    if (date.month>=0) {
      // Aug 9, 1995
      jslGetNextToken(&lex);
      if (lex.tk == LEX_INT) {
        date.day = _parse_int(&lex);
        jslGetNextToken(&lex);
        if (lex.tk==',') {
          jslGetNextToken(&lex);
          if (lex.tk == LEX_INT) {
            date.year = _parse_int(&lex);
            jslGetNextToken(&lex);
            if (lex.tk == LEX_INT) {
              _parse_time(&lex, &time, 0);
            }
          }
        }
      }
    } else if (date.dow>=0) {
      date.month = 0;
      jslGetNextToken(&lex);
      if (lex.tk==',') {
        jslGetNextToken(&lex);
        if (lex.tk == LEX_INT) {
          date.day = _parse_int(&lex);
          jslGetNextToken(&lex);
          if (lex.tk == LEX_ID && getMonth(jslGetTokenValueAsString(&lex))>=0) {
            date.month = getMonth(jslGetTokenValueAsString(&lex));
            jslGetNextToken(&lex);
            if (lex.tk == LEX_INT) {
               date.year = _parse_int(&lex);
               jslGetNextToken(&lex);
               if (lex.tk == LEX_INT) {
                 _parse_time(&lex, &time, 0);
               }
            }
          }
        }
      }
    } else {
      date.dow = 0;
      date.month = 0;
    }
  } else if (lex.tk == LEX_INT) {
    // assume 2011-10-10T14:48:00 format
    date.year = _parse_int(&lex);
    jslGetNextToken(&lex);
    if (lex.tk=='-') {
      jslGetNextToken(&lex);
      if (lex.tk == LEX_INT) {
        date.month = _parse_int(&lex) - 1;
        jslGetNextToken(&lex);
        if (lex.tk=='-') {
          jslGetNextToken(&lex);
          if (lex.tk == LEX_INT) {
            date.day = _parse_int(&lex);
            jslGetNextToken(&lex);
            if (lex.tk == LEX_ID && jslGetTokenValueAsString(&lex)[0]=='T') {
              _parse_time(&lex, &time, 1);
            }
          }
        }
      }
    }
  }

  jslKill(&lex);
  time.daysSinceEpoch = fromCalenderDate(&date);
  return fromTimeInDay(&time);
}
Пример #2
0
static CalendarDate getCalendarDateFromDateVar(JsVar *date) {
  return getCalendarDate(getTimeFromDateVar(date).daysSinceEpoch);
}
Пример #3
0
/*JSON{
  "type" : "method",
  "class" : "Date",
  "name" : "toUTCString",
  "generate" : "jswrap_date_toUTCString",
  "return" : ["JsVar","A String"]
}
Converts to a String, eg: `Fri, 20 Jun 2014 14:52:20 GMT`

**Note:** This always assumes a timezone of GMT
*/
JsVar *jswrap_date_toUTCString(JsVar *parent) {
  TimeInDay time = getTimeFromDateVar(parent);
  CalendarDate date = getCalendarDate(time.daysSinceEpoch);

  return jsvVarPrintf("%s, %d %s %d %02d:%02d:%02d GMT", &DAYNAMES[date.dow*4], date.day, &MONTHNAMES[date.month*4], date.year, time.hour, time.min, time.sec);
}
Пример #4
0
void ioptronHC8406::sendScopeTime()
{
    char cdate[32]={0};
    double ctime;
    int h, m, s;
    int utc_h, utc_m, utc_s;
    double lx200_utc_offset = 0;
    char utc_offset_res[32]={0};
    int day, month, year, result;
    struct tm ltm;
    struct tm utm;
    time_t time_epoch;

    if (isSimulation())
    {
        snprintf(cdate, 32, "%d-%02d-%02dT%02d:%02d:%02d", 1979, 6, 25, 3, 30, 30);
        IDLog("Telescope ISO date and time: %s\n", cdate);
        IUSaveText(&TimeT[0], cdate);
        IUSaveText(&TimeT[1], "3");
        IDSetText(&TimeTP, nullptr);
        return;
    }

    //getCommandSexa(PortFD, &lx200_utc_offset, ":GG#");
    //tcflush(PortFD, TCIOFLUSH);
    getCommandString(PortFD, utc_offset_res, ":GG#");

    f_scansexa(utc_offset_res,&lx200_utc_offset);
    result = sscanf(utc_offset_res, "%d%*c%d%*c%d", &utc_h, &utc_m, &utc_s);
    if (result != 3)
    {
        DEBUG(INDI::Logger::DBG_ERROR, "Error reading UTC offset from Telescope.");
        return;
    }
    DEBUGF(INDI::Logger::DBG_DEBUG, "<VAL> UTC offset: %d:%d:%d --->%g",utc_h,utc_m, utc_s, lx200_utc_offset);
    // LX200 TimeT Offset is defined at the number of hours added to LOCAL TIME to get TimeT. This is contrary to the normal definition.
    DEBUGF(INDI::Logger::DBG_DEBUG, "<VAL> UTC offset str: %s",utc_offset_res);
    IUSaveText(&TimeT[1], utc_offset_res);
    //IUSaveText(&TimeT[1], lx200_utc_offset);

    getLocalTime24(PortFD, &ctime);
    getSexComponents(ctime, &h, &m, &s);

    getCalendarDate(PortFD, cdate);
    result = sscanf(cdate, "%d%*c%d%*c%d", &year, &month, &day);
    if (result != 3)
    {
        DEBUG(INDI::Logger::DBG_ERROR, "Error reading date from Telescope.");
        return;
    }

    // Let's fill in the local time
    ltm.tm_sec  = s;
    ltm.tm_min  = m;
    ltm.tm_hour = h;
    ltm.tm_mday = day;
    ltm.tm_mon  = month - 1;
    ltm.tm_year = year - 1900;

    // Get time epoch
    time_epoch = mktime(&ltm);

    // Convert to TimeT
    //time_epoch -= (int)(atof(TimeT[1].text) * 3600.0);
    time_epoch -= (int)(lx200_utc_offset * 3600.0);

    // Get UTC (we're using localtime_r, but since we shifted time_epoch above by UTCOffset, we should be getting the real UTC time
    localtime_r(&time_epoch, &utm);

    /* Format it into ISO 8601 */
    strftime(cdate, 32, "%Y-%m-%dT%H:%M:%S", &utm);
    IUSaveText(&TimeT[0], cdate);

    DEBUGF(INDI::Logger::DBG_DEBUG, "Mount controller Local Time: %02d:%02d:%02d", h, m, s);
    DEBUGF(INDI::Logger::DBG_DEBUG, "Mount controller UTC Time: %s", TimeT[0].text);

    // Let's send everything to the client
    IDSetText(&TimeTP, nullptr);
}