Skip to content

Commit

Permalink
add time-related slides
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 11, 2021
1 parent 452f32e commit c9d3295
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 29 deletions.
1 change: 1 addition & 0 deletions crystal/crystal.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"regexes.md",
"testing.md",
"sqlite.md",
"time.md",
"other.md"
]
}
7 changes: 0 additions & 7 deletions crystal/examples/my_time.out

This file was deleted.

5 changes: 5 additions & 0 deletions crystal/examples/time/add_timespan.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
now = Time.utc
tomorrow = now + Time::Span.new(days: 1)
puts now
puts tomorrow

2 changes: 2 additions & 0 deletions crystal/examples/time/add_timespan.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2021-06-11 10:06:03 UTC
2021-06-12 10:06:03 UTC
11 changes: 11 additions & 0 deletions crystal/examples/time/elapsed_time.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
t0 = Time.monotonic
sleep 1
t1 = Time.monotonic

elapsed = t1-t0
puts elapsed
puts elapsed.seconds
puts elapsed.total_seconds
puts elapsed.total_milliseconds
puts elapsed.microseconds
puts elapsed.total_microseconds
6 changes: 6 additions & 0 deletions crystal/examples/time/elapsed_time.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
00:00:01.004479308
1
1.004479308
1004.479308
4479
1004479.308
3 changes: 3 additions & 0 deletions crystal/examples/time/formatting.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time = Time.utc(2016, 2, 15, 10, 20, 30)
puts time.to_s("%Y-%m-%d %H:%M:%S %:z")
puts time.to_s("%Y-%m-%d")
2 changes: 2 additions & 0 deletions crystal/examples/time/formatting.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2016-02-15 10:20:30 +00:00
2016-02-15
3 changes: 3 additions & 0 deletions crystal/examples/time/my_time.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
now = Time.utc
puts now
puts now.year
2 changes: 2 additions & 0 deletions crystal/examples/time/my_time.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2021-06-11 10:06:26 UTC
2021
3 changes: 3 additions & 0 deletions crystal/examples/time/sleep.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
puts Time.monotonic
sleep 1.5
puts Time.monotonic
2 changes: 2 additions & 0 deletions crystal/examples/time/sleep.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
27.01:06:47.337685122
27.01:06:48.841641088
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
now = Time.utc
puts now
puts now.year


time1 = Time.utc(2016, 2, 15, 10, 20, 30)
time2 = Time.utc(2016, 2, 16, 10, 20, 30)
elapsed = time2 - time1 # Time::Span
puts elapsed.days
puts elapsed.total_seconds

tomorrow = now + Time::Span.new(days: 1)
puts tomorrow

puts time1.to_s("%Y-%m-%d %H:%M:%S %:z")
puts time1.to_s("%Y-%m-%d")
2 changes: 2 additions & 0 deletions crystal/examples/time/time_difference.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
86400.0
2 changes: 2 additions & 0 deletions crystal/examples/try.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
t = Time.monotonic
puts typeof(t)

# counter = Int32|Nil

Expand Down
11 changes: 0 additions & 11 deletions crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ shards install
![](examples/use_yaml.cr)


## Dates and Time
{id: datetime}
{i: Time}
{i: Time::Span}

* [Time](https://crystal-lang.org/api/Time.html)
* [Time::Span](https://crystal-lang.org/api/Time/Span.html)

![](examples/my_time.cr)
![](examples/my_time.out)

## Testing with Spec
{id: testing-with-spec}

Expand Down
60 changes: 60 additions & 0 deletions crystal/time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Time
{id: time}


## Dates and Time
{id: datetime}
{i: Time}
{i: Time::Span}

* [Time](https://crystal-lang.org/api/Time.html)
* [Time::Span](https://crystal-lang.org/api/Time/Span.html)

![](examples/time/my_time.cr)
![](examples/time/my_time.out)

## Sleep
{id: sleep}
{i: sleep}

* Sleep in seconds.
* It can get either and integer or a floating point number.


![](examples/time/sleep.cr)
![](examples/time/sleep.out)

## Time difference or Time::Span
{id: time-difference}

![](examples/time/time_difference.cr)
![](examples/time/time_difference.out)

## Elapsed time
{id: elapsed-time}
{i: monotonic}
{i: seconds}
{i: total_seconds}
{i: total_milliseconds}
{i: microseconds}
{i: total_microseconds}
{i: sleep}

* [Time::Span](https://crystal-lang.org/api/Time/Span.html)

![](examples/time/elapsed_time.cr)
![](examples/time/elapsed_time.out)

## Timestamp formatting
{id: timestamp-formatting}
{i: to_s}

![](examples/time/formatting.cr)
![](examples/time/formatting.out)

## Add timespan
{id: add-timespan}

![](examples/time/add_timespan.cr)
![](examples/time/add_timespan.out)

0 comments on commit c9d3295

Please sign in to comment.