Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
C0D3-M4513R committed Apr 4, 2022
0 parents commit 6f1533d
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Rust Builds

on:
push:
branches: [dev,main]
# Remove the line above to run when pushing to master
pull_request:
branches: [ main ]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: windows-latest
strategy:
matrix:
profile: [release] #--release -v,
target: [x86_64-pc-windows-msvc]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
- uses: actions-rs/cargo@v1
with:
command: build
args: --profile ${{ matrix.profile }} --target ${{ matrix.target }}
- name: Save build
uses: actions/upload-artifact@v3
with:
name: time_${{ matrix.profile }}_${{ matrix.target }}.exe
path: target/${{ matrix.target }}/${{ matrix.profile }}/time.exe
- uses: actions-rs/cargo@v1
with:
command: test
args: --profile ${{ matrix.profile }} --target ${{ matrix.target }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/untitled1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "time"
version = "0.1.0"
edition = "2021"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 C0D3 M4513R

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# time
This repository is a binary, that writes the current time to a file.
The produced executable will expect a path to a file.
In that file specified the current time will be written.
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

use test;
87 changes: 87 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::arch::x86_64::_mm_pause;
use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};
use std::os::windows::fs::FileExt;
use std::thread::{sleep, yield_now};
use std::time::{Duration, Instant};

type Result<T> = std::result::Result<T, std::io::Error>;

fn main() -> Result<()> {
let env: Vec<String> = std::env::args().collect();
if env.len() < 2 {
println!(
"Enter the FilePath, to write the time to as a argument {:#?}",
env
);
return Ok(());
}
let filepath = unsafe { env.get_unchecked(1) };
//Safety: Checked above
println!(
"The contents of the following file will be deleted.
It will be used for this program, to write the current system time to.
'{}'",
filepath
);
pause();
let mut test = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(filepath)?;
let mut times = 0;
loop {
let time = std::time::SystemTime::now();
let s = Instant::now();
let dur = time
.duration_since(std::time::UNIX_EPOCH)
.expect("The clock was set to before 1970-01-01 00:00:00. Please set your clock.");
let seconds = dur.as_secs() % 60;
let minutes = dur.as_secs() / 60 % 60;
let hours = dur.as_secs() / 60 / 60 % 24;
let time = format!("{:02}:{:02}:{:02}", hours, minutes, seconds);
let r = test.seek(SeekFrom::Start(0));
if r.is_err() {
let r1 = test.seek_write(time.as_bytes(), 0);
if r.is_err() {
eprintln!(
"Could not seek, or seek and write.\n\
Error for seek is {}. Error for seek and write is {}.",
r.unwrap_err(),
r1.unwrap_err()
);
}
} else {
let r = test.write_all(time.as_bytes());
if r.is_err() {
eprintln!(
"Could not write to file. File write returned {}",
r.unwrap_err()
);
}
}
times += 1;
let dur_no_sec = dur - Duration::from_secs(dur.as_secs());
if (dur_no_sec + s.elapsed()).as_millis() > 100 && times > 10 {
panic!(
"Something went wrong. We wrote {}ms after the second changed.",
(dur_no_sec + s.elapsed()).as_millis()
);
}
println!(
"{}ms slow, processing & writing took {}ns",
dur_no_sec.as_millis(),
s.elapsed().as_nanos()
);
sleep(Duration::from_millis(1000) - dur_no_sec - s.elapsed());
}
}

fn pause() {
println!("Press any key to continue...");
let clin = std::io::stdin();
let mut str = "".to_string();
clin.read_line(&mut str).unwrap();
println!("Resuming.");
}

0 comments on commit 6f1533d

Please sign in to comment.