File: sahpi_time_utils.c

package info (click to toggle)
openhpi 3.8.0-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 31,796 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (155 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*      -*- linux-c -*-
 *
 * (C) Copyright IBM Corp. 2004
 * (C) Copyright Pigeon Point Systems. 2010
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Author(s):
 *      Steve Sherman <stevees@us.ibm.com>
 *      Anton Pak <anton.pak@pigeonpoint.com>
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include <glib.h>

#include <SaHpi.h>
#include <oh_utils.h>
#include <oh_error.h>


/**
 * oh_localtime:
 * @time: SaHpiTimeT time to be converted.                    
 * @tm: Location to store the converted broken-down time.
 *
 * Converts an SaHpiTimeT time value to broken-down
 * time representation.
 * 
 * Returns:
 * SA_OK - normal operation. 
 * SA_ERR_HPI_INVALID_PARAMS - @buffer is NULL.
 * SA_ERR_HPI_INTERNAL_ERROR - conversion failed
 **/
SaErrorT oh_localtime(const SaHpiTimeT time, struct tm *tm)
{
        time_t t;
        struct tm *tm2;

        if (!tm) {
                return(SA_ERR_HPI_INVALID_PARAMS);
        }

        t = (time_t)(time / 1000000000);

#ifndef _WIN32
        tm2 = localtime_r(&t, tm);
#else /* _WIN32 */
        // Windows version of localtime is thread-safe
        tm2 = localtime(&t);
        if (tm2) {
                *tm = *tm2;
        }
#endif /* _WIN32 */

        if (!tm2) {
                return SA_ERR_HPI_INTERNAL_ERROR;
        }

        return(SA_OK);
}

/**
 * oh_decode_time:
 * @time: SaHpiTimeT time to be converted.                    
 * @buffer: Location to store the converted string.
 *
 * Converts an SaHpiTimeT time value to the preferred date/time string 
 * representation defined for the current locale.
 * String is stored in an SaHpiTextBufferT data structure.
 * 
 * Returns:
 * SA_OK - normal operation. 
 * SA_ERR_HPI_INVALID_PARAMS - @buffer is NULL.
 * SA_ERR_HPI_INTERNAL_ERROR - @buffer not big enough to accomodate
 *                             date/time representation string or
 *                             conversion failed.
 **/
SaErrorT oh_decode_time(SaHpiTimeT time, SaHpiTextBufferT *buffer)
{
	int count;
	SaErrorT err;
	SaHpiTextBufferT working;

	if (!buffer) {
		return(SA_ERR_HPI_INVALID_PARAMS);
	}

	err = oh_init_textbuffer(&working);
	if (err != SA_OK) { return(err); }

        if (time == SAHPI_TIME_UNSPECIFIED) {
                strcpy((char *)working.Data,"SAHPI_TIME_UNSPECIFIED     ");
		count = sizeof("SAHPI_TIME_UNSPECIFIED     ");
        } else if (time > SAHPI_TIME_MAX_RELATIVE) { /*absolute time*/
                struct tm tm;
                err = oh_localtime(time, &tm);
                if (err != SA_OK) {
                        return(err);
                }
                count = strftime((char *)working.Data, SAHPI_MAX_TEXT_BUFFER_LENGTH, "%Y-%m-%d %H:%M:%S", &tm);
        } else {   /*relative time*/
                long secs  = (long)( time / 1000000000L );
                long mins  = ( secs / 60L ) % 60L;
                long hours = ( secs / 3600L ) % 24L;
                long days  = secs / 86400L;
                secs = secs % 60L;
                count = snprintf( (char *)working.Data,
                                  SAHPI_MAX_TEXT_BUFFER_LENGTH,
                                  "RELATIVE: %ldd %02ld:%02ld:%02ld",
                                  days, hours, mins, secs );
        }

        if (count == 0) { return(SA_ERR_HPI_INTERNAL_ERROR); }
	else working.DataLength = count;
	
	err = oh_copy_textbuffer(buffer, &working);
	if (err != SA_OK) { return(err); }

	return(SA_OK);
}

/**
 * oh_gettimeofday:
 * @time: Location to store Time of Day value
 *
 * Find the time of day and converts it into an HPI time.
 * 
 * Returns:
 * SA_OK - normal operation.
 * SA_ERR_HPI_INVALID_PARAMS - @time is NULL.
 **/
SaErrorT oh_gettimeofday(SaHpiTimeT *time)
{
        GTimeVal now;

	if (!time) {
		return(SA_ERR_HPI_INVALID_PARAMS);
	}

        g_get_current_time(&now);

        *time = (SaHpiTimeT)now.tv_sec * 1000000000 + now.tv_usec * 1000;

	return(SA_OK);
}