|
| 1 | +open Printf |
| 2 | +open Core |
| 3 | + |
| 4 | +(*****************************************************************************) |
| 5 | +(* *) |
| 6 | +(* Working with Dates and Time *) |
| 7 | +(* *) |
| 8 | +(* The standard library does not provide a Date or DateTime module but it *) |
| 9 | +(* leverages the Unix module to work with dates and timestamps. The [Unix] *) |
| 10 | +(* module provides a set of time functions and a specific record type that *) |
| 11 | +(* represents wallclock and calendar time (it's called [tm]). *) |
| 12 | +(* *) |
| 13 | +(* Thankfully, the popular library [Core] offers two different modules: *) |
| 14 | +(* 1. [Date]: Type and functions to work with calendar dates. *) |
| 15 | +(* 2. [Time_float]: Type and functions to work with raw time. It also has *) |
| 16 | +(* a submodule [Zone] needed for when a function requires a timezone *) |
| 17 | +(* argument. The most used time zone is [Core.Time_float.Zone.utc]. *) |
| 18 | +(* *) |
| 19 | +(*****************************************************************************) |
| 20 | + |
| 21 | +module Exercise = struct |
| 22 | + let my_birthday = |
| 23 | + let date = Date.create_exn ~y:1992 ~m:Month.Apr ~d:9 in |
| 24 | + let time = Time_float.Ofday.of_string "10:36:15" in |
| 25 | + Time_float.of_date_ofday ~zone:Time_float.Zone.utc date time |
| 26 | + ;; |
| 27 | + |
| 28 | + let today = Time_float.now () |
| 29 | + |
| 30 | + let run () = |
| 31 | + let living_days = |
| 32 | + Time_float.Span.to_day @@ Time_float.abs_diff today my_birthday |
| 33 | + in |
| 34 | + let living_years = living_days /. 365.0 in |
| 35 | + printf "%f years have passed since I was born.\n" living_years |
| 36 | + ;; |
| 37 | +end |
| 38 | + |
| 39 | +(*****************************************************************************) |
| 40 | +(* *) |
| 41 | +(* Dificultad Extra (Opcional) *) |
| 42 | +(* *) |
| 43 | +(* Utilizando la fecha de tu cumpleaños, formatéala y muestra su resultado *) |
| 44 | +(* de 10 maneras diferentes. Por ejemplo: *) |
| 45 | +(* *) |
| 46 | +(* - Día, mes y año. *) |
| 47 | +(* - Hora, minuto y segundo. *) |
| 48 | +(* - Día del año. *) |
| 49 | +(* - Día de la semana. *) |
| 50 | +(* - Nombre del mes. *) |
| 51 | +(* (lo que se te ocurra...) *) |
| 52 | +(* *) |
| 53 | +(*****************************************************************************) |
| 54 | + |
| 55 | +module Challenge = struct |
| 56 | + let pst = Time_float.Zone.of_utc_offset ~hours:(-8) |
| 57 | + let my_birthday = Exercise.my_birthday |
| 58 | + let bday_as_date = Time_float.to_date ~zone:pst my_birthday |
| 59 | + |
| 60 | + let run () = |
| 61 | + print_endline "My birthday formatted in 10 different times:"; |
| 62 | + printf "%s\n" @@ Time_float.to_string_utc my_birthday; |
| 63 | + printf "%s\n" @@ Time_float.to_sec_string_with_zone ~zone:pst my_birthday; |
| 64 | + printf "%s\n" @@ Time_float.to_filename_string ~zone:pst my_birthday; |
| 65 | + printf "%s\n" @@ Date.to_string_american bday_as_date; |
| 66 | + printf "%s\n" @@ Date.to_string_iso8601_basic bday_as_date; |
| 67 | + printf "Day %d\n" @@ Date.day bday_as_date; |
| 68 | + printf "%s\n" (Date.day_of_week bday_as_date |> Day_of_week.to_string_long); |
| 69 | + printf "%s\n" (Date.month bday_as_date |> Month.to_string); |
| 70 | + printf |
| 71 | + "Unix Timestamp (ms): %d\n" |
| 72 | + (Time_float.to_span_since_epoch my_birthday |
| 73 | + |> Time_float.Span.to_sec |
| 74 | + |> int_of_float); |
| 75 | + Time_float.to_string_abs_parts ~zone:pst my_birthday |
| 76 | + |> List.iter ~f:print_endline |
| 77 | + ;; |
| 78 | +end |
| 79 | + |
| 80 | +let _ = |
| 81 | + Exercise.run (); |
| 82 | + Challenge.run () |
| 83 | +;; |
0 commit comments