public static DateTime ChangeDay(this DateTime date, DayOfWeek day, DayIndex index) { var firstOfMonth = FirstDayOfMonth(date); int offset = (int)day - (int)firstOfMonth.DayOfWeek; if (offset < 0) { offset += 7; } var newDate = firstOfMonth.AddDays(offset + 7 * (int)index); if (newDate.Month != date.Month) { // must be last in month (else wouldn't have gone over, so go back one week newDate = newDate.AddDays(-7); } return newDate; }
/// <summary> /// Gets the hash code /// </summary> /// <returns>Hash code</returns> public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hashCode = 41; if (DayIndex != null) { hashCode = hashCode * 59 + DayIndex.GetHashCode(); } if (StartTime != null) { hashCode = hashCode * 59 + StartTime.GetHashCode(); } if (EndTime != null) { hashCode = hashCode * 59 + EndTime.GetHashCode(); } return(hashCode); } }
public static DateTime ChangeDay(this DateTime date, DayOfWeek day, DayIndex index) { var firstOfMonth = FirstDayOfMonth(date); int offset = (int)day - (int)firstOfMonth.DayOfWeek; if (offset < 0) { offset += 7; } var newDate = firstOfMonth.AddDays(offset + 7 * (int)index); if (newDate.Month != date.Month) { // must be last in month (else wouldn't have gone over, so go back one week newDate = newDate.AddDays(-7); } return(newDate); }
/// <summary> /// Returns true if StoreOpenHour instances are equal /// </summary> /// <param name="input">Instance of StoreOpenHour to be compared</param> /// <returns>Boolean</returns> public bool Equals(StoreOpenHour input) { if (input == null) { return(false); } return (( DayIndex == input.DayIndex || DayIndex != null && DayIndex.Equals(input.DayIndex) ) && ( StartTime == input.StartTime || StartTime != null && StartTime.Equals(input.StartTime) ) && ( EndTime == input.EndTime || EndTime != null && EndTime.Equals(input.EndTime) )); }
public MonthlyRecurrency(DateTime startDate, int numOccurrences = _Default_Occurrences, int interval = 1, DayOfWeek dayOfWeek = DayOfWeek.Monday, DayIndex dayIndex = DayIndex.First) : base(startDate, numOccurrences, interval) { SetTypeAndDays(MonthlyType.Weekday, dayOfWeek, dayIndex); }
private void SetTypeAndDays(MonthlyType type, DayOfWeek dayOfWeek, DayIndex dayIndex) { _Type = type; DayOfWeek = dayOfWeek; DayIndex = dayIndex; }
/// <summary> /// 返回日线数据、5分钟数据 /// </summary> /// <param name="dataType">读取数据类型,“DAY,5MIN”</param> /// <param name="dataCode">证券代码,例如SH600050,SZ000005,如果是获取全部数据则传入NULL</param> /// <returns></returns> public DataTable GetStockDataLine(string dataType, string dataCode) { //数据文件的完整路径 string filePath = ""; //读取不同的文件 switch (dataType) { case "DAY": filePath = DataPath + Market + "\\DAY.DAT"; break; case "5MIN": filePath = DataPath + Market + "\\MIN.DAT"; break; default: filePath = DataPath + Market + "\\DAY.DAT";; break; } //判断文件是否存在 this.CheckFile(filePath); if (message == filePath + "文件不存在") { return(null); } //构建保存日线、分钟线的DataTable DataTable dtData = new DataTable(); dtData.Columns.Add("证券代码", typeof(string)); dtData.Columns.Add("日期", typeof(string)); dtData.Columns.Add("开盘", typeof(string)); dtData.Columns.Add("最高", typeof(string)); dtData.Columns.Add("最低", typeof(string)); dtData.Columns.Add("收盘", typeof(string)); dtData.Columns.Add("成交量", typeof(string)); dtData.Columns.Add("成交额", typeof(string)); //dtData.Columns.Add("上涨数", typeof(string));//数据不是很正确,弃用,用程序计算取代 //dtData.Columns.Add("下跌数", typeof(string));//数据不是很正确,弃用,用程序计算取代 //打开数据文件,建立文件流 FileStream fs = null; BinaryReader br = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); br = new BinaryReader(fs); } catch { message = "读取文件" + filePath + "失败"; return(null); } finally { message = "读取文件" + filePath + "成功"; } /*程序读取逻辑 * 1、读取证券总数 * 2、读取证券数据索引,取出每个证券的数据条数、每个块的偏移量 * 3、从每个块中取出每条记录 */ //从0C开始读,0C-0F 表示证券总数 fs.Position = 0x0c; //12 //DAY.DAT所保存的股票数量 int stockCount = br.ReadInt32(); //从18h开始至 40017h 每64byte为一条股票数据分配记录,总共有多少由0x0c提供 //数据索引开始位置 int indexStartAddress = 0x18; //18h //数据索引块大小 int indexBlock = 0x40; // 64 //数据索引块数量,每个股票25个块 int indexBlockNum = 25; //证券数据的开始位置 int dataStarAddress = 0x41000; //证券数据的块大小,8k int dataBlock = 8192; //证券记录块中记录数,每条记录的大小32字节,8K可记录256条 int dataNum = 256; //遍历每条股票 for (int i = 0; i < stockCount; i++) { //实例化数据索引结构 DayIndex dataIndex = new DayIndex(); //实例化日线索引结构中的数据块偏移量数组,否则下面用到时会报错 dataIndex.blockOffSet = new Int16[indexBlockNum]; //设置当前位置 fs.Position = indexStartAddress + i * indexBlock; //取证券代码,10个字节,后面是\0,去掉 dataIndex.stockCode = this.Market + System.Text.Encoding.Default.GetString(br.ReadBytes(10)).Replace("\0", ""); //判断是否取到证券代码 if (string.IsNullOrEmpty(dataIndex.stockCode)) { break; } //如果股票代码在静态代码表中,判断需要获取的单个代码 DataRow[] rows = Main.staticStockCode.Select("证券代码 = '" + dataIndex.stockCode + "'"); bool isAllCode = true; if (dataCode == null) //传入的证券代码 { isAllCode = rows.Length > 0; } else { //如果是查询单个证券,则当前证券代码=传入的证券代码而且传入的证券代码在静态代码表中 isAllCode = (dataIndex.stockCode == dataCode && rows.Length > 0); } if (isAllCode == true) { //读4字节该股票代码的日线记录数 dataIndex.dataCount = br.ReadInt32(); //读25次,每次两字节,共50字节,记录块的偏移量,没有数据块后,偏移量为FF FF(-1) for (int k = 0; k < indexBlockNum; k++) { dataIndex.blockOffSet[k] = br.ReadInt16(); } //开始遍历该股票的所有日线记录,一直读到股票日线数量为止 int l = 0; //for (int l = 0; l < dataIndex.dataCount; l++) //用for死循环,l不累加,待查 while (l < dataIndex.dataCount) { //每个记录块8K大小,包含256条记录,每条记录32字节,从41000h开始的8KB为第0号数据存储块,以后类推 //遍历偏移量 for (int m = 0; m < indexBlockNum; m++) { Int16 offset = dataIndex.blockOffSet[m]; //偏移量 //偏移量为-1,说明没有数据了 if (offset == -1) { break; } //转到41000h开始的第offset块数据,位置为偏移量*8K加上起始位置41000h fs.Position = dataStarAddress + offset * dataBlock; //每条记录32字节,共256条记录 for (int n = 0; n < dataNum; n++) { //t==0表示没有数据了,256条没有填满,或者在文件最后由于有几条记录就只写几条数据,读出会溢出 if (l >= dataIndex.dataCount) { break; } //每个32字节块的前4个字节转成INT32表示时间 //日线表示19700101开始的天数 //5分钟表示19700101开始的秒数 Int32 t = br.ReadInt32(); //UInt32 t = br.ReadUInt32(); if (t == 0) { break; } //实例化证券数据结构 DayLine dzhDay = new DayLine(); //计算日期还是时间 switch (dataType) { case "DAY": dzhDay.m_time = date19700101.AddDays(t / 86400).ToString("yyyy-MM-dd"); break; case "5MIN": dzhDay.m_time = date19700101.AddSeconds(t).ToString("yyyy-MM-dd HH:mm:ss"); break; default: dzhDay.m_time = date19700101.AddDays(t / 86400).ToString("yyyy-MM-dd"); break; } //接着读4*6+2+2共28字节 dzhDay.m_fOpen = br.ReadSingle(); dzhDay.m_fHigh = br.ReadSingle(); dzhDay.m_fLow = br.ReadSingle();; dzhDay.m_fClose = br.ReadSingle();; dzhDay.m_fVolume = br.ReadSingle(); dzhDay.m_fAmount = br.ReadSingle();; dzhDay.m_wAdvance = br.ReadInt16(); dzhDay.m_wDecline = br.ReadInt16(); //将读出的一条日线数据保存到表格中 DataRow dRow = dtData.NewRow(); dRow["证券代码"] = dataIndex.stockCode; dRow["日期"] = dzhDay.m_time; dRow["开盘"] = dzhDay.m_fOpen.ToString("F2"); dRow["最高"] = dzhDay.m_fHigh.ToString("F2"); dRow["最低"] = dzhDay.m_fLow.ToString("F2"); dRow["收盘"] = dzhDay.m_fClose.ToString("F2"); dRow["成交量"] = dzhDay.m_fVolume.ToString("F0"); dRow["成交额"] = dzhDay.m_fAmount.ToString("F0"); //dRow["上涨数"] = dzhDay.m_wAdvance.ToString("F0"); //dRow["下跌数"] = dzhDay.m_wDecline.ToString("F0"); dtData.Rows.Add(dRow); l++; } } } } } br.Close(); fs.Close(); fs.Dispose(); return(dtData); }
public YearlyRecurrency(DateTime startDate, int numOccurrences = _Default_Occurrences, int interval = 1, DayOfWeek dayOfWeek = DayOfWeek.Monday, DayIndex dayIndex = DayIndex.First, int month = 1) : base(startDate, numOccurrences, interval, dayOfWeek, dayIndex) { _Month = month; }
public YearlyRecurrency(DateTime startDate, DateTime endDate, int interval = 1, DayOfWeek dayOfWeek = DayOfWeek.Monday, DayIndex dayIndex = DayIndex.First, int month = 1) : base(startDate, endDate, interval, dayOfWeek, dayIndex) { _Month = month; }