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#ifndef _PSITIME_H_
21#define _PSITIME_H_
22
23#include "config.h"
24
25#include <iostream>
26#include <cstdint>
27
28#include <sys/time.h>
29#include <time.h>
30#include <unistd.h>
31#include <sys/types.h>
32
33#include "plpintl.h"
34
41typedef struct psi_timeval_t {
46 std::ostream::fmtflags old = o.flags();
47 uint64_t micro = ptv.tv_high;
48 micro = (micro << 32) | ptv.tv_low;
49 micro /= 1000000;
50 int s = micro % 60;
51 micro /= 60;
52 int m = micro % 60;
53 micro /= 60;
54 int h = micro % 24;
55 micro /= 24;
56 int d = micro % 365;
57 micro /= 365;
58 int y = micro;
59 o << std::dec;
60 if (y > 0)
61 o << y << ((y > 1) ? _(" years ") : _(" year "));
62 if (d > 0)
63 o << d << ((d > 1) ? _(" days ") : _(" day "));
64 if (h > 0)
65 o << h << ((h != 1) ? _(" hours ") : _(" hour "));
66 if (m > 0)
67 o << m << ((m != 1) ? _(" minutes ") : _(" minute "));
68 o << s << ((s != 1) ? _(" seconds") : _(" second"));
69 o.flags(old);
70 return o;
71 }
75 uint32_t tv_low;
79 uint32_t tv_high;
81
85typedef struct psi_timezone_t {
87 std::ostream::fmtflags old = s.flags();
88 int h = ptz.utc_offset / 3600;
89 int m = ptz.utc_offset % 3600;
90 s << "offs: " << std::dec << h << "h";
91 if (m != 0)
92 s << ", " << m << "m";
93 s.flags(old);
94 return s;
95 }
96 signed long utc_offset;
97 unsigned long dst_zones;
98 unsigned long home_zone;
100
125class PsiTime {
126public:
133 PsiTime(struct timeval *_utv, struct timezone *_utz = nullptr);
134
140 PsiTime(time_t time);
141
148 PsiTime(psi_timeval *_ptv, psi_timezone *_ptz = nullptr);
149
156 PsiTime(const uint32_t _ptvHi, const uint32_t _ptvLo);
157
161 PsiTime(void);
162
166 PsiTime(const PsiTime &t);
167
171 ~PsiTime();
172
178 void setPsiTime(psi_timeval *_ptv);
179
185 void setSiboTime(uint32_t stime);
186
193 void setPsiTime(const uint32_t _ptvHi, const uint32_t _ptvLo);
194
200 void setPsiZone(psi_timezone *_ptz);
201
207 void setUnixTime(struct timeval *_utv);
208
214 void setUnixTime(time_t time);
215
220 void setUnixNow(void);
221
228 struct timeval &getTimeval(void);
229
236 time_t getTime(void);
237
244 uint32_t getSiboTime();
245
253
261 uint32_t getPsiTimeLo(void);
262
270 uint32_t getPsiTimeHi(void);
271
282 friend std::ostream &operator<<(std::ostream &s, const PsiTime &t);
283
287 PsiTime &operator=(const PsiTime &t);
288
292 bool operator==(const PsiTime &t);
293 bool operator<(const PsiTime &t);
294 bool operator>(const PsiTime &t);
295
296 enum zone {
301 PSI_TZ_HOME = 0x40000000
302 };
303
304private:
305 void psi2unix(void);
306 void unix2psi(void);
307 void tryPsiZone();
308
311 struct timeval utv;
312 struct timezone utz;
314};
315
326class PsiZone {
327 friend class rpcs32;
328
329public:
334 static PsiZone &getInstance();
335
344 bool getZone(psi_timezone &ptz);
345
346private:
351
355 PsiZone();
356
357 void setZone(psi_timezone &ptz);
358
361};
362#endif
Psion time related utility class.
Definition: psitime.h:125
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:312
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:313
psi_timeval & getPsiTimeval(void)
Retrieves the instance's current value in Psion time format.
Definition: psitime.cc:136
struct timeval utv
Definition: psitime.h:311
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:299
@ PSI_TZ_NONE
Definition: psitime.h:297
@ PSI_TZ_EUROPEAN
Definition: psitime.h:298
@ PSI_TZ_SOUTHERN
Definition: psitime.h:300
@ PSI_TZ_HOME
Definition: psitime.h:301
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:309
psi_timezone ptz
Definition: psitime.h:310
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:326
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:350
psi_timezone _ptz
Definition: psitime.h:360
static PsiZone & getInstance()
Retrieve the singleton object.
Definition: psitime.cc:286
bool _ptzValid
Definition: psitime.h:359
This is the implementation of the rpcs protocol for Psion series 5 (EPOC) variant.
Definition: rpcs32.h:36
#define _(String)
Definition: plpintl.h:35
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:41
uint32_t tv_low
The lower 32 bits.
Definition: psitime.h:75
friend std::ostream & operator<<(std::ostream &o, const psi_timeval_t &ptv)
Prints a psi_timeval in human readable format.
Definition: psitime.h:45
uint32_t tv_high
The upper 32 bits.
Definition: psitime.h:79
holds a Psion time zone description.
Definition: psitime.h:85
friend std::ostream & operator<<(std::ostream &s, const psi_timezone_t &ptz)
Definition: psitime.h:86
unsigned long home_zone
Definition: psitime.h:98
unsigned long dst_zones
Definition: psitime.h:97
signed long utc_offset
Definition: psitime.h:96