Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed LoRaWAN-lib SX1272Lib
app/unixtimestamp.cpp@11:8407ff54c40e, 2019-02-06 (annotated)
- Committer:
- MGstic
- Date:
- Wed Feb 06 19:16:44 2019 +0000
- Revision:
- 11:8407ff54c40e
- Parent:
- 10:1a85ff06be1a
V04;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MGstic | 10:1a85ff06be1a | 1 | #include "unixtimestamp.h" |
| MGstic | 10:1a85ff06be1a | 2 | |
| MGstic | 10:1a85ff06be1a | 3 | int unix_timestamp (int date[6]) |
| MGstic | 10:1a85ff06be1a | 4 | { |
| MGstic | 10:1a85ff06be1a | 5 | int seconde = 0; |
| MGstic | 10:1a85ff06be1a | 6 | int bi; |
| MGstic | 10:1a85ff06be1a | 7 | bi = bissextile (date[5]); |
| MGstic | 10:1a85ff06be1a | 8 | seconde = |
| MGstic | 10:1a85ff06be1a | 9 | date[2] + (60 * date[1]) + (3600 * date[0]) + (86400 * (date[3] - 1)); |
| MGstic | 10:1a85ff06be1a | 10 | seconde += an_to_s (date[5]) + mois_to_s (date[4], bi); |
| MGstic | 10:1a85ff06be1a | 11 | return seconde; |
| MGstic | 10:1a85ff06be1a | 12 | } |
| MGstic | 10:1a85ff06be1a | 13 | |
| MGstic | 11:8407ff54c40e | 14 | /* |
| MGstic | 11:8407ff54c40e | 15 | void chk_and_set_time(int date[6]) |
| MGstic | 11:8407ff54c40e | 16 | { |
| MGstic | 11:8407ff54c40e | 17 | |
| MGstic | 11:8407ff54c40e | 18 | struct tm t; |
| MGstic | 11:8407ff54c40e | 19 | time_t seconds; |
| MGstic | 11:8407ff54c40e | 20 | |
| MGstic | 11:8407ff54c40e | 21 | |
| MGstic | 11:8407ff54c40e | 22 | t.tm_year = date[5]; |
| MGstic | 11:8407ff54c40e | 23 | |
| MGstic | 11:8407ff54c40e | 24 | t.tm_mon = date[4]; |
| MGstic | 11:8407ff54c40e | 25 | |
| MGstic | 11:8407ff54c40e | 26 | t.tm_mday = date[3]; |
| MGstic | 11:8407ff54c40e | 27 | |
| MGstic | 11:8407ff54c40e | 28 | t.tm_hour = date[0]; |
| MGstic | 11:8407ff54c40e | 29 | |
| MGstic | 11:8407ff54c40e | 30 | t.tm_min = date[1]; |
| MGstic | 11:8407ff54c40e | 31 | |
| MGstic | 11:8407ff54c40e | 32 | t.tm_sec = date[2]; |
| MGstic | 11:8407ff54c40e | 33 | |
| MGstic | 11:8407ff54c40e | 34 | |
| MGstic | 11:8407ff54c40e | 35 | seconds = mktime(&t); |
| MGstic | 11:8407ff54c40e | 36 | set_time(seconds); |
| MGstic | 11:8407ff54c40e | 37 | printf( |
| MGstic | 11:8407ff54c40e | 38 | "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", |
| MGstic | 11:8407ff54c40e | 39 | t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec |
| MGstic | 11:8407ff54c40e | 40 | ); |
| MGstic | 11:8407ff54c40e | 41 | }*/ |
| MGstic | 11:8407ff54c40e | 42 | |
| MGstic | 10:1a85ff06be1a | 43 | int bissextile (int an) |
| MGstic | 10:1a85ff06be1a | 44 | { |
| MGstic | 10:1a85ff06be1a | 45 | int bi = 0; |
| MGstic | 10:1a85ff06be1a | 46 | if (((an % 4 == 0) && (an % 100 != 0)) || (an % 400 == 0)) { |
| MGstic | 10:1a85ff06be1a | 47 | bi = 1; |
| MGstic | 10:1a85ff06be1a | 48 | } |
| MGstic | 10:1a85ff06be1a | 49 | return bi; |
| MGstic | 10:1a85ff06be1a | 50 | } |
| MGstic | 10:1a85ff06be1a | 51 | |
| MGstic | 10:1a85ff06be1a | 52 | int mois_to_s (int tab, int bi) |
| MGstic | 10:1a85ff06be1a | 53 | { |
| MGstic | 10:1a85ff06be1a | 54 | int sec; |
| MGstic | 10:1a85ff06be1a | 55 | int jour = 0; |
| MGstic | 10:1a85ff06be1a | 56 | for (int i = 1; i <= tab - 1; i++) { |
| MGstic | 10:1a85ff06be1a | 57 | if (i == 2 && bi) { |
| MGstic | 10:1a85ff06be1a | 58 | jour += 29; |
| MGstic | 10:1a85ff06be1a | 59 | } else if (i == 2) { |
| MGstic | 10:1a85ff06be1a | 60 | jour += 28; |
| MGstic | 10:1a85ff06be1a | 61 | } else if ((i == 1) || (i == 3) || (i == 5) || (i == 7) || (i == 8) |
| MGstic | 10:1a85ff06be1a | 62 | || (i == 10) || (i == 12)) { |
| MGstic | 10:1a85ff06be1a | 63 | jour += 31; |
| MGstic | 10:1a85ff06be1a | 64 | } else { |
| MGstic | 10:1a85ff06be1a | 65 | jour += 30; |
| MGstic | 10:1a85ff06be1a | 66 | } |
| MGstic | 10:1a85ff06be1a | 67 | } |
| MGstic | 10:1a85ff06be1a | 68 | sec = jour * 86400; |
| MGstic | 10:1a85ff06be1a | 69 | return sec; |
| MGstic | 10:1a85ff06be1a | 70 | } |
| MGstic | 10:1a85ff06be1a | 71 | |
| MGstic | 10:1a85ff06be1a | 72 | int an_to_s (int an) |
| MGstic | 10:1a85ff06be1a | 73 | { |
| MGstic | 10:1a85ff06be1a | 74 | int seco = 0; |
| MGstic | 10:1a85ff06be1a | 75 | |
| MGstic | 10:1a85ff06be1a | 76 | for (int i = 1970; i <= an - 1; i++) { |
| MGstic | 10:1a85ff06be1a | 77 | if (bissextile (i)) { |
| MGstic | 10:1a85ff06be1a | 78 | seco += 366 * 86400; |
| MGstic | 10:1a85ff06be1a | 79 | } else { |
| MGstic | 10:1a85ff06be1a | 80 | seco += 365 * 86400; |
| MGstic | 10:1a85ff06be1a | 81 | } |
| MGstic | 10:1a85ff06be1a | 82 | } |
| MGstic | 10:1a85ff06be1a | 83 | return seco; |
| MGstic | 10:1a85ff06be1a | 84 | } |
| MGstic | 10:1a85ff06be1a | 85 | |
| MGstic | 10:1a85ff06be1a | 86 | void Whex_buffer(int deb,int tab[8]) |
| MGstic | 10:1a85ff06be1a | 87 | { |
| MGstic | 10:1a85ff06be1a | 88 | unsigned int mask=0x0000000F; |
| MGstic | 10:1a85ff06be1a | 89 | for (int j =0;j<=7;j++) |
| MGstic | 10:1a85ff06be1a | 90 | { |
| MGstic | 10:1a85ff06be1a | 91 | int i=(deb&mask) ; |
| MGstic | 10:1a85ff06be1a | 92 | tab[7-j]=i; |
| MGstic | 10:1a85ff06be1a | 93 | //printf("%X\n",i); |
| MGstic | 10:1a85ff06be1a | 94 | deb>>=4; |
| MGstic | 10:1a85ff06be1a | 95 | } |
| MGstic | 10:1a85ff06be1a | 96 | |
| MGstic | 10:1a85ff06be1a | 97 | } |