项目作者: lucamora

项目描述 :
Simple Particle library to switch DST time
高级语言: C++
项目地址: git://github.com/lucamora/particle-dst.git
创建时间: 2017-07-01T15:44:14Z
项目社区:https://github.com/lucamora/particle-dst

开源协议:MIT License

下载


Particle-dst

DST switching made simple!

What is it?

Particle-dst is a simple Particle library that can be easily used to switch manually or automatically DST all over the world on Particle devices!

Installation

Particle-dst is easy to install because it is a normal Particle library.
There are three ways to install this library:

Command Line Interface (CLI)

First of all, go to your project directory:

  1. cd /path/to/your/fantastic/project

Then install the library with the following command:

  1. particle library add particle-dst

Web IDE (Build)

  1. Select your project or create a new one
  2. Click on Libraries in the left sidebar
  3. In the search field type: dst
  4. Select particle-dst
  5. Click on ÌNCLUDE IN PROJECT

Desktop IDE (Dev)

  1. Select your project or create a new one
  2. Click on Browse and manage Particle libraries in the left sidebar
  3. In the search field type: dst
  4. In the particle-dst panel click on Use

Usage

1) Define limits

The library enable and disable DST using two limits that define the beginning and the end dates of DST in your country.
Limits can be created using the specific type provided by the library:

  1. // create the two limits
  2. dst_limit_t beginning;
  3. dst_limit_t end;
  4. // define beginning of DST
  5. beginning.hour = 2;
  6. beginning.day = DST::days::tue;
  7. beginning.month = DST::months::feb;
  8. beginning.occurrence = 2;
  9. // define end of DST
  10. end.hour = 3;
  11. end.day = DST::days::wed;
  12. end.month = DST::months::oct;
  13. end.occurrence = 2;

To define days and months you can use the Particle numbering or you can easily use the enumerators provided by the library. (The values will be converted to the Particle numbering)

  1. beginning.day = DST::days::tue;
  2. // same as
  3. beginning.day = 3; // tuesday
  1. beginning.month = DST::months::feb;
  2. // same as
  3. beginning.months = 2; // february

The occurrence indicates in which week of the target month the DST change.
It could have positive and negative values to indicate if the occurrence is from the beginning or the end of the month:

  • positive: occurrence from the beginning of the month
  • negative: occurrence from the end of the month
    1. beginning.occurrence = 1; // first week
    2. beginning.occurrence = -1; // last week

2) Initialize the library

To start using the library, initialize it with the two limits. The last parameter indicates the DST offset in your country.

  1. DST dst;
  2. dst_limit_t beginning;
  3. dst_limit_t end;
  4. void setup() {
  5. // initialize limits
  6. // ...
  7. dst.begin(beginning, end, 1); // DST adds 1 hour
  8. }

3) Use it

There are two methods to use this library: manual mode where the user application has to periodically check if the DST has changed or automatic mode where the library switch DST on its own.

Manual mode

In manual mode the user has to periodically trigger the library to check if DST changed.
To switch DST, simply call one method defined in the library:

  1. bool enabled = dst.check();
  2. // returns true if DST is enabled and false if it is disabled
  3. Serial.printlnf("DST: %s", enabled ? "enabled" : "disabled");

Automatic mode

In automatic mode the library checks every hour if DST is enabled or disabled automatically without the need of any user interaction.
Simply activate the automatic mode passing true to the automatic() method and you’re done!

NOTE: You have to enable the automatic mode after the initialization of the library otherwise it will not work!

  1. dst.begin(beginning, end, 1);
  2. // ...
  3. dst.automatic(true); // enable automatic mode

You can disable the automatic mode whenever you want passing false to the automatic() method:

  1. dst.automatic(false); // disable automatic mode

Other features

Format limits

With particle-dst you can get the timestamp of the beginning and the end of DST in the current year:

  1. int beginning = dst.beginning();
  2. // example: 1487037600
  3. int end = dst.end();
  4. // example: 1507690800

Moreover it is possible to get a formatted string of the limits of the DST in the current year:

  1. char beginning[] = dst.beginning("%a, %d %B @ %R");
  2. // example: Tue, 14 February @ 02:00
  3. char end[] = dst.end("%a, %d %B @ %R");
  4. // example: Wed, 11 October @ 03:00

The library supports all the Particle format strings:

DST enabled

  1. bool enabled = dst.enabled();

This function returns true if DST is currently enabled and false otherwise.

Time zone

Set the local time zone.

  1. // set time zone to Central European Time
  2. dst.timezone(1);

API

  1. void begin(dst_limit_t beginning_limit, dst_limit_t end_limit, int offset);
  1. bool check();
  1. void automatic(bool enable);
  1. int beginning();
  1. char* beginning(String format);
  1. int end();
  1. char* end(String format);
  1. bool enabled();
  1. void timezone(int zone);

Help me

I’ve tried to develop this library to be easy to use without having to write many lines of code for doing a simple activity.
If you have some suggestions or you found a bug (oh no!), please send me an e-mail at luca.morandini98@gmail.com!

That’s all folks!

Thanks and happy coding!