File: sahpi_time_utils.c

package info (click to toggle)
openhpi 2.14.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,368 kB
  • ctags: 13,803
  • sloc: ansic: 187,087; cpp: 32,188; sh: 10,292; makefile: 4,466; perl: 1,529
file content (110 lines) | stat: -rw-r--r-- 3,125 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
/*      -*- linux-c -*-
 *
 * (C) Copyright IBM Corp. 2004
 *
 * 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>
 */

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

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

/**
 * 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.
 **/
SaErrorT oh_decode_time(SaHpiTimeT time, SaHpiTextBufferT *buffer)
{
	int count;
	struct tm t;
	time_t tt;
	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_MAX_RELATIVE) { /*absolute time*/
                tt = time / 1000000000;
                count = strftime((char *)working.Data, SAHPI_MAX_TEXT_BUFFER_LENGTH, "%F %T", localtime(&tt));
        } else if (time ==  SAHPI_TIME_UNSPECIFIED) {
                strcpy((char *)working.Data,"SAHPI_TIME_UNSPECIFIED     ");
		count = sizeof("SAHPI_TIME_UNSPECIFIED     ");
        } else if (time > SAHPI_TIME_UNSPECIFIED) { /*invalid time*/
                strcpy((char *)working.Data,"Invalid time     ");
		count = sizeof("Invalid time     ");
        } else {   /*relative time*/
                tt = time / 1000000000;
                localtime_r(&tt, &t);
                /* count = strftime(str, size, "%b %d, %Y - %H:%M:%S", &t); */
                count = strftime((char *)working.Data, SAHPI_MAX_TEXT_BUFFER_LENGTH, "%c", &t);
        }

        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)
{
	int err;
        struct timeval now;

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

        err = gettimeofday(&now, NULL);
	if (err) {
		err("gettimeofday failed");
		return(SA_ERR_HPI_INTERNAL_ERROR);
	}

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

	return(SA_OK);
}