コード例 #1
0
ファイル: gps.c プロジェクト: dawsonjon/speedometer
int is_bst(){
  int i, last_sunday_in_october, last_sunday_in_march;

  /*Find last Sunday of March*/
  for(i=31; i>=1; i--){
    if(day_of_week(year + 2000, 3, i) == 0) break;
  }
  last_sunday_in_march = i;

  /*Find last Sunday of October*/
  for(i=31; i>=1; i--){
    if(day_of_week(year + 2000, 10, i) == 0) break;
  }
  last_sunday_in_october = i;

  /*If before 1:00 GMT on last Sunday in March return false*/
  if(month < 3) return 0;
  if(month == 3) {
    if(utc_day < last_sunday_in_march) return 0;
    if(utc_day == last_sunday_in_march && utc_hour < 1) return 0;
  }

  /*If after 1:00 GMT on last Sunday in October return false*/
  if(month > 10) return 0;
  if(month == 10) {
    if(utc_day > last_sunday_in_october) return 0;
    if(utc_day == last_sunday_in_october && utc_hour >= 1) return 0;
  }
  return 1;

}
コード例 #2
0
ファイル: time64.c プロジェクト: code0100fun/rubinius
time64_t timestamp64(time_t (*func)(struct tm*), struct tm64* tm64) {
  time64_t time;
  struct tm tm;
  memset(&tm, 0, sizeof(tm));

  /* If this succeeds it fits in a standard struct tm */
  if(tm64_to_tm(tm64, &tm) == 0) {
    time = timestamp64_mktime(func, &tm);
    /*
     * This still would fail for 1969-12-31 23:59:59 GMT, but
     * the fallback code will properly handle that case anyway.
     */
    if(time != -1) {
      /* Copy back updated information */
      tm_to_tm64(&tm, tm64);
      return time;
    }
  }

  /*
   * Convert the struct to a year that is day compatible with
   * the current day.
   */

  int64_t year = tm64->tm_year;

  if(year < 1902) {
    /* For years below the 32 bit size time_t value we need to use
    * the lower comparable years */
    int day = day_of_week(year, tm64->tm_mon + 1, tm64->tm_mday);
    if(tm64->tm_mon == 2 && leap_year(year)) {
      tm.tm_year = lower_leap_month_table[day] - 1900;
    } else {
      tm.tm_year = lower_common_month_table[tm.tm_mon][day] - 1900;
    }
  } else if(year > 2037) {
    /* For years above the 32 bit size time_t value we need to use
     * the lower comparable years */
    int day = day_of_week(year, tm64->tm_mon + 1, tm64->tm_mday);
    if(tm64->tm_mon == 2 && leap_year(year)) {
      tm.tm_year = higher_leap_month_table[day] - 1900;
    } else {
      tm.tm_year = higher_common_month_table[tm.tm_mon][day] - 1900;
    }
  }

  time = timestamp64_mktime(func, &tm);
  tm_to_tm64(&tm, tm64);

  if(year != tm64->tm_year) {
    /* Correct for the changed year to do the mktime computation */
    time += year_diff_to_seconds(tm64->tm_year, year, day_before_leap(tm64));
  }

  tm64->tm_year = year;
  return time;
}
コード例 #3
0
ファイル: builtins-date.c プロジェクト: wuthering-bytes/wake
// isoweek_from_date -
//	Computes the ISO week number from the specified date.
static void  isoweek_from_date ( int  y, int  m, int  d, int *  iso_week, int *  iso_year )
   {
	int		y_leap			=  IS_LEAP ( y ), 
			y_previous_leap		=  IS_LEAP ( y - 1 ) ;
	int		doy			=  day_of_year ( y, m, d ) + 1 ;
	int		weekday, jan1_weekday ;

	//if  ( y_leap  &&  m  >  2 )
	//	doy ++ ;

	jan1_weekday	=  day_of_week ( y, 1, 1, 1 ) ;
	weekday		=  day_of_week ( y, m, d, 1 ) ;

	if  ( ! weekday )
		weekday		=  7 ;

	if  ( ! jan1_weekday )
		jan1_weekday	=  7 ;

	/* Find if Y M D falls in year Y-1, week# 52 or 53 */
	if  ( doy  <=  ( 8 - jan1_weekday )  &&  jan1_weekday  >  4 )
	   {
		* iso_year	=  y - 1 ;

		if  ( jan1_weekday  ==  5  ||  ( y_previous_leap  &&  jan1_weekday  ==  6 ) )
			* iso_week	=  53 ;
		else
			* iso_week	=  52 ;
	    }
	else
		* iso_year	=  y ;

	/* Find if Y M D falls in year Y+1, week# 1 */
	if  ( * iso_year  ==  y )
	   {
		int		i	=  ( y_leap ) ?  366 : 365 ;

		if  ( ( i - ( doy - y_leap ) )  <  4 - weekday )
		   {
			( * iso_year ) ++ ;
			* iso_week	=  1 ;

			return ;
		    }
	    }

	/* Find if Y M D falls in year Y, week# 1 through 53 */
	if  ( * iso_year  ==  y )
	   {
		int	j =  doy + ( 7 - weekday ) + ( jan1_weekday - 1 ) ;

		* iso_week	=  j / 7 ;

		if  ( jan1_weekday )
			( * iso_week ) -- ;
	    }
    }
コード例 #4
0
ファイル: datetime.c プロジェクト: 8bitgeek/bacnet-stack
void testBACnetDayOfWeek(
    Test * pTest)
{
    uint8_t dow = 0;

    /* 1/1/1900 is a Monday */
    dow = day_of_week(1900, 1, 1);
    ct_test(pTest, dow == 1);

    /* 1/1/2007 is a Monday */
    dow = day_of_week(2007, 1, 1);
    ct_test(pTest, dow == 1);
    dow = day_of_week(2007, 1, 2);
    ct_test(pTest, dow == 2);
    dow = day_of_week(2007, 1, 3);
    ct_test(pTest, dow == 3);
    dow = day_of_week(2007, 1, 4);
    ct_test(pTest, dow == 4);
    dow = day_of_week(2007, 1, 5);
    ct_test(pTest, dow == 5);
    dow = day_of_week(2007, 1, 6);
    ct_test(pTest, dow == 6);
    dow = day_of_week(2007, 1, 7);
    ct_test(pTest, dow == 7);

    dow = day_of_week(2007, 1, 31);
    ct_test(pTest, dow == 3);
}
コード例 #5
0
ファイル: date.c プロジェクト: BigEd/wp34s
/* Convert a (y, m, d) triple into a decimal number that represents the date.
 * Some care is required for overflow and the like.
 */
static decNumber *build_date(decNumber *res, int year, int month, int day) {
	int sign = 1, shift = -6, r = 0;

	if (check_date(year, month, day)) {
		set_NaN(res);
		return res;
	}
	if (year < 0) {
		year = -year;
		sign = -1;
	}

	switch (UState.date_mode) {
	case DATE_YMD:
		r = ((year * 100) + month) * 100 + day;
		shift = -4;
		break;

	case DATE_DMY:
		r = ((day * 100) + month)* 10000 + year;
		break;

	case DATE_MDY:
		r = ((month * 100) + day)* 10000 + year;
		break;
	}
	int_to_dn(res, r * sign);
	dn_mulpow10(res, res, shift);
	day_of_week(sign * year, month, day, &DispMsg);
	return res;
}
コード例 #6
0
int m48_tod_set(int year,		/* 1980-2079 */
		int month,		/* 01-12 */
		int day,		/* 01-31 */
		int hour,		/* 00-23 */
		int minute,		/* 00-59 */
		int second)		/* 00-59 */

{
    SYS_TOD_UNPROTECT();

    M48_ADDR[CONTROL] |= 0x80;	/* Set WRITE bit */

    M48_ADDR[YEAR] = to_bcd(year % 100);
    M48_ADDR[MONTH] = to_bcd(month);
    M48_ADDR[DAY] = to_bcd(day);
    M48_ADDR[DAY_OF_WEEK] = day_of_week(year, month, day) + 1;
    M48_ADDR[HOUR] = to_bcd(hour);
    M48_ADDR[MINUTE] = to_bcd(minute);
    M48_ADDR[SECOND] = to_bcd(second);

    M48_ADDR[CONTROL] &= ~0x80;	/* Clear WRITE bit */

    SYS_TOD_PROTECT();

    return 0;
}
コード例 #7
0
ファイル: time.c プロジェクト: AhmadQasim/GPU-Virtualization
/**
 * Calculate seconds since the Epoch
 *
 * @v tm		Broken-down time
 * @ret time		Seconds since the Epoch
 */
time_t mktime ( struct tm *tm ) {
	int days_since_epoch;
	int seconds_since_day;
	time_t seconds;

	/* Calculate day of year */
	tm->tm_yday = ( ( tm->tm_mday - 1 ) +
			days_to_month_start[ tm->tm_mon ] );
	if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
		tm->tm_yday++;

	/* Calculate day of week */
	tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );

	/* Calculate seconds since the Epoch */
	days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
			     leap_years_to_end ( tm->tm_year - 1 ) );
	seconds_since_day =
		( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
	seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
		    seconds_since_day );

	DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
	       "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
	       tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
	       weekdays[ tm->tm_wday ], tm->tm_yday );

	return seconds;
}
コード例 #8
0
 Date next_Sunday(const Date& d)
 {
     Date nsd = d;
     Day day = day_of_week(d);
     nsd.add_day(7-day);
     return nsd;
 }
コード例 #9
0
ファイル: 요일구하기.cpp プロジェクト: imgosari/acm
int main()
{
	char *day[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT" };
	scanf("%d %d",&m,&d);
	printf("%s\n", day[day_of_week(2007,m,d)]);
	return 0;
}
コード例 #10
0
ファイル: datetime.c プロジェクト: 8bitgeek/bacnet-stack
/** Utility to add or subtract minutes to a BACnet DateTime structure
 *
 * @param bdatetime [in] the starting date and time
 * @param minutes [in] number of minutes to add or subtract from the time
 */
void datetime_add_minutes(
    BACNET_DATE_TIME * bdatetime,
    int32_t minutes)
{
    uint32_t bdatetime_minutes = 0;
    uint32_t bdatetime_days = 0;
    int32_t days = 0;

    /* convert bdatetime to seconds and days */
    bdatetime_minutes =
        seconds_since_midnight(bdatetime->time.hour, bdatetime->time.min,
        bdatetime->time.sec) / 60;
    bdatetime_days =
        days_since_epoch(bdatetime->date.year, bdatetime->date.month,
        bdatetime->date.day);

    /* add */
    days = minutes / (24 * 60);
    bdatetime_days += days;
    minutes -= (days * 24 * 60);
    bdatetime_minutes += minutes;
    days = bdatetime_minutes / (24 * 60);
    bdatetime_days += days;

    /* convert bdatetime from seconds and days */
    seconds_since_midnight_into_hms(bdatetime_minutes * 60,
        &bdatetime->time.hour, &bdatetime->time.min, NULL);
    days_since_epoch_into_ymd(bdatetime_days, &bdatetime->date.year,
        &bdatetime->date.month, &bdatetime->date.day);
    bdatetime->date.wday =
        day_of_week(bdatetime->date.year, bdatetime->date.month,
        bdatetime->date.day);
}
コード例 #11
0
ファイル: date.c プロジェクト: BigEd/wp34s
decNumber *dateDayOfWeek(decNumber *res, const decNumber *x) {
	int y, m, d;

	if (dateExtract(res, x, &y, &m, &d))
		int_to_dn(res, day_of_week(y, m, d, &DispMsg));
	return res;
}
コード例 #12
0
ファイル: ktime.c プロジェクト: stuartatpeasy/m68k-system
/*
    rtc_time_from_str() - take a string, in the format "YYYYMMDDHHMMSS", and populate
    tm with the values.
*/
s32 rtc_time_from_str(const char * const str, rtc_time_t * const tm)
{
    s32 i;
    s8 s[14];

    // Set the date and time.  Acceptable format:
    //      date YYYYMMDDHHMMSS
    if(strlen(str) != 14)
        return EINVAL;

    for(i = 0; i < 14; i++)
    {
        if((str[i] >= '0') && (str[i] <= '9'))
            s[i] = str[i] - '0';
        else
            return EINVAL;
    }

    tm->year    = (s[0] * 1000) + (s[1] * 100) + (s[2] * 10) + s[3];
    tm->month   = (s[4] * 10) + s[5];
    tm->day     = (s[6] * 10) + s[7];
    tm->hour    = (s[8] * 10) + s[9];
    tm->minute  = (s[10] * 10) + s[11];
    tm->second  = (s[12] * 10) + s[13];

    // Day of week
    tm->day_of_week = day_of_week(tm->year, tm->month, tm->day);

    return VALID_RTC_DATE(tm) ? SUCCESS : EINVAL;
}
コード例 #13
0
ファイル: julian_calendar.cpp プロジェクト: spezek/Khronos
	std::string Julian::to_string() const & {
		std::ostringstream oss;
		std::string era(" AD, ");
		std::string weekday;
		year_t year = year_;
		if (year < 0) {
			year = abs(year) + 1;
			era = " BC, ";
			weekday = ((julian_day_name((day_of_week(julian_to_jd(year, month_, day_)) % 7))));
		}
		else {
			weekday = ((julian_day_name((day_of_week(to_jd())))));
		}
		oss << weekday << ", " << julian_month_name(month_) << ' ' << day_ << ' ';
		oss << year << era << hms_to_string(hour_, minute_, second_);
		return oss.str();
	}
コード例 #14
0
ファイル: gregorian.cpp プロジェクト: FenLys/C_and_CPP
	string Gregorian::to_string() const {
		ostringstream oss;
		//Wednesday, January 1 1000 CE, 12:00 : 00 am
		oss << gregorian_week_name(day_of_week(*this)) << ", "
			<< gregorian_month_name(month_) << " " << day_ << " "
			<< ((year_ > 0) ? std::to_string(year_) : std::to_string(abs(year_) + 1))
			<< ((year_ > 0) ? " CE, " : " BCE, ")
			<< hms_to_string(this->to_jd());
		return oss.str();
	}
コード例 #15
0
ファイル: islamic.cpp プロジェクト: spezek/Khronos
	std::string Islamic::to_string() const & {
		std::ostringstream oss;
		std::string weekday;
		year_t year = year_;
		weekday = ((islamic_day_name(  ((day_of_week(to_jd()) + 1) % 7 )  ) )  );

		oss << weekday << ", " << islamic_month_name(month_) << ' ' << day_ << ' ';
		oss << year << ", " << hms_to_string(hour_, minute_, second_);
		return oss.str();
	}
コード例 #16
0
ファイル: subr_clock.c プロジェクト: mulichao/freebsd
void
clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
{
	time_t i, year, days;
	time_t rsec;	/* remainder seconds */
	time_t secs;

	secs = ts->tv_sec;
	days = secs / SECDAY;
	rsec = secs % SECDAY;

	ct->dow = day_of_week(days);

	/* Subtract out whole years, counting them in i. */
	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
		days -= days_in_year(year);
	ct->year = year;

	/* Subtract out whole months, counting them in i. */
	for (i = 1; days >= days_in_month(year, i); i++)
		days -= days_in_month(year, i);
	ct->mon = i;

	/* Days are what is left over (+1) from all that. */
	ct->day = days + 1;

	/* Hours, minutes, seconds are easy */
	ct->hour = rsec / 3600;
	rsec = rsec % 3600;
	ct->min  = rsec / 60;
	rsec = rsec % 60;
	ct->sec  = rsec;
	ct->nsec = ts->tv_nsec;
	if (ct_debug) {
		printf("ts_to_ct(%ld.%09ld) = ",
		    (long)ts->tv_sec, (long)ts->tv_nsec);
		print_ct(ct);
		printf("\n");
	}

	KASSERT(ct->year >= 0 && ct->year < 10000,
	    ("year %d isn't a 4 digit year", ct->year));
	KASSERT(ct->mon >= 1 && ct->mon <= 12,
	    ("month %d not in 1-12", ct->mon));
	KASSERT(ct->day >= 1 && ct->day <= 31,
	    ("day %d not in 1-31", ct->day));
	KASSERT(ct->hour >= 0 && ct->hour <= 23,
	    ("hour %d not in 0-23", ct->hour));
	KASSERT(ct->min >= 0 && ct->min <= 59,
	    ("minute %d not in 0-59", ct->min));
	/* Not sure if this interface needs to handle leapseconds or not. */
	KASSERT(ct->sec >= 0 && ct->sec <= 60,
	    ("seconds %d not in 0-60", ct->sec));
}
コード例 #17
0
ファイル: date.c プロジェクト: BigEd/wp34s
void date_alphaday(enum nilop op) {
	decNumber x;
	int y, m, d, dow;

	getX(&x);
	if (decNumberIsSpecial(&x) || extract_date(&x, &y, &m, &d))
		err(ERR_BAD_DATE);
	else {
		dow = day_of_week(y, m, d, NULL);
		copy3(&("MONTUEWEDTHUFRISATSUN"[3*(dow-1)]));
	}
}
コード例 #18
0
ファイル: datetime.c プロジェクト: 8bitgeek/bacnet-stack
void datetime_set_date(
    BACNET_DATE * bdate,
    uint16_t year,
    uint8_t month,
    uint8_t day)
{
    if (bdate) {
        bdate->year = year;
        bdate->month = month;
        bdate->day = day;
        bdate->wday = day_of_week(year, month, day);
    }
}
コード例 #19
0
/**
* @brief Set S35390A Alarm Interrupt settings, using INT2
* @param enable[in] : Alarm Interrupt enable (0 disable/1 enable).
* @param pending[in] : Alarm Interrupt pending (0 not pending/1 pending).
* @param time[in] : Alarm time value.
*/
static int  s35390a_set_alarm(unsigned char enable, unsigned char pending, struct rtc_time *alarm)
{
	unsigned char time[4] = {0};
	unsigned char reg = 0x40; /*use INT2: 0x40     use INT1:  0x04;*/
	unsigned char tmp = 0;
	unsigned char weekDayEn = 0x80;
	int ret = 0;

	if(alarm->tm_hour >= 12){
		tmp = 0x40;
	}
	DEBUG0("Set alarm %d-%d-%d-%d-%d-%d\n", alarm->tm_year+1900, alarm->tm_mon, alarm->tm_mday, alarm->tm_hour, alarm->tm_min, alarm->tm_sec);

	alarm->tm_wday = day_of_week(alarm->tm_year+1900, alarm->tm_mon, alarm->tm_mday);
	time[0] = bin2bcd(alarm->tm_wday) | weekDayEn;/*0x80 will make weekDay enable*/
	time[1] = bin2bcd(alarm->tm_hour) | 0x80;
	time[1] = time[1] |tmp;/*must set am/pm whether 12 or 24*/
	time[2] = bin2bcd(alarm->tm_min) | 0x80;
	
	
	//DEBUG0("Set alarm[0x%x][0x%x][0x%x]\n", time[0], time[1], time[2]);
	
	/**/
	ret = s35390a_get_reg(EXT_RTC_STS_REG2, &reg, 1);
	if(ret != SP_OK)
		return ret;
	
	if(enable == 1){
		reg &= 0x0f;
		reg |= 0x40;
		ret = s35390a_set_reg(EXT_RTC_STS_REG2, &reg, 1);
	}else{
		reg &= 0x0f;
		ret = s35390a_set_reg(EXT_RTC_STS_REG2, &reg, 1);
	}
	if(ret != SP_OK)
		return ret;

	ret = s35390a_set_reg(EXT_RTC_INT_REG2, time, 3);
	if(ret != SP_OK)
		return ret;
	
	ret = s35390a_get_reg(EXT_RTC_STS_REG2, &reg, 1);
	if(ret != SP_OK)
		return ret;
	//DEBUG0("enable = %d, REG = 0x%x \n", enable, reg);

	return ret;

}
コード例 #20
0
ファイル: date.cpp プロジェクト: alex8374/ufc
 string date::format(const string& __fmt) const
 {
     string __result;
     string::const_iterator __it  = __fmt.begin();
     string::const_iterator __end = __fmt.end();
     while (__it != __end)
     {
         if (*__it == '%')
         {
             if (++__it != __end)
             {
                 switch (*__it)
                 {
                 case 'w': __result.append(weekday_names[day_of_week()], 0, 3); break;
                 case 'W': __result.append(weekday_names[day_of_week()]); break;
                 case 'b': __result.append(month_names[month() - 1], 0, 3); break;
                 case 'B': __result.append(month_names[month() - 1]); break;
                 case 'd': __result += string_util::format0(day(), 2); break;
                 case 'e': __result += string_util::format(day()); break;
                 case 'f': __result += string_util::format(day(), 2); break;
                 case 'm': __result += string_util::format0(month(), 2); break;
                 case 'n': __result += string_util::format(month()); break;
                 case 'o': __result += string_util::format(month(), 2); break;
                 case 'y': __result += string_util::format0(year() % 100, 2); break;
                 case 'Y': __result += string_util::format0(year(), 4); break;
                 default:  __result += *__it;
                 }
                 ++__it;
             }
         }
         else
         {
             __result += *__it++;
         }
     }
     return __result;
 }
コード例 #21
0
static int gp_ext_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	unsigned char i = 0;
	unsigned char time[7];
	int ret = 0;
	
	DEBUG0("[%s]: Enter!\n", __FUNCTION__);	
	DEBUG0("Y,M,D = %d, %d, %d \n", tm->tm_year, tm->tm_mon, tm->tm_mday);

	if(!gp_ext_rtc_info){
		return (-EPERM);
	}
	
	/*Check year*/	
	if(tm->tm_year >= 1900)
		tm->tm_year -= 1900;

	if(tm->tm_year < 100)	/*Base on 2000*/
		return (-EPERM);
		
	tm->tm_mon += 1; /*upper layer:0~11; s35390a: 1~12*/
	
	time[0] = tm->tm_year-100;	/*UI base on 1900,but external base on 2000*/
	time[1] = tm->tm_mon;	
	time[2] = tm->tm_mday;
	tm->tm_wday = day_of_week(tm->tm_year+1900, tm->tm_mon, tm->tm_mday);	
	time[3] = tm->tm_wday;
	time[4] = tm->tm_hour;
	time[5] = tm->tm_min;
	time[6] = tm->tm_sec;

	//DEBUG0("To Reg:y[%d] m[%d] d[%d] h[%d] m[%d] s[%d]\n",time[0],time[1],time[2],time[4],time[5],time[6]);

	for (i=0;i<7;i++) {
		time[i] = bin2bcd(time[i]);
	}
	
	spin_lock_irq(&gp_ext_rtc_info->lock);
	ret = s35390a_set_reg(EXT_RTC_TIME_REG1, time, 7);/*read real time*/
	if(ret != SP_OK)
		ret = (-EIO);
	spin_unlock_irq(&gp_ext_rtc_info->lock); 	
	
	return ret;
}
コード例 #22
0
ファイル: datetime.c プロジェクト: 8bitgeek/bacnet-stack
void datetime_set_values(
    BACNET_DATE_TIME * bdatetime,
    uint16_t year,
    uint8_t month,
    uint8_t day,
    uint8_t hour,
    uint8_t minute,
    uint8_t seconds,
    uint8_t hundredths)
{
    if (bdatetime) {
        bdatetime->date.year = year;
        bdatetime->date.month = month;
        bdatetime->date.day = day;
        bdatetime->date.wday = day_of_week(year, month, day);
        bdatetime->time.hour = hour;
        bdatetime->time.min = minute;
        bdatetime->time.sec = seconds;
        bdatetime->time.hundredths = hundredths;
    }
}
コード例 #23
0
 Date next_weekday(const Date& d)
 {
     Date nwd = d;
     Day day = day_of_week(d);
     switch (day) {
     case sunday:
     case monday:
     case tuesday:
     case wednesday:
     case thursday:
         nwd.add_day(1);
         break;
     case saturday:
         nwd.add_day(2);
         break;
     case friday:
         nwd.add_day(3);
         break;
     }
     return nwd;
 }
コード例 #24
0
ファイル: subr_clock.c プロジェクト: coyizumi/cs111
void
clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
{
	int i, year, days;
	time_t rsec;	/* remainder seconds */
	time_t secs;

	secs = ts->tv_sec;
	days = secs / SECDAY;
	rsec = secs % SECDAY;

	ct->dow = day_of_week(days);

	/* Subtract out whole years, counting them in i. */
	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
		days -= days_in_year(year);
	ct->year = year;

	/* Subtract out whole months, counting them in i. */
	for (i = 1; days >= days_in_month(year, i); i++)
		days -= days_in_month(year, i);
	ct->mon = i;

	/* Days are what is left over (+1) from all that. */
	ct->day = days + 1;

	/* Hours, minutes, seconds are easy */
	ct->hour = rsec / 3600;
	rsec = rsec % 3600;
	ct->min  = rsec / 60;
	rsec = rsec % 60;
	ct->sec  = rsec;
	ct->nsec = ts->tv_nsec;
	if (ct_debug) {
		printf("ts_to_ct(%ld.%09ld) = ",
		    (long)ts->tv_sec, (long)ts->tv_nsec);
		print_ct(ct);
		printf("\n");
	}
}
コード例 #25
0
ファイル: date.c プロジェクト: BigEd/wp34s
void date_setdate(enum nilop op) {
	int d, m, y, dow;
	decNumber x;

	getX(&x);
	if (extract_date(&x, &y, &m, &d)) {
		err(ERR_BAD_DATE);
		return;
	}
	dow = day_of_week(y, m, d, NULL);
#ifdef REALBUILD
	if (Xtal) {
		busy();
		RTC_SetDate((unsigned short) y, (unsigned char) m,
			    (unsigned char) d, (unsigned char) dow);
	} else
		err(ERR_NO_CRYSTAL);
#else
	// So that very strict compilers (i.e. gcc4.6 do not complain that dow is unused with -Wall)
	(void) dow;
	err(ERR_ILLEGAL);
#endif
}
コード例 #26
0
/**
 * Generate textual weather forecast for the specified radio tower.
 */
std::string weather_forecast( const point &abs_sm_pos )
{
    std::ostringstream weather_report;
    // Local conditions
    const auto cref = overmap_buffer.closest_city( tripoint( abs_sm_pos, 0 ) );
    const std::string city_name = cref ? cref.city->name : std::string( _( "middle of nowhere" ) );
    // Current time
    weather_report << string_format(
                       _( "The current time is %s Eastern Standard Time.  At %s in %s, it was %s. The temperature was %s. " ),
                       to_string_time_of_day( calendar::turn ), print_time_just_hour( calendar::turn ),
                       city_name,
                       weather_data( g->weather ).name, print_temperature( g->temperature )
                   );

    //weather_report << ", the dewpoint ???, and the relative humidity ???.  ";
    //weather_report << "The wind was <direction> at ? mi/km an hour.  ";
    //weather_report << "The pressure was ??? in/mm and steady/rising/falling.";

    // Regional conditions (simulated by choosing a random range containing the current conditions).
    // Adjusted for weather volatility based on how many weather changes are coming up.
    //weather_report << "Across <region>, skies ranged from <cloudiest> to <clearest>.  ";
    // TODO: Add fake reports for nearby cities

    // TODO: weather forecast
    // forecasting periods are divided into 12-hour periods, day (6-18) and night (19-5)
    // Accumulate percentages for each period of various weather statistics, and report that
    // (with fuzz) as the weather chances.
    // int weather_proportions[NUM_WEATHER_TYPES] = {0};
    double high = -100.0;
    double low = 100.0;
    const tripoint abs_ms_pos = tripoint( sm_to_ms_copy( abs_sm_pos ), 0 );
    // TODO: wind direction and speed
    const time_point last_hour = calendar::turn - ( calendar::turn - calendar::time_of_cataclysm ) %
                                 1_hours;
    for( int d = 0; d < 6; d++ ) {
        weather_type forecast = WEATHER_NULL;
        const auto wgen = g->get_cur_weather_gen();
        for( time_point i = last_hour + d * 12_hours; i < last_hour + ( d + 1 ) * 12_hours; i += 1_hours ) {
            w_point w = wgen.get_weather( abs_ms_pos, i, g->get_seed() );
            forecast = std::max( forecast, wgen.get_weather_conditions( w ) );
            high = std::max( high, w.temperature );
            low = std::min( low, w.temperature );
        }
        std::string day;
        bool started_at_night;
        const time_point c = last_hour + 12_hours * d;
        if( d == 0 && calendar( to_turn<int>( c ) ).is_night() ) {
            day = _( "Tonight" );
            started_at_night = true;
        } else {
            day = _( "Today" );
            started_at_night = false;
        }
        if( d > 0 && ( ( started_at_night && !( d % 2 ) ) || ( !started_at_night && d % 2 ) ) ) {
            day = string_format( pgettext( "Mon Night", "%s Night" ), to_string( day_of_week( c ) ) );
        } else {
            day = to_string( day_of_week( c ) );
        }
        weather_report << string_format(
                           _( "%s... %s. Highs of %s. Lows of %s. " ),
                           day, weather_data( forecast ).name,
                           print_temperature( high ), print_temperature( low )
                       );
    }
    return weather_report.str();
}
コード例 #27
0
ファイル: time64.c プロジェクト: code0100fun/rubinius
struct tm64* localtime64_r(const time64_t* time64, struct tm64* tm64) {
  if(*time64 >= LONG_MIN && *time64 <= LONG_MAX) {
    struct tm tm;
    memset(&tm, 0, sizeof(tm));
    time_t time = (time_t) *time64;

    if(localtime_r(&time, &tm)) {
      tm_to_tm64(&tm, tm64);
#ifndef HAVE_TM_ZONE
#if HAVE_TZNAME && HAVE_DAYLIGHT
      tm64->tm_zone = tzname[daylight && tm64->tm_isdst];
#else
      tm64->tm_zone = NULL;
#endif
#endif
      return tm64;
    }
  }

  /*
   * First determine the (approximate) year that this timestamp
   * is in using the gmtime64_r function. We then use this so
   * we are able to map this to a year that should be compatible
   * with the system localtime_r
   */

  struct tm64 gm;
  gmtime64_r(time64, &gm);

  int64_t gm_year = gm.tm_year;

  if(gm_year < 1902) {
    /* For years below the 32 bit size time_t value we need to use
    * the lower comparable years */
    int day = day_of_week(gm_year, gm.tm_mon + 1, gm.tm_mday);
    if(gm.tm_mon == 2 && leap_year(gm_year)) {
      gm.tm_year = lower_leap_month_table[day];
    } else {
      gm.tm_year = lower_common_month_table[gm.tm_mon][day];
    }
  } else if(gm_year > 2037) {
    /* For years above the 32 bit size time_t value we need to use
     * the lower comparable years */
    int day = day_of_week(gm_year, gm.tm_mon + 1, gm.tm_mday);
    if(gm.tm_mon == 2 && leap_year(gm_year)) {
      gm.tm_year = higher_leap_month_table[day];
    } else {
      gm.tm_year = higher_common_month_table[gm.tm_mon][day];
    }
  }

  /*
   * Get the timestamp for the safe date that we mapped the
   * large date to.
   */
  time_t time = (time_t)timegm64(&gm);
  struct tm tm;
  memset(&tm, 0, sizeof(tm));

  localtime_r(&time, &tm);
  tm_to_tm64(&tm, tm64);
  tm64->tm_year = gm_year;

  /*
   * We need to correct here for the case where the timezone difference
   * results in the dates occuring in two different years.
   */
  int month_diff = tm64->tm_mon - gm.tm_mon;
  if(month_diff == 11) {
    tm64->tm_year--;
  } else if(month_diff == -11) {
    tm64->tm_year++;
  }

  /* If the gm year is a leap year, but the local isn't and we're on
   * December 31st in the local year, this is marked as day 365 because
   * the gm year is a leap year. Therefore we need to substract 1.
   */
  if(!leap_year(tm64->tm_year) && tm64->tm_yday == 365 ) {
    tm64->tm_yday--;
  }

#ifndef HAVE_TM_ZONE
#if HAVE_TZNAME && HAVE_DAYLIGHT
  tm64->tm_zone = tzname[daylight && tm64->tm_isdst];
#else
  tm64->tm_zone = NULL;
#endif
#endif

  return tm64;
}
コード例 #28
0
ファイル: Fl_Date_Time.cpp プロジェクト: GustavoMOG/efltk
Fl_String Fl_Date_Time::day_name (void) const {
   return dayname[day_of_week() - 1];
}
コード例 #29
0
ファイル: solution.c プロジェクト: blastbao/codinghighway
void day_of_week_test(int day, int month, int year, int expected) {
	struct date d = { day, month, year };
	int val;
	if ((val = day_of_week(&d)) != expected)
		fprintf(stderr, "Wrong answer for day of week of %d/%d/%d, expected was \"%s\", but got \"%s\" instead.\n", month, day, year, week[expected], week[val]);
}
コード例 #30
0
ファイル: sflcvdp.c プロジェクト: evoskuil/gsl
char *
conv_date_pict (
    long date,
    const char *picture)
{
    static char
        *month_name [] =
          {
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
          },
        *day_name [] =
          {
            "Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"
          },
        formatted [FORMAT_MAX + 1];     /*  Formatted return string          */
    int
        century,                        /*  Century component of date        */
        year,                           /*  Year component of date           */
        month,                          /*  Month component of date          */
        day,                            /*  Day component of date            */
        cursize;                        /*  Size of current component        */
    char
       *dest,                           /*  Store formatted data here        */
        ch,                             /*  Next character in picture        */
        lastch = '0';                   /*  Last character we output         */
    long
        full_date = date;

    conv_reason = 0;                    /*  No conversion errors so far      */

    /*  Zero date is returned as empty string                                */
    if (date == 0)
      {
        strclr (formatted);
        return (formatted);
      }

    default_century (&full_date);
    century = GET_CENTURY (full_date);
    year    = GET_YEAR    (full_date);
    month   = GET_MONTH   (full_date);
    day     = GET_DAY     (full_date);

    ASSERT (month > 0 && month <= 12);
    ASSERT (day   > 0 && day   <= 31);

    /*  Scan through picture, converting each component                      */
    dest = formatted;
    *dest = 0;                          /*  string is empty                  */
    while (*picture)
      {
        /*  Get character and count number of occurences                     */
        ch = *picture++;
        for (cursize = 1; *picture == ch; cursize++)
            picture++;

        switch (ch)
          {
            /*  cc        century 2 digits, 01-99                            */
            case 'c':
                if (cursize == 2)
                    sprintf (dest, "%02d", century);
                break;

            /*  y         day of year, 1-366                                 */
            /*  yy        year 2 digits, 00-99                               */
            /*  yyyy      year 4 digits, 0100-9999                           */
            case 'y':                   /*  y = day of year                  */
                if (cursize == 1)
                    sprintf (dest, "%d", julian_date (full_date));
                else
                if (cursize == 2)
                    sprintf (dest, "%02d", year);
                else
                if (cursize == 4)
                    sprintf (dest, "%02d%02d", century, year);
                break;

            /*  m         month, 1-12                                        */
            /*  mm        month, 01-12                                       */
            /*  mmm       month, 3 letters                                   */
            /*  mmmm      month, full name                                   */
            case 'm':
                if (cursize == 1)
                    sprintf (dest, (isdigit (lastch)? "%2d": "%d"), month);
                else
                if (cursize == 2)
                    sprintf (dest, "%02d", month);
                else
                if (cursize == 3)
                  {
                    memcpy (dest, month_name [month - 1], 3);
                    dest [3] = 0;
                  }
                else
                if (cursize == 4)
                    strcpy (dest, month_name [month - 1]);
                break;

            /*  MMM       month, 3-letters, ucase                            */
            /*  MMMM      month, full name, ucase                            */
            case 'M':
                if (cursize == 3)
                  {
                    memcpy (dest, month_name [month - 1], 3);
                    dest [3] = 0;
                    strupc (dest);
                  }
                else
                if (cursize == 4)
                  {
                    strcpy (dest, month_name [month - 1]);
                    strupc (dest);
                  }
                break;

            /*  d         day, 1-31                                          */
            /*  dd        day, 01-31                                         */
            /*  ddd       day of week, Sun-Sat                               */
            /*  dddd      day of week, Sunday-Saturday                       */
            case 'd':
                if (cursize == 1)
                    sprintf (dest, (isdigit (lastch)? "%2d": "%d"), day);
                else
                if (cursize == 2)
                    sprintf (dest, "%02d", day);
                else
                if (cursize == 3)
                  {
                    memcpy (dest, day_name [day_of_week (full_date)], 3);
                    dest [3] = 0;
                  }
                else
                if (cursize == 4)
                    strcpy (dest, day_name [day_of_week (full_date)]);
                break;

            /*  DDD       day of week, SUN-SAT                               */
            /*  DDDD      day of week, SUNDAY-SATURDAY                       */
            case 'D':
                if (cursize == 3)
                  {
                    memcpy (dest, day_name [day_of_week (full_date)], 3);
                    dest [3] = 0;
                    strupc (dest);
                  }
                else
                if (cursize == 4)
                  {
                    strcpy (dest, day_name [day_of_week (full_date)]);
                    strupc (dest);
                  }
                break;

            /*  w         day of week, 1-7 (1=Sunday)                        */
            /*  ww        week of year, 1-53                                 */
            case 'w':
                if (cursize == 1)
                    sprintf (dest, "%d", day_of_week (full_date) + 1);
                else
                if (cursize == 2)
                    sprintf (dest, "%d", week_of_year (full_date));
                break;

            /*  q         year quarter, 1-4                                  */
            case 'q':
                if (cursize == 1)
                    sprintf (dest, "%d", year_quarter (full_date));
                break;

            /*  \x        literal character x                                */
            case '\\':
                ch = *picture++;
        }
        if (*dest)                      /*  If something was output,         */
            while (*dest)               /*    skip to end of string          */
                dest++;
        else
            while (cursize--)           /*  Else output ch once or more      */
                *dest++ = ch;           /*    and bump dest pointer          */

        lastch = *(dest - 1);           /*  Get previous character           */
        *dest = 0;                      /*  Terminate the string nicely      */
    }
    return (formatted);
}