How to work with Chrono Literals?

I like the fast that we can use chrono literals in our function calls like sleep and delay. I understand that with sleep, the .duration default is not millis just like delay. All this is good and there is some explanation in the docs.

Is there a way to construct literals from a variable? Can a literal be assigned to a variable? It seems that this could make for better user interaction (literal as an input to a Particle function) and publishes.

Thanks, Chip

Chip

Thanks for raising this - I hadn’t yet looked at Chrono Literals. Could you please give an example of what you would like to do but don’t know how?

The values assigned to each constant variables are referred to as the “literals” . Generally, both terms, constants and literals are used interchangeably. For eg, “const int = 5;“, is a constant expression and the value 5 is referred to as constant integer literal. There are four types of literals in C and five types of literals in C++ (int, float, char, string, boolean).

I think this can provide some additional reference:
https://en.cppreference.com/w/cpp/header/chrono

Here are the literal operators:

image

3 Likes

@nrobinson2000,

OK, will have to play with this a bit. Thank you for the reference.

Chip

1 Like

what you would like to do but don’t know how?

The values assigned to each constant variables are referred to as the “literals” . Generally, both terms, constants and literals are used interchangeably. For eg, “const int = 5;“, is a constant expression and the value 5 is referred to as constant integer literal.

@minousoso,

Here are a few things that are on my “to-do” list to play with.

  1. When constructing messages for the Particle console, I typically build a string using snprintf() and then print. Many times, the only reason I do this is to add units (such as 30 sec). Can I publish a chrono literal in such a way as it includes the unit when published?

  2. When taking input from a user Particle.function() is a chrono literal handled the same was as a string? Would I need to parse 30s or could I assign this directly to a variable to simplify the code?

  3. How do string literals handle fractions and can you do math:

  • is the chrono literal for 36 hours 1.5days or is it 1day 12hours?
  • can I manipulate the values such as 30s + 30(?) = 1min?

I know these are basic questions but I want to see what I can do with this new capability and, so far, all the examples given are with constants (eg .duration(30s) not variables .duration(sleepDuration)).

Thanks, Chip

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.