Skip to content

Commit

Permalink
Factored out the common current_time function.
Browse files Browse the repository at this point in the history
Also expanded the comments on `get_cache_dir_for`.

Closes #1.
  • Loading branch information
DanielKeep committed May 31, 2015
1 parent 84bde09 commit 4b08607
Showing 1 changed file with 34 additions and 35 deletions.
69 changes: 34 additions & 35 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ This module is for platform-specific stuff.

pub use self::inner::{current_time, file_last_modified, get_cache_dir_for};

#[cfg(any(unix, windows))]
mod inner_unix_or_windows {
extern crate time;

/**
Gets the current system time, in milliseconds since the UNIX epoch.
*/
pub fn current_time() -> u64 {
/*
This is kinda dicey, since *ideally* both this function and `file_last_modified` would be using the same underlying APIs. They are not, insofar as I know.
At least, not when targetting Windows.
That said, so long as everything is in the same units and uses the same epoch, it should be fine.
*/
let now_1970_utc = time::now_utc().to_timespec();
if now_1970_utc.sec < 0 || now_1970_utc.nsec < 0 {
// Fuck it.
return 0
}
let now_ms_1970_utc = (now_1970_utc.sec as u64 * 1000)
+ (now_1970_utc.nsec as u64 / 1_000_000);
now_ms_1970_utc
}
}

#[cfg(unix)]
mod inner {
extern crate time;
pub use super::inner_unix_or_windows::current_time;

use std::path::{Path, PathBuf};
use std::{cmp, env, fs};
Expand All @@ -26,22 +52,10 @@ mod inner {
}

/**
Gets the current system time, in milliseconds since the UNIX epoch.
*/
pub fn current_time() -> u64 {
/*
This is kinda dicey, since *ideally* both this function and `file_last_modified` would be using the same underlying APIs. They are not, insofar as I know.
*/
let now_1970_utc = time::now_utc().to_timespec();
if now_1970_utc.sec < 0 || now_1970_utc.nsec < 0 {
// Fuck it.
return 0
}
let now_ms_1970_utc = (now_1970_utc.sec as u64 * 1000)
+ (now_1970_utc.nsec as u64 / 1_000_000);
now_ms_1970_utc
}
Get a directory suitable for storing user- and machine-specific data which may or may not be persisted across sessions.
This is chosen to match the location where Cargo places its cache data.
*/
pub fn get_cache_dir_for<P>(product: P) -> Result<PathBuf, MainError>
where P: AsRef<Path> {
let home = match env::var_os("HOME") {
Expand All @@ -68,6 +82,8 @@ pub mod inner {
extern crate winapi;
extern crate uuid;

pub use super::inner_unix_or_windows::current_time;

use std::ffi::OsString;
use std::fmt;
use std::fs;
Expand Down Expand Up @@ -95,28 +111,11 @@ pub mod inner {
mtime_ms_1970_utc
}

/**
Gets the current system time, in milliseconds since the UNIX epoch.
*/
pub fn current_time() -> u64 {
extern crate time;

/*
This is kinda dicey, since *ideally* both this function and `file_last_modified` would be using the same underlying APIs. They are not, insofar as I know.
*/
let mut now_1970_utc = time::now_utc().to_timespec();
if now_1970_utc.sec < 0 || now_1970_utc.nsec < 0 {
// Fuck it.
now_1970_utc = time::Timespec::new(0, 0);
}
let now_ms_1970_utc = (now_1970_utc.sec as u64 * 1000)
+ (now_1970_utc.nsec as u64 / 1_000_000);
now_ms_1970_utc
}

/**
Get a directory suitable for storing user- and machine-specific data which may or may not be persisted across sessions.
This is *not* chosen to match the location where Cargo places its cache data, because Cargo is *wrong*. This is at least *less wrong*.
On Windows, LocalAppData is where user- and machine- specific data should go, but it *might* be more appropriate to use whatever the official name for "Program Data" is, though.
*/
pub fn get_cache_dir_for<P>(product: P) -> Result<PathBuf, MainError>
Expand Down

0 comments on commit 4b08607

Please sign in to comment.