NSPR Reference Previous Contents Next |
NSPR represents time in two ways, absolute time and clock/calendar time. NSPR provides types and constants for both representations, and functions to convert time values between the two.
Clock/calendar time, used for human interfaces, represents time in the familiar year, month, day, hour, minute, second components. In this form, the time zone information is important. For example, without specifying the time zone, the time 8:00AM 1 May 1998 is ambiguous. The NSPR data type for clock/calendar time, called an exploded time, has the time zone information in it, so that its corresponding point in absolute time is uniquely specified.
Types and Constants
Time Parameter Callback Functions
Functions
Macros for Time Unit Conversion
PRTime
PRTimeParameters
PRTimeParameters
In addition, callback functions are defined for determining time zone information:
Time Parameter Callback Functions
#include <prtime.h>
#define PR_MSEC_PER_SEC 1000UL
#define PR_USEC_PER_SEC 1000000UL
#define PR_NSEC_PER_SEC 1000000000UL
#define PR_USEC_PER_MSEC 1000UL
#define PR_NSEC_PER_MSEC 1000000UL
SEC
stands for
seconds, MSEC
for milliseconds, USEC
for microseconds, and NSEC
for nanoseconds.
#include <prtime.h>
typedef PRInt64 PRTime;
In NSPR, we use the more familiar term Greenwich Mean Time (GMT) in place of UTC. Although UTC and GMT are not exactly the same in their precise definitions, they can generally be treated as if they were.
Because PRTime
is a 64-bit integer, you must use the LL macros defined in
prlong.h
to manipulate PRTime
values. (See Chapter 18 "Long Long (64-bit)
Integers)"
#include <prtime.h>
typedef struct PRTimeParameters {
PRInt32 tp_gmt_offset;
PRInt32 tp_dst_offset;
} PRTimeParameters;
PRTimeParameters
structure
represents the local time zone information in terms of the offset (in seconds) from
GMT. The overall offset is broken into two components:
tp_gmt_offset
: The offset of the local standard time from GMT.
tp_dst_offset
: If daylight savings time (DST) is in effect, the DST adjustment
from the local standard time. This is most commonly 1 hour, but may also be 30
minutes or some other amount. If DST is not in effect, the tp_dst_offset
component is 0.
tp_gmt_offset
is -28800
seconds. tp_dst_offset
is 0, indicating that daylight saving time is not in
effect.
In PDT, the clock is turned forward by one hour, so the local time is 7 hours
behind GMT. This is broken down as -8 + 1 hours, so tp_gmt_offset
is
-28800 seconds, and tp_dst_offset
is 3600 seconds.
tp_gmt_offset
is 25200 seconds, and tp_dst_offset
is 0.
#include <prtime.h>
typedef struct PRExplodedTime {
PRInt32 tm_usec;
PRInt32 tm_sec;
PRInt32 tm_min;
PRInt32 tm_hour;
PRInt32 tm_mday;
PRInt32 tm_month;
PRInt16 tm_year;
PRInt8 tm_wday;
PRInt16 tm_yday;
PRTimeParameters tm_params;
} PRExplodedTime;
PRExplodedTime
structure represents clock/calendar time. PRExplodedTime
has the familiar time components: year, month, day of month, hour, minute,
second. It also has a microsecond component, as well as the day of week and the
day of year. In addition, PRExplodedTime
PRTimeParameters
structure
representing the local time zone information, so that the time point is
non-ambiguously specified.
The essential members of PRExplodedTime
are:
tm_year
: absolute year, AD. (Note that we do not count from 1900.)
tm_month
: number of months past tm_year
. The range is [0, 11]. 0 is January
and 11 is December.
tm_mday
: the day of month. The range is [1, 31]. Note that it starts from 1 as
opposed to 0.
tm_hour
: number of hours past tm_mday
. The range is [0, 23].
tm_min
: number of minutes past tm_hour
. The range is [0, 59].
tm_sec
: number of seconds past tm_min
. The range is [0, 61]. The values 60 and
61 are for accommodating up to two leap seconds.
tm_usec
: number of microseconds past tm_sec
. The range is [0, 999999].
tm_params
: a PRTimeParameters
structure representing the local time zone
information.
PRExplodedTime
are:
tm_wday
: day of week. The range is [0, 6]. 0 is Sunday, 1 is Monday, and 6 is
Saturday.
tm_yday
: day of year. The range is [0, 365]. 0 is the 1st of January.
PRExplodedTime
must
be specified. The two nonessential members (day of week and day of year) are
ignored by NSPR functions as input. When an NSPR function returns a
PRExplodedTime
object or sets a PRExplodedTime
object as output, all of the
PRExplodedTime
members are set, including the nonessential members. You can
also use PR_NormalizeTime
to calculate the values of the nonessential members.
You can define your own time parameter callback functions, which must conform
to the definition PRTimeParamFn
. Two often-used callback functions of this type are
provided by NSPR:
PRTimeParamFn
PR_LocalTimeParameters and PR_GMTParameters
#include <prtime.h>
typedef PRTimeParameters (PR_CALLBACK_DECL *PRTimeParamFn)
(const PRExplodedTime *gmt);
PRTimeParamFn
represents a callback function that, when given a time
instant in GMT, returns the time zone information (offset from GMT and DST
offset) at that time instant.
#include <prtime.h>
PRTimeParameters PR_LocalTimeParameters (
const PRExplodedTime *gmt);
PRTimeParameters PR_GMTParameters (
const PRExplodedTime *gmt);
gmt
|
A pointer to the clock/calendar time, in GMT.
|
PR_ExplodeTime
and PR_NormalizeTime
.
PR_GMTParameters
encodes the rule of the GMT. This is a trivial function; for any
input, it returns a PRTimeParameters
structure with both fields set to 0.
PR_LocalTimeParameters
encodes the rule of the local time zone.
PR_Now
PR_ExplodeTime
PR_ImplodeTime
PR_NormalizeTime
#include <prtime.h>
PRTime PR_Now(void);
PR_Now
returns the current time as number of microseconds since the NSPR epoch,
midnight (00:00:00) 1 January 1970 UTC.
You cannot assume that the values returned by PR_Now
are monotonically
increasing because the system clock of the computer may be reset. To obtain
monotonically increasing time stamps suitable for measuring elapsed time, use
PR_IntervalNow
.
#include <prtime.h>
void PR_ExplodeTime(
PRTime usecs,
PRTimeParamFn params,
PRExplodedTime *exploded);
exploded
parameter contains the converted time value.
#include <prtime.h>
PRTime PR_ImplodeTime(const PRExplodedTime *exploded);
exploded
|
A pointer to the clock/calendar time to be converted.
|
#include <prtime.h>
void PR_NormalizeTime (
PR_ExplodedTime *time,
PRTimeParamFn params);
time
|
A pointer to a clock/calendar time in the PR_ExplodedTime format.
|
params
|
A time parameter callback function.
|
Call this function in these situations:
PRExplodedTime
object that represents the date 3 March 1998 and you want to say "forty days
from 3 March 1998", you can simply add 40 to the tm_mday
field and then call
PR_NormalizeTime
.
To calculate the optional field values tm_wday
and tm_yday
. For example,
suppose you want to compute the day of week for 3 March 1998. You can set
tm_mday
to 3, tm_month
to 2, and tm_year
to 1998, and all the other fields to 0,
then call PR_NormalizeTime
with PR_GMTParameters
. On return, tm_wday
(and tm_yday
) are set for you.
To convert from one time zone to another. For example, if the input argument
time
is in time zone A and the input argument params
represents time zone B,
when PR_NormalizeTime
returns, time
will be in time zone B.
Last Updated May 18, 2001