forked from patricoferris/get-activity
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
type t = | ||
[ `Last_week | ||
| `Range of string * string | ||
| `Since_last_fetch ] | ||
|
||
let one_week = 60. *. 60. *. 24. *. 7. | ||
|
||
let to_8601 t = | ||
let open Unix in | ||
let t = gmtime t in | ||
Printf.sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ" | ||
(t.tm_year + 1900) | ||
(t.tm_mon + 1) | ||
(t.tm_mday) | ||
(t.tm_hour) | ||
(t.tm_min) | ||
(t.tm_sec) | ||
|
||
let mtime path = | ||
match Unix.stat path with | ||
| info -> Some info.Unix.st_mtime | ||
| exception Unix.Unix_error(Unix.ENOENT, _, _) -> None | ||
|
||
let set_mtime path time = | ||
if not (Sys.file_exists path) then | ||
close_out @@ open_out_gen [Open_append; Open_creat] 0o600 path; | ||
Unix.utimes path time time | ||
|
||
let with_period period ~last_fetch_file ~f = | ||
let now = Unix.time () in | ||
let last_week = now -. one_week in | ||
let range = | ||
match period with | ||
| `Since_last_fetch -> | ||
let last_fetch = Option.value ~default:last_week (mtime last_fetch_file) in | ||
(to_8601 last_fetch, to_8601 now) | ||
| `Last_week -> | ||
(to_8601 last_week, to_8601 now) | ||
| `Range r -> r | ||
in | ||
f range; | ||
match period with | ||
| `Since_last_fetch | `Last_week -> set_mtime last_fetch_file now | ||
| `Range _ -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
type t = | ||
[ `Last_week | ||
| `Range of string * string | ||
| `Since_last_fetch ] | ||
|
||
val one_week : float | ||
|
||
val to_8601 : float -> string | ||
|
||
val with_period : t -> last_fetch_file:string -> f:(string * string -> unit) -> unit | ||
(** Run [f (start, finish)], where [(start, finish)] is the period specified by [period]. | ||
If [period] is [`Since_last_fetch] or [`Last_week] then update the last-fetch timestamp on success. *) |