public static int GetIdOfFirstPostOfDay(DateTimeOffset day) { var items = Query( $"/2.2/posts", $"pagesize=1&order=asc&min={day.ToUnixTimeSeconds()}&max={day.AddDays(1).ToUnixTimeSeconds()}&sort=creation").Result; return items[0].post_id; }
public void ToUnixTimeInSeconds_DateTimeOffset_ShouldMatch() { var offset = new DateTimeOffset(2016, 1, 8, 4, 11, 28, TimeSpan.FromHours(7)); var expected = offset.ToUnixTimeSeconds(); var actual = offset.ToUnixTimeInSeconds(); Assert.AreEqual(expected, actual); }
public static byte[] EncodeBlockHeader(UInt32 Version, UInt256 PreviousBlock, UInt256 MerkleRoot, DateTimeOffset Time, UInt32 Bits, UInt32 Nonce) { using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { writer.WriteUInt32(Version); writer.WriteUInt256(PreviousBlock); writer.WriteUInt256(MerkleRoot); writer.WriteUInt32((uint)Time.ToUnixTimeSeconds()); writer.WriteUInt32(Bits); writer.WriteUInt32(Nonce); return stream.ToArray(); } }
// to unitime /* public static int UnixTime(DateTime now) { //(long)new DateTime(1970, 1, 1, 9, 0, 0, 0).ToFileTimeUtc(); return (int)(now.ToFileTimeUtc() / 10000000 - 11644506000L); } */ private void Form1_Load(object sender, EventArgs e) { string dt_str = ""; // unixtime : date this.label2.Text = "^:UnixTime v:DateTime"; DateTime now_unix_time_dt = DateTime.Now; TimeSpan ts = new TimeSpan(0, 0, 0, 0); now_unix_time_dt = now_unix_time_dt + ts; dt_str = now_unix_time_dt.ToString(); this.textBox2.Text = dt_str; var dto = new DateTimeOffset(now_unix_time_dt, new TimeSpan(+09, 00, 00)); var dtot = dto.ToUnixTimeSeconds(); dt_str = dtot.ToString(); this.textBox1.Text = dt_str; }
// クリップボードにテキストがコピーされると呼び出される private void OnClipBoardChanged(object sender, ClipboardEventArgs args) { string input_str; int i = 0; this.textBox1.Text = args.Text; input_str = this.textBox1.Text; // unixtime or datetime を確認 DateTime dt; if (DateTime.TryParse(input_str, out dt)) { // date -> unixtime this.label2.Text = "DateTime -> UnixTime"; var dto = new DateTimeOffset(dt, new TimeSpan(+09, 00, 00)); var dtot = dto.ToUnixTimeSeconds(); string dt_str = dtot.ToString(); this.textBox2.Text = dt_str; } else if ( int.TryParse(input_str, out i) ) { // unixtime -> date this.label2.Text = "UnixTime -> DateTime"; //int input_int = int.Parse(input_str); var dto = (DateTimeOffset.FromUnixTimeSeconds(int.Parse(input_str)).ToLocalTime()); string dt_str = dto.ToString(); this.textBox2.Text = dt_str; } else { this.label2.Text = "DateTimeに変換できません。"; this.textBox2.Text = ""; } }
async Task<int> GetStackOverflowReputation(DateTimeOffset start, DateTimeOffset end, int firstPostIdToConsider) { dynamic items = await QueryHelpers.Query( $"/2.2/users/{StackOverflowID}/reputation", $"fromdate={start.ToUnixTimeSeconds()}&todate={end.ToUnixTimeSeconds()}"); int reputation = 0; foreach (var item in items) { // Ignore if the post that the event is attached to is too old if (item.post_id < firstPostIdToConsider) continue; if (item.vote_type == "up_votes") { if (item.post_type == "answer") { reputation += 10; } else { // No points for upvotes on questions } } else if (item.vote_type == "accepts") { reputation += 20; } else { // We ignore bounties } } return reputation; }