Skip to main content

rl_lang/docs/entries/tutorial/t2/
p_b9.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static ADV_TIME: ConceptEntry = ConceptEntry {
4    name: "9. working with time",
5    summary: "working with time",
6    category: ConceptCategory::Tooling,
7    prerequisites: &[],
8    descriptions: &[
9        DescriptionEntry {
10            kind: DescriptionKind::Explanation,
11            title: None,
12            description: "std::time gives you the current time as a unix timestamp - an integer counting seconds since January 1 1970. time_now() returns it. store this when a task is created so you know when it was added",
13            examples: &[
14                "get time_now from std::time\n\ndec int created_at = time_now()\nprintln(created_at) // e.g. 1750000000",
15            ],
16            expected_output: &[],
17        },
18        DescriptionEntry {
19            kind: DescriptionKind::Explanation,
20            title: None,
21            description: "raw timestamps are not readable. format_date_str turns a timestamp into a date string. format_time_str gives you the time. format_time lets you build any pattern you want",
22            examples: &[
23                "get time_now, format_date_str, format_time_str, format_time from std::time\n\ndec int ts = time_now()\nprintln(format_date_str(ts))          // 2026-06-20\nprintln(format_time_str(ts))          // 14:32:07\nprintln(format_time(ts, \"%d/%m/%Y\"))  // 20/06/2026",
24            ],
25            expected_output: &[],
26        },
27        DescriptionEntry {
28            kind: DescriptionKind::Explanation,
29            title: None,
30            description: "time_diff gives you the number of seconds between two timestamps. useful for showing how old a task is",
31            examples: &[
32                "get time_now, time_diff from std::time\n\ndec int created = 1750000000\ndec int now     = time_now()\ndec int age     = time_diff(created, now)\n\nprintln(format(\"{} seconds old\", age))",
33            ],
34            expected_output: &[],
35        },
36        DescriptionEntry {
37            kind: DescriptionKind::Explanation,
38            title: None,
39            description: "time_parts breaks a timestamp into its components as an array: [year, month, day, hour, minute, second]",
40            examples: &[
41                "get time_now, time_parts from std::time\n\ndec arr[int] parts = time_parts(time_now())\nprintln(parts[0]) // year  e.g. 2026\nprintln(parts[1]) // month e.g. 6\nprintln(parts[2]) // day   e.g. 20",
42            ],
43            expected_output: &[],
44        },
45        DescriptionEntry {
46            kind: DescriptionKind::Explanation,
47            title: None,
48            description: "exercise: write a function format_age(int created_at) -> string that returns a human readable age string. use time_diff and some arithmetic\n\nexpected output examples:\n  just now\n  5 minutes ago\n  3 hours ago\n  2 days ago",
49            examples: &[
50                "get time_now, time_diff from std::time\nget format              from std::str\n\nfn format_age(int created_at) -> int {\n    dec int diff    = time_diff(created_at, time_now())\n    dec int minutes = diff / 60\n    dec int hours   = minutes / 60\n    dec int days    = hours / 24\n\n    if (diff < 60)     { return \"just now\" }\n    if (minutes < 60)  { return format(\"{} minutes ago\", minutes) }\n    if (hours < 24)    { return format(\"{} hours ago\", hours) }\n    return format(\"{} days ago\", days)\n}",
51            ],
52            expected_output: &[],
53        },
54    ],
55    pitfalls: &[],
56    related: &[],
57    related_stdlib: &[],
58    since: None,
59};