int getWeekNumber(int y, int m, int d) { int julian = getDayOfYear(y,m,d); // since this is only for display purposes, we can mix settings here: if(getSetting(SETTING_WEEK_START_DAY)==1) { if(julian>1) julian--; else { y--; julian = getDayOfYear(y,m,d); } } int dowk = dow(y,m,d); int dowkJan1 = dow(y,1,1); int weekNum = ((julian + 6) / 7); if (dowk < dowkJan1) weekNum++; return weekNum; }
static int _parse_time(char **argv, struct tm *time) { short i; char *end; i = strtol(argv[0], &end, 10); time->tm_year = i - 1900; i = strtol(end + 1, &end, 10); time->tm_mon = i - 1; i = strtol(end + 1, &end, 10); time->tm_mday = i; i = strtol(argv[1], &end, 10); time->tm_hour = i; i = strtol(end + 1, &end, 10); time->tm_min = i; i = strtol(end + 1, &end, 10); time->tm_sec = i; time->tm_wday = dow(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday); time->tm_isdst = -1; /* undefined */ return 0; }
function plotDay(var value,int type) { int periods = 2*24; LookBack = max(LookBack,periods); int h = 2*hour() + minute()/30; if(h > periods) return; plotSeason(h,hour(),dow(),value,type); }
function plotWeek(var value,int type) { int periods = (4*24 + 22); LookBack = max(LookBack,periods); int h = hour() + 24*(dow()-1); if(h > periods) return; plotSeason(h,hour(),week(),value,type); }
function plotYear(var value,int type) { int periods = 52*5; LookBack = max(LookBack,periods); int n = (week()-1)*5 + dow()-1; if(n > periods) return; plotSeason(n,month(),year(),value,type); }
void printDate(const Date& date){ //output the date in a readable format std::cout << std::string(20, '*') << std::endl; std::cout << "\nmonth : " << date.month << std::endl; std::cout << "day : " << date.day << std::endl; std::cout << "year : " << date.year << std::endl; std::cout << "day of week : " << getDayOfWeek(dow(date)) << std::endl; std::cout << std::string(20, '*') << std::endl; }
Date getNextMonday(Date date){ // keep adding a day // until the day equals monday // then return that day while (dow(date) != 1){ date.day++; } return date; }
static void cm_update_segs(Week *w, Tick tick, Tick dur, int *start_index, int *end_index, Boolean addto) { int num_segs, i, start, start_hour, duration, nday; start_hour = hour(tick); if (start_hour >= w->end_hour) { *start_index = -1; *end_index = -1; return; } if (start_hour < w->begin_hour) { start = 0; duration = dur - ((w->begin_hour - (start_hour + (double)minute(tick)/(double)60)) * hrsec); } else { start = ((start_hour - w->begin_hour) * 60 + minute(tick)); duration = dur; } if (duration <= 0) { *start_index = -1; *end_index = -1; return; } nday = (nday=dow(tick))==0? 6: nday-1; num_segs = (double)start / (double)MINS_IN_SEG; *start_index = (double)start / (double)MINS_IN_SEG + (nday * (w->segs_in_array/7)); if (start - (num_segs * MINS_IN_SEG) > 7) (*start_index)++; num_segs = ((double)duration / (double)60 / (double)MINS_IN_SEG); *end_index = num_segs + *start_index; if (((double)duration/(double)60-MINS_IN_SEG*num_segs) > 7) (*end_index)++; if (*end_index > (i = ((nday + 1) * (w->segs_in_array / 7))) ) *end_index = i; for (i = *start_index; i < *end_index; i++) if (addto) w->time_array[i]++; else w->time_array[i]--; }
function tradeOneNightStand() { vars Price = series(price()); vars SMA10 = series(SMA(Price, 10)); vars SMA40 = series(SMA(Price, 40)); //Stop = 3 * 90 * PIP; var BuyStop,SellStop; BuyStop = HH(10) + 1*PIP; SellStop = LL(10) - 1*PIP; if (dow() == 5 && NumOpenLong == 0 && NumPendingLong == 0 && SMA10[0] > SMA40[0]) enterLong(0,BuyStop); else if (dow() == 5 && NumOpenShort == 0 && NumPendingShort == 0 && SMA10[0] < SMA40[0]) enterShort(0,SellStop); if (dow() != 5 && dow() != 6 && dow() != 7) { exitLong(); exitShort(); } }
void DS3231::setDateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) { Wire.beginTransmission(DS3231_ADDRESS); #if ARDUINO >= 100 Wire.write(DS3231_REG_TIME); #else Wire.send(DS3231_REG_TIME); #endif #if ARDUINO >= 100 Wire.write(dec2bcd(second)); Wire.write(dec2bcd(minute)); Wire.write(dec2bcd(hour)); Wire.write(dec2bcd(dow(year, month, day))); Wire.write(dec2bcd(day)); Wire.write(dec2bcd(month)); Wire.write(dec2bcd(year-2000)); #else Wire.send(dec2bcd(second)); Wire.send(dec2bcd(minute)); Wire.send(dec2bcd(hour)); Wire.send(dec2bcd(dow(year, month, day))); Wire.send(dec2bcd(day)); Wire.send(dec2bcd(month)); Wire.send(dec2bcd(year-2000)); #endif #if ARDUINO >= 100 Wire.write(DS3231_REG_TIME); #else Wire.send(DS3231_REG_TIME); #endif Wire.endTransmission(); }
void setDate(int year, int month, int day) { char temp = *RCR2; //for rtc stopping/starting //stop RTC temp |= 0b10; temp &= 0b11111110; *RCR2 = temp; // set year *RYRCNT = ((year / 1000) << 12) | (((year % 1000)/100) << 8) | (((year % 100) / 10) << 4) | (year % 10); // set month *RMONCNT = ((month / 10) << 4) | (month % 10); // set day *RDAYCNT = ((day / 10) << 4) | (day % 10); // set day of week *RWKCNT = dow(year, month, day) & 0b111; // start RTC *RCR2 |= 1; }
void compute_times() { struct sched_entry *s; struct time_entry *t; time_count = 0; int i; time_t now = time(); if (now < SANE_TIME) return; for (i = config.sched_count, s = config.scheds, t = times; i > 0; i--, s++) { time_t ontime = s->start; if (ontime == 0) ontime = now; ontime = midnight(ontime) + s->time; int failsafe = 0; while(ontime <= now || (dow(ontime) & s->dow) == 0 ) { ontime += 86400 * s->repeat; failsafe++; if (failsafe > 100) break; } if (failsafe < 101) if (ontime < s->end || s->end == 0) { t->ontime = ontime; t->duration = s->duration; t->zone = s->zone; os_printf("computed zone %d time %d (%d from now) duration %d\n", t->zone, t->ontime, (t->ontime - time()), t->duration); t++; time_count++; } } }
/* Converts a Gregorian date into a * Julian Day Number + Time of Day * */ double greg_to_jul(int year, int month, int day, int hour, int minute, int second) { int a, y, m, jdn; double jd; a = (14 - month)/12; y = year + 4800 - a; m = month + (12*a) - 3; day = dow(year, month, day); jdn = day + (((153*m)+2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045; //printf("%d\n", jdn); jd = (double)jdn + ((double)hour/24.0) + ((double)minute/1440.0) + ((double)second/86400.0); return jd; }
openstudio::Date YearDescription_Impl::makeDate(openstudio::MonthOfYear monthOfYear, unsigned dayOfMonth) { boost::optional<int> calendarYear = this->calendarYear(); if (calendarYear){ return openstudio::Date(monthOfYear, dayOfMonth, *calendarYear); } openstudio::YearDescription yd; yd.isLeapYear = this->isLeapYear(); std::string dayofWeekforStartDay = this->dayofWeekforStartDay(); if (!dayofWeekforStartDay.empty()){ if (istringEqual(dayofWeekforStartDay, "UseWeatherFile")){ LOG(Info, "'UseWeatherFile' is not yet a supported option for YearDescription"); }else{ openstudio::DayOfWeek dow(dayofWeekforStartDay); yd.yearStartsOnDayOfWeek = dow; } } return openstudio::Date(monthOfYear, dayOfMonth, yd); }
int YearDescription_Impl::assumedYear() const { boost::optional<int> calendarYear = this->calendarYear(); if (calendarYear){ return *calendarYear; } openstudio::YearDescription yd; yd.isLeapYear = this->isLeapYear(); std::string dayofWeekforStartDay = this->dayofWeekforStartDay(); if (!dayofWeekforStartDay.empty()){ try{ openstudio::DayOfWeek dow(dayofWeekforStartDay); yd.yearStartsOnDayOfWeek = dow; }catch(const std::exception& ){ LOG(Error, "'" << dayofWeekforStartDay << "' is not yet a supported option for YearDescription"); } } return yd.assumedYear(); }
extern void week_event(XEvent *event) { Calendar *c = calendar; Props *p = (Props*)c->properties; static int lastdate; static XEvent lastevent; int x, y, i, j, hr, id; Week *w = (Week *)c->view->week_info; Selection *wsel; Editor *e = (Editor *)c->editor; ToDo *t = (ToDo*)c->todo; GEditor *ge = (GEditor*)c->geditor; static int lastrow, lastcol; int row, col; x = event->xbutton.x; y = event->xbutton.y; wsel = (Selection *)w->current_selection; switch(event->type) { case MotionNotify: j = week_xytoclock(w, x, y); (col = dow(j)) == 0 ? col = 6 : col--; if (inchart(w, x, y)) row = (double)(y - w->chart_y) / (double)(w->chart_hour_height+ w->add_pixels); else row = wsel->row; if (j != lastdate || lastcol != col || lastrow != row) { calendar_deselect(c); wsel->row = row; wsel->col = col; if (j > 0) { c->view->olddate = c->view->date; c->view->date = j; calendar_select(c, weekdaySelect, NULL); } } lastcol = wsel->col; lastrow = wsel->row; lastdate = c->view->date; break; case ButtonPress: j = week_xytoclock(w, x, y); if (j == -1) return; hr = (inchart(w, x, y)) ? week_xytohour(w, x, y) : (wsel->row + w->begin_hour); if (ds_is_double_click(&lastevent, event)) { _DtTurnOnHourGlass(c->frame); if (j == lastdate) { show_editor(c, next_nhours(j, hr), next_nhours(j, hr + 1), False); } else if (editor_showing(e)) { set_editor_defaults(e, next_nhours(j, hr), next_nhours(j, hr + 1), False); add_all_appt(e); } _DtTurnOffHourGlass(c->frame); } else { calendar_deselect(c); (wsel->col = dow(j)) == 0 ? wsel->col = 6 : wsel->col--; if (inchart(w, x, y)) wsel->row = (double)(y - w->chart_y) / (double)(w->chart_hour_height + w->add_pixels); if (j > 0) { c->view->olddate = c->view->date; c->view->date = j; calendar_select(c, weekdaySelect, NULL); } if (editor_showing(e)) { set_editor_defaults(e, next_nhours(j, hr), next_nhours(j, hr + 1), False); add_all_appt(e); } if (todo_showing(t)) { set_todo_defaults(t); add_all_todo(t); } if (geditor_showing(ge)) { set_geditor_defaults(ge, 0, 0); add_all_gappt(ge); } } lastdate = c->view->date; lastcol = wsel->col; lastrow = wsel->row; break; default: break; }; /* switch */ lastevent = *event; }
void init_timeline(struct timeline_tree * t_line, struct cron_spec * n) { unsigned int cur[6]; memset(&cur, 0, sizeof(cur)); int year = t_line->year; memset(t_line, 0, sizeof(*t_line)); t_line->year = year; unsigned short * days_in_month; unsigned char active_mon[12]; if (t_line->year == 0) { if (n->year > 0) t_line->year = n->year - 1900; else { struct timeval now; gettimeofday(&now, 0); struct tm * g = gmtime(&now.tv_sec); t_line->year = g->tm_year; } } if(leap_year(t_line->year + 1900)) days_in_month = leap_mon_end_days; else days_in_month = mon_end_days; memset(active_mon, 0, sizeof(active_mon)); if (n->mon_flag) { for(int i = 0; i < 12; i++) { if (n->mon[i]) active_mon[i] = 1; } } else { for(int i = 0; i < 12; i++) active_mon[i] = 1; } if (n->mday_flag) { int mon = 0; int md = 1; for(int i = 0; i < 366; i++,md++) { if (md > days_in_month[mon]) { mon++; md = 1; } if (mon < 12) { if (n->mday[md]) { if (active_mon[mon]) { t_line->yday[i] = 1; t_line->mday[i] = md; t_line->mon[i] = mon; } } } } } if (n->wday_flag) { int mon = 0; int w_d = dow(t_line->year + 1900, 1, 1); int md = 1; for(int i = 0; i < 366; i++, w_d++, md++) { if (w_d > 6) w_d = 0; if (md > days_in_month[mon]) { mon++; md = 1; } if (mon < 12) { if (n->wday[w_d]) { if (active_mon[mon]) { t_line->yday[i] = 1; t_line->mday[i] = md; t_line->mon[i] = mon; } } } } } if ((n->wday_flag == 0) && (n->mday_flag == 0)) { int mon = 0; int md = 1; for(int i = 0; i < 366; i++) { if (active_mon[mon]) { t_line->yday[i] = 1; } md++; if (md > days_in_month[mon]) { mon++; md = 1; } } } if (n->hour_flag) { for(int i = 0; i < 24; i++) { if (n->hour[i]) t_line->hour[i] = 1; } } else { for(int i = 0; i < 24; i++) t_line->hour[i] = 1; } if (n->min_flag) { for(int i = i; i < 60; i++) { if (n->min[i]) t_line->min[i] = 1; } } else { for(int i = 0; i < 60; i++) t_line->min[i] = 1; } if (n->sec_flag) { for(int i = 0; i < 60; i++) { if (n->sec[i]) t_line->sec[i] = 1; } } else { for(int i = 0; i < 60; i++) t_line->sec[i] = 1; } }
TEST(dateTest, Date){ //validation based on http://www.timeanddate.com/date/weekday.html Date birthday; birthday.day = 7; birthday.month = 11; birthday.year = 1984; Date dday; dday.day = 6; dday.month = 6; dday.year = 1944; Date apolloEleven; apolloEleven.day = 20; apolloEleven.month = 7; apolloEleven.year = 1969; // begin addDay check addDay(birthday); CHECK(birthday.day == 8 && birthday.month == 11 && birthday.year == 1984); CHECK(getDayOfWeek(dow(birthday)) == "Thursday"); addDay(dday); CHECK(dday.day == 7 && dday.month == 6 && dday.year == 1944); CHECK(getDayOfWeek(dow(dday)) == "Wednesday"); addDay(apolloEleven); CHECK(apolloEleven.day == 21 && apolloEleven.month == 7 && apolloEleven.year == 1969); CHECK(getDayOfWeek(dow(apolloEleven)) == "Monday"); // end addDay check // begin addMonth check addMonth(birthday); CHECK(birthday.day == 8 && birthday.month == 12 && birthday.year == 1984); CHECK(getDayOfWeek(dow(birthday)) == "Saturday"); addMonth(dday); CHECK(dday.day == 7 && dday.month == 7 && dday.year == 1944); CHECK(getDayOfWeek(dow(dday)) == "Friday"); addMonth(apolloEleven); CHECK(apolloEleven.day == 20 && apolloEleven.month == 8 && apolloEleven.year == 1969); CHECK(getDayOfWeek(dow(apolloEleven)) == "Wednesday"); // end addMonth check // begin addYear check addYear(birthday); CHECK(birthday.day == 8 && birthday.month == 12 && birthday.year == 1985); CHECK(getDayOfWeek(dow(birthday)) == "Sunday"); addYear(dday); CHECK(dday.day == 7 && dday.month == 7 && dday.year == 1945); CHECK(getDayOfWeek(dow(dday)) == "Saturday"); addYear(apolloEleven); CHECK(apolloEleven.day == 20 && apolloEleven.month == 8 && apolloEleven.year == 1970); CHECK(getDayOfWeek(dow(apolloEleven)) == "Thursday"); // end addYear check // begin getNextMonday check Date nmBirthday = getNextMonday(birthday); CHECK(nmBirthday.day == 9 && nmBirthday.month == 12 && nmBirthday.year == 1985); CHECK(getDayOfWeek(dow(nmBirthday)) == "Monday"); Date nmDDay = getNextMonday(dday); CHECK(nmDDay.day == 9 && nmDDay.month == 7 && nmDDay.year == 1945); CHECK(getDayOfWeek(dow(nmDDay)) == "Monday"); Date nmApolloEleven = getNextMonday(apolloEleven); CHECK(nmApolloEleven.day == 24 && nmApolloEleven.month == 8 && nmApolloEleven.year == 1970); CHECK(getDayOfWeek(dow(nmApolloEleven)) == "Monday"); // end getNextMonday check }
/* * Set up data needed to draw this particular week */ static void init_week(Calendar *c, Boundary *boundary) { Week *w = (Week *)c->view->week_info; int char_width, char_height; Props *p; int num_hrs, day_of_week; int empty_space, day_box; int skip_days = 0; XFontSetExtents regfontextents, boldfontextents; *boundary = okay; /* * The week view starts on Monday. Map Sunday to the last day of the * week */ if ((day_of_week = dow(c->view->date)) == 0) day_of_week = 6; else day_of_week--; ((Selection*)w->current_selection)->col = day_of_week; w->start_date = lowerbound(c->view->date - (day_of_week * daysec)); /* make sure date is within bounds */ if (w->start_date == -1) { if (year(c->view->date) == year(get_bot())) { w->start_date = get_bot(); *boundary = lower; } } else if (year(next_ndays(w->start_date, 7)) > year(get_eot())) { *boundary = upper; } /* * Set up a bunch of variables which are needed to draw and fill * the week at a glance screen */ XtVaGetValues(c->canvas, XmNwidth, &w->canvas_w, XmNheight, &w->canvas_h, NULL); w->font = c->fonts->labelfont; w->small_font = c->fonts->viewfont; w->small_bold_font = c->fonts->boldfont; CalFontExtents(w->font, ®fontextents); CalFontExtents(w->small_bold_font, &boldfontextents); w->x = c->view->outside_margin; w->y = 2 * (int) boldfontextents.max_logical_extent.height; char_height = regfontextents.max_logical_extent.height; char_width = regfontextents.max_logical_extent.width; w->label_height = char_height * 2; w->day_width = ((int) (w->canvas_w - 2 * w->x)) / 5; /* height of box with label */ w->day_height = ((int) (w->canvas_h - 2 * w->y)) / 2; /* * We compute week dimensions from day dimensions to remove rounding * errors */ w->width = w->day_width * 5; /* height from top of box to bottom of weekend boxes */ w->height = w->day_height * 2; p = (Props *)c->properties; w->begin_hour = get_int_prop(p, CP_DAYBEGIN); w->end_hour = get_int_prop(p, CP_DAYEND); /* width of a column in chart */ w->chart_day_width = (3 * w->day_width - 3 * char_width) / 7; /* width of chart */ w->chart_width = w->chart_day_width * 7; num_hrs = w->end_hour - w->begin_hour; /* height of box without label */ day_box = w->day_height - w->label_height; /* height of an hour in chart */ w->chart_hour_height = day_box / num_hrs; /* chart_hour_height must be evenly divisble by BOX_SEG */ w->chart_hour_height -= (w->chart_hour_height % BOX_SEG); w->chart_height = w->chart_hour_height * num_hrs; /* x point of upper left corner of chart */ w->chart_x = w->x + 2 * boldfontextents.max_logical_extent.width; /* y point of upper left corner of chart */ w->chart_y = w->y + w->height - w->chart_height; /* left over empty space above chart after round off error */ empty_space = day_box - w->chart_height; /* add pixels to the height of each hour box in chart to fill gap*/ if (w->add_pixels = ((double)empty_space / (double)num_hrs)) { w->chart_y -= w->add_pixels * num_hrs; w->chart_height += w->add_pixels * num_hrs; } w->segs_in_array = BOX_SEG * num_hrs * 7; if (w->time_array != NULL) free(w->time_array); w->time_array = (char*)ckalloc(w->segs_in_array); c->view->outside_margin = w->x; c->view->topoffset = w->y; }
static Boolean print_week (Calendar *c, int num_page, void *xp, Tick first_date, Props *p, Boolean first) { Boolean more, done = False, all_done = True; int num_appts, day_of_week; char buf[128]; int i, j; OrderingType ot = get_int_prop(p, CP_DATEORDERING); time_t start, stop; CSA_return_code stat; CSA_entry_handle *list; CSA_attribute *range_attrs; CSA_enum *ops; CSA_uint32 a_total; int lines_per_page; static Tick start_date = 0; static int total_pages; static char *days[] = { (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL }; if (days[0] == (char *)NULL) { days[0] = XtNewString(catgets(c->DT_catd, 1, 596, "Monday %d")); days[1] = XtNewString(catgets(c->DT_catd, 1, 597, "Tuesday %d")); days[2] = XtNewString(catgets(c->DT_catd, 1, 598, "Wednesday %d")); days[3] = XtNewString(catgets(c->DT_catd, 1, 599, "Thursday %d")); days[4] = XtNewString(catgets(c->DT_catd, 1, 600, "Friday %d")); days[5] = XtNewString(catgets(c->DT_catd, 1, 601, "Saturday %d")); days[6] = XtNewString(catgets(c->DT_catd, 1, 602, "Sunday %d")); } x_init_printer(xp, LANDSCAPE); x_init_week(xp); lines_per_page = x_get_week_lines_per_page(xp); if (first) start_date = first_date; if (num_page > 1) { start_date = prevweek(start_date); if (!timeok(start_date)) start_date = get_bot(); } else total_pages = (lines_per_page > 0) ? count_week_pages(c, lines_per_page, start_date) : 1; format_week_header(start_date, ot, buf); x_print_header(xp, buf, num_page, total_pages); x_week_appt_boxes(xp); x_week_sched_boxes(xp); /* print the times and text of appts */ for (i = (dow(start_date) + 6) % 7; i < 7; i++) { /* print <Weekday DD> centered at top of appt box */ x_week_sched_init(xp); sprintf(buf, days[i], dom(start_date)); /* setup a time limit for appts searched */ start = (time_t) lowerbound (start_date); stop = (time_t) next_ndays(start_date, 1) - 1; setup_range(&range_attrs, &ops, &j, start, stop, CSA_TYPE_EVENT, 0, B_FALSE, c->general->version); csa_list_entries(c->cal_handle, j, range_attrs, ops, &a_total, &list, NULL); free_range(&range_attrs, &ops, j); num_appts = count_multi_appts(list, a_total, c); if ((lines_per_page > 0) && (num_appts > (lines_per_page * num_page))) more = True; else more = False; x_week_daynames(xp, buf, i, more); /* print out times and appts */ if (lines_per_page > 0) done = x_print_multi_appts(xp, list, a_total, num_page, weekGlance); else done = True; if (!done) all_done = False; x_week_sched_draw(xp, i); start_date = nextday(start_date); csa_free(list); } x_finish_printer(xp); return(all_done); }