plptools
Loading...
Searching...
No Matches
psitime.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 2000-2002 Fritz Elfert <felfert@to.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20#pragma once
21
22#include "config.h"
23
24#include <iostream>
25#include <cstdint>
26
27#include <sys/time.h>
28#include <time.h>
29#include <unistd.h>
30#include <sys/types.h>
31
32#include "plpintl.h"
33
40typedef struct psi_timeval_t {
45 std::ostream::fmtflags old = o.flags();
46 uint64_t micro = ptv.tv_high;
47 micro = (micro << 32) | ptv.tv_low;
48 micro /= 1000000;
49 int s = micro % 60;
50 micro /= 60;
51 int m = micro % 60;
52 micro /= 60;
53 int h = micro % 24;
54 micro /= 24;
55 int d = micro % 365;
56 micro /= 365;
57 int y = micro;
58 o << std::dec;
59 if (y > 0)
60 o << y << ((y > 1) ? _(" years ") : _(" year "));
61 if (d > 0)
62 o << d << ((d > 1) ? _(" days ") : _(" day "));
63 if (h > 0)
64 o << h << ((h != 1) ? _(" hours ") : _(" hour "));
65 if (m > 0)
66 o << m << ((m != 1) ? _(" minutes ") : _(" minute "));
67 o << s << ((s != 1) ? _(" seconds") : _(" second"));
68 o.flags(old);
69 return o;
70 }
74 uint32_t tv_low;
78 uint32_t tv_high;
80
84typedef struct psi_timezone_t {
86 std::ostream::fmtflags old = s.flags();
87 int h = ptz.utc_offset / 3600;
88 int m = ptz.utc_offset % 3600;
89 s << "offs: " << std::dec << h << "h";
90 if (m != 0)
91 s << ", " << m << "m";
92 s.flags(old);
93 return s;
94 }
95 signed long utc_offset;
96 unsigned long dst_zones;
97 unsigned long home_zone;
99
124class PsiTime {
125public:
132 PsiTime(struct timeval *_utv, struct timezone *_utz = nullptr);
133
139 PsiTime(time_t time);
140
147 PsiTime(psi_timeval *_ptv, psi_timezone *_ptz = nullptr);
148
155 PsiTime(const uint32_t _ptvHi, const uint32_t _ptvLo);
156
160 PsiTime(void);
161
165 PsiTime(const PsiTime &t);
166
170 ~PsiTime();
171
177 void setPsiTime(psi_timeval *_ptv);
178
184 void setSiboTime(uint32_t stime);
185
192 void setPsiTime(const uint32_t _ptvHi, const uint32_t _ptvLo);
193
199 void setPsiZone(psi_timezone *_ptz);
200
206 void setUnixTime(struct timeval *_utv);
207
213 void setUnixTime(time_t time);
214
219 void setUnixNow(void);
220
227 struct timeval &getTimeval(void);
228
235 time_t getTime(void);
236
243 uint32_t getSiboTime();
244
252
260 uint32_t getPsiTimeLo(void);
261
269 uint32_t getPsiTimeHi(void);
270
281 friend std::ostream &operator<<(std::ostream &s, const PsiTime &t);
282
286 PsiTime &operator=(const PsiTime &t);
287
291 bool operator==(const PsiTime &t);
292 bool operator<(const PsiTime &t);
293 bool operator>(const PsiTime &t);
294
295 enum zone {
300 PSI_TZ_HOME = 0x40000000
301 };
302
303private:
304 void psi2unix(void);
305 void unix2psi(void);
306 void tryPsiZone();
307
310 struct timeval utv;
311 struct timezone utz;
313};
314
325class PsiZone {
326 friend class RPCS32;
327
328public:
333 static PsiZone &getInstance();
334
343 bool getZone(psi_timezone &ptz);
344
345private:
350
354 PsiZone();
355
356 void setZone(psi_timezone &ptz);
357
360};
Psion time related utility class.
Definition: psitime.h:124
void psi2unix(void)
Definition: psitime.cc:253
bool operator==(const PsiTime &t)
Comparison operators.
Definition: psitime.cc:158
~PsiTime()
Destroys the instance.
Definition: psitime.cc:86
bool operator>(const PsiTime &t)
Definition: psitime.cc:172
struct timezone utz
Definition: psitime.h:311
uint32_t getPsiTimeHi(void)
Retrieves the instance's current value in Psion time format, low 32 bits.
Definition: psitime.cc:144
void setPsiZone(psi_timezone *_ptz)
Sets the Psion time zone of this instance.
Definition: psitime.cc:120
bool ptzValid
Definition: psitime.h:312
psi_timeval & getPsiTimeval(void)
Retrieves the instance's current value in Psion time format.
Definition: psitime.cc:136
struct timeval utv
Definition: psitime.h:310
PsiTime & operator=(const PsiTime &t)
Assignment operator.
Definition: psitime.cc:148
uint32_t getPsiTimeLo(void)
Retrieves the instance's current value in Psion time format, high 32 bits.
Definition: psitime.cc:140
uint32_t getSiboTime()
Retrieves the instance's current value in SIBO time format.
Definition: psitime.cc:246
@ PSI_TZ_NORTHERN
Definition: psitime.h:298
@ PSI_TZ_NONE
Definition: psitime.h:296
@ PSI_TZ_EUROPEAN
Definition: psitime.h:297
@ PSI_TZ_SOUTHERN
Definition: psitime.h:299
@ PSI_TZ_HOME
Definition: psitime.h:300
void setUnixNow(void)
Sets the value of this instance to the current time of the Unix machine.
Definition: psitime.cc:102
bool operator<(const PsiTime &t)
Definition: psitime.cc:164
psi_timeval ptv
Definition: psitime.h:308
psi_timezone ptz
Definition: psitime.h:309
void setSiboTime(uint32_t stime)
Modifies the value of this instance.
Definition: psitime.cc:237
friend std::ostream & operator<<(std::ostream &s, const PsiTime &t)
Prints the instance's value in human readable format.
Definition: psitime.cc:180
time_t getTime(void)
Retrieves the instance's current value in Unix time format.
Definition: psitime.cc:132
void setUnixTime(struct timeval *_utv)
Sets the value of this instance.
Definition: psitime.cc:90
PsiTime(void)
Constructs a new instance, initializing to now.
Definition: psitime.cc:31
struct timeval & getTimeval(void)
Retrieves the instance's current value in Unix time format.
Definition: psitime.cc:128
void setPsiTime(psi_timeval *_ptv)
Modifies the value of this instance.
Definition: psitime.cc:108
void tryPsiZone()
Definition: psitime.cc:276
void unix2psi(void)
Definition: psitime.cc:265
A singleton wrapper for a psi_timezone .
Definition: psitime.h:325
void setZone(psi_timezone &ptz)
Definition: psitime.cc:297
bool getZone(psi_timezone &ptz)
Retrieve the Psion time zone.
Definition: psitime.cc:303
PsiZone()
Private constructor.
Definition: psitime.cc:292
static PsiZone * _instance
This objects instance (singleton)
Definition: psitime.h:349
psi_timezone _ptz
Definition: psitime.h:359
static PsiZone & getInstance()
Retrieve the singleton object.
Definition: psitime.cc:286
bool _ptzValid
Definition: psitime.h:358
This is the implementation of the RPCS protocol for Psion series 5 (EPOC) variant.
Definition: rpcs32.h:36
#define _(String)
Definition: plpintl.h:34
struct psi_timeval_t psi_timeval
Holds a Psion time value.
struct psi_timezone_t psi_timezone
holds a Psion time zone description.
Holds a Psion time value.
Definition: psitime.h:40
uint32_t tv_low
The lower 32 bits.
Definition: psitime.h:74
friend std::ostream & operator<<(std::ostream &o, const psi_timeval_t &ptv)
Prints a psi_timeval in human readable format.
Definition: psitime.h:44
uint32_t tv_high
The upper 32 bits.
Definition: psitime.h:78
holds a Psion time zone description.
Definition: psitime.h:84
friend std::ostream & operator<<(std::ostream &s, const psi_timezone_t &ptz)
Definition: psitime.h:85
unsigned long home_zone
Definition: psitime.h:97
unsigned long dst_zones
Definition: psitime.h:96
signed long utc_offset
Definition: psitime.h:95