Exemplo n.º 1
0
	// Uppdaterar year, month och day baserat baserat på jdn
	void Julian::refresh() {
		/*	y 	4716 	v 	3
			j 	1401 	u 	5
			m 	2 	s 	153
			n 	12 	w 	2
			r 	4 	B 	274277
			p 	1461 	C 	-38

			f = J + j
			2. e = r × f + v
			3. g = mod(e, p) div r
			4. h = u × g + w
			5. D = (mod(h, s)) div u + 1
			6. M = mod(h div s + m, n) + 1
			7. Y = (e div p) - y + (n + m - M) div n
		*/
		int f = jdn + 1401;
		unsigned long e = 4 * f + 3;
		int g = (int)floor((e % 1461) / 4.0);
		unsigned long h = 5 * g + 2;
		d_day = (int) floor((h % 153) / 5) + 1;
		d_month = (((int) floor(h / 153) + 2) % 12) + 1;
		d_year = (int)floor(e / 1461) - 4716 + (int)floor((14 - d_month) / 12);

		// Om året vi räknat ut ej är tilllåtet - Kasta exception
		if (!is_valid_date())
			throw std::out_of_range("Error: invalid date");
	}
/**
  Convert a DATE MySQL value to Python datetime.date.

  Convert a DATETIME MySQL value to Python datetime.date. When a date
  can be parsed, but it is invalid, None is returned.

  Raises ValueError when the date is not for format %d-%d-%d.

  @param    data        string to be converted

  @return   datetime.date object.
    @retval PyDate  OK
    @retval None    Invalid date
    @retval NULL    Exception
*/
PyObject*
mytopy_date(const char *data)
{
    int year= 0, month= 0, day= 0;

    PyDateTime_IMPORT;

#pragma warning(push)
// sscanf data comes from MySQL and is fixed
#pragma warning(disable: 4996)
    if (3 == sscanf(data, "%d-%d-%d", &year, &month, &day))
#pragma warning(pop)
    {
        // Invalid dates are returned as None instead of raising ValueError
        if (!is_valid_date(year, month, day))
        {
            Py_RETURN_NONE;
        }
        return PyDate_FromDate(year, month, day);
    }

    PyErr_SetString(PyExc_ValueError,
                    "Received incorrect DATE value from MySQL server");
    return NULL;
}
int main()
{
    special_date[1].year=1582;special_date[1].month=10;special_date[1].day=4;special_date[1].week=4;
    special_date[2].year=1582;special_date[2].month=10;special_date[2].day=15;special_date[2].week=5;
    special_date[3].year=1752;special_date[3].month=9;special_date[3].day=2;special_date[3].week=3;
    special_date[4].year=1752;special_date[4].month=9;special_date[4].day=14;special_date[4].week=4;
    date_start.year=2014;date_start.month=9;date_start.day=9;date_start.week=2;
    while(scanf("%d%d%d",&in_month,&in_day,&in_year)!=EOF&&(in_year+in_month+in_day)>0)
    {
        date date_end;
        date_end.year=in_year;
        date_end.month=in_month;
        date_end.day=in_day;
        if(is_valid_date(date_end))
        {
            get_week(date_start,date_end);
            printf("%s %d, %d is a %s\n",month_map[date_end.month],date_end.day,date_end.year,week_map[date_end.week]);
        }
        else
        {
            printf("%d/%d/%d is an invalid date.\n",date_end.month,date_end.day,date_end.year);
        }
    }
    return(0);
}
Exemplo n.º 4
0
	Julian::Julian(int y, unsigned m, unsigned d) {
		jdn = calculate_jdn(y, m, d);
		d_year = y;
		d_month = m;
		d_day = d;
		if (!is_valid_date()) 
			throw std::invalid_argument("Error: Invalid argument.");
	}
Exemplo n.º 5
0
lab2::Julian::Julian(const unsigned int year,
                     const unsigned int month,
                     const unsigned int day) :
    WesternDate(year, month, day) {
    if(!is_valid_date())
        throw std::invalid_argument("invalid date");
    date_to_jdn(); // sets _offset
}
Exemplo n.º 6
0
void
jumpto_go_window_close_cb (GtkButton *button, gpointer user_data) {

    jday = atoi (gtk_entry_get_text(GTK_ENTRY(day_entry)));
    jmonth = atoi (gtk_entry_get_text(GTK_ENTRY(month_entry)));
    jyear = atoi (gtk_entry_get_text(GTK_ENTRY(year_entry)));

    if (is_valid_date(jday, jmonth-1, jyear) == TRUE)
        jump_to_date (jday, jmonth-1, jyear);

    jumpto_window_close_cb (NULL, NULL);

}
Exemplo n.º 7
0
  int JulianDate::ymd_to_mjd_offset(const int year, const int month, const int day) const {
    if (!is_valid_date(is_leap_year_util(year), month, day)) {
      std::stringstream ss;
      ss << "Invalid Julian date: " << year << "-" << month << "-" << day;
      throw std::out_of_range(ss.str());
    }

    // Source: https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Julian_calendar_date_to_Julian_Day_Number
    const int a = (14 - month) / 12;
    const int y = year + 4800 - a;
    const int m = month + 12 * a - 3;

    return day
      + ((153 * m + 2) / 5)
      + 365L * y + (y / 4)
      - 32083L - 2400001LL;
  }
Exemplo n.º 8
0
int main()
{
    setlocale(LC_ALL, "RUS");

    int i, sum, day, month, year, temperature;
    FILE *file_pointer;
    char *file_name = "temperature";

    do {
        printf("\nВведите дату день/месяц/год (слэш - разделитель): ");
        scanf("%d/%d/%d", &day, &month, &year);
    } while ( !is_valid_date(day, month, year) );

    do {
        printf("\nВведите температуру (в диапазоне [-100; 80]): ");
        scanf("%d", &temperature);
    } while ( temperature < -100 || temperature > 80 );

    printf("\nСохранение данных...");
    file_pointer = fopen(file_name, "a");
    if ( file_pointer == NULL ) {
        perror("Невозможно открыть файл для записи");
        return -1;
    }

    fprintf(file_pointer, "%d %d %d %d\n", day, month, year, temperature);
    fclose(file_pointer);

    printf("\nЗагрузка данных...");
    file_pointer = fopen(file_name, "r");
    if ( file_pointer == NULL ) {
        perror("Невозможно открыть файл для чтения");
        return -1;
    }

    for (i = 0, sum = 0; !feof(file_pointer); i++) {
        fscanf(file_pointer, "%d %d %d %d", &day, &month, &year, &temperature);
        sum += temperature;
    }
    fclose(file_pointer);

    printf("\nСреднее значение температуры: %lf", (double) sum / i);

    return 0;
}
/**
  Convert a DATETIME MySQL value to Python datetime.datetime.

  Convert a DATETIME MySQL value to Python datetime.datetime. The
  fractional part is supported.

  @param    data        string to be converted
  @param    length      length of data

  @return   datetime.datetime object.
    @retval PyDateTime OK
*/
PyObject*
mytopy_datetime(const char *data, const unsigned long length)
{
	int year= 0, month= 0, day= 0;
	int hours= 0, mins= 0, secs= 0, usecs= 0;
    int value= 0;
    int parts[7]= {0};
    int part= 0;
    const char *end= data + length;

    PyDateTime_IMPORT;

    /* Parse year, month, days, hours, minutes and seconds */
    for (;;)
    {
        for (value= 0; data != end && isdigit(*data) ; data++)
        {
            value= (value * 10) + (unsigned int)(*data - '0');
        }
        parts[part++]= (unsigned int)value;
        if (part == 8 || (end-data) < 2
            || (*data != '-' && *data != ':' && *data != ' ')
            || !isdigit(data[1]))
        {
            break;
        }
        data++;  // skip separators '-' and ':'
    }

    if (data != end && end - data >= 2 && *data == '.')
    {
        // Fractional part
        int field_length= 6;   // max fractional - 1
        data++;
        value= (unsigned int)(*data - '0');
        while (data++ != end && isdigit(*data))
        {
            if (field_length-- > 0)
            {
                value= (value * 10) + (unsigned int)(*data - '0');
            }
        }
        parts[6]= value;
    }

    year= parts[0];
    month= parts[1];
    day= parts[2];
    hours= parts[3];
    mins= parts[4];
    secs= parts[5];
    usecs= parts[6];

    if (!is_valid_date(year, month, day))
    {
        Py_RETURN_NONE;
    }

    if (!is_valid_time(hours, mins, secs, usecs))
    {
        Py_RETURN_NONE;
    }

    return PyDateTime_FromDateAndTime(year, month, day,
                                      hours, mins, secs, usecs);
}
Exemplo n.º 10
0
	// Copy constructors
	Julian::Julian(const Julian &other_julian) {
		copy(other_julian);
		if (!is_valid_date())
			throw std::out_of_range("Error: Not a valid date");
	}