FatTime.h
Go to the documentation of this file.
1 /****
2  * FatTime.h
3  *
4  * This file is part of the FatIFS Library
5  *
6  * This library is free software: you can redistribute it and/or modify it under the terms of the
7  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along with this library.
14  * If not, see <https://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #pragma once
19 
20 #include <IFS/TimeStamp.h>
21 #include <DateTime.h>
22 
26 union FatTime {
27  static constexpr unsigned BaseYear{1980};
28 
29  struct {
30  uint16_t time;
31  uint16_t date;
32  };
33  struct {
34  uint32_t second : 5;
35  uint32_t minute : 6;
36  uint32_t hour : 5;
37  uint32_t day : 5;
38  uint32_t month : 4;
39  uint32_t year : 7;
40  };
41  uint32_t value;
42 
43  FatTime(uint32_t fdatetime = 0) : value(fdatetime)
44  {
45  }
46 
47  FatTime(uint16_t fdate, uint16_t ftime) : time(ftime), date(fdate)
48  {
49  }
50 
52  {
53  }
54 
56  : second(dt.Second / 2U), minute(dt.Minute), hour(dt.Hour), day(dt.Day), month(dt.Month + 1U),
57  year(dt.Year - BaseYear)
58  {
59  }
60 
61  operator DateTime() const
62  {
63  DateTime dt;
64  if(value != 0) {
65  dt.setTime(second * 2, minute, hour, day, month - 1, year + BaseYear);
66  }
67  return dt;
68  }
69 
70  explicit operator time_t() const
71  {
72  return DateTime(*this);
73  }
74 };
75 static_assert(sizeof(FatTime) == 4, "Bad FatTime");
@ Second
Definition: Timezone.h:26
Date and time class.
Definition: DateTime.h:80
void setTime(time_t time)
Set time using Unix timestamp.
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:36
FAT timestamp support.
Definition: FatTime.h:26
FatTime(uint16_t fdate, uint16_t ftime)
Definition: FatTime.h:47
uint16_t time
Definition: FatTime.h:30
uint32_t day
Definition: FatTime.h:37
FatTime(DateTime dt)
Definition: FatTime.h:55
uint32_t month
Definition: FatTime.h:38
uint32_t year
Definition: FatTime.h:39
uint32_t minute
Definition: FatTime.h:35
uint32_t value
Definition: FatTime.h:41
uint32_t second
Definition: FatTime.h:34
FatTime(uint32_t fdatetime=0)
Definition: FatTime.h:43
FatTime(IFS::TimeStamp ts)
Definition: FatTime.h:51
static constexpr unsigned BaseYear
Definition: FatTime.h:27
uint16_t date
Definition: FatTime.h:31
uint32_t hour
Definition: FatTime.h:36