Skip to content

Commit

Permalink
time.h: time() and ctime() (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
yulvil authored and elliotchance committed Oct 23, 2017
1 parent f43c66d commit aaae775
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
42 changes: 42 additions & 0 deletions noarch/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package noarch

import "time"

// TimeT is the representation of "time_t".
type TimeT int32

// NullToTimeT converts a NULL to an array of TimeT.
func NullToTimeT(i int32) []TimeT {
return []TimeT{}
}

// Time returns the current time.
func Time(tloc []TimeT) TimeT {
var t = TimeT(int32(time.Now().Unix()))

if len(tloc) > 0 {
tloc[0] = t
}

return t
}

// IntToTimeT converts an int32 to a TimeT.
func IntToTimeT(t int32) TimeT {
return TimeT(t)
}

// Ctime converts TimeT to a string.
func Ctime(tloc []TimeT) []byte {
if len(tloc) > 0 {
var t = time.Unix(int64(tloc[0]), 0)
return []byte(t.Format(time.ANSIC) + "\n")
}

return nil
}

// TimeTToFloat64 converts TimeT to a float64. It is used by the tests.
func TimeTToFloat64(t TimeT) float64 {
return float64(t)
}
4 changes: 4 additions & 0 deletions program/function_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ var builtInFunctionDefinitions = []string{
"long unsigned int strtoul(const char *, char **, int) -> noarch.Strtoul",
"long long unsigned int strtoull(const char *, char **, int) -> noarch.Strtoull",

// time.h
"time_t time(time_t *) -> noarch.Time",
"char* ctime(const time_t *) -> noarch.Ctime",

// I'm not sure which header file these comes from?
"uint32 __builtin_bswap32(uint32) -> darwin.BSwap32",
"uint64 __builtin_bswap64(uint64) -> darwin.BSwap64",
Expand Down
48 changes: 48 additions & 0 deletions tests/time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <time.h>

#include "tests.h"

#define START_TEST(t) \
diag(#t); \
test_##t();

void test_time()
{
time_t now;
time_t tloc;

now = time(NULL);
is_not_eq(now, 0);

now = time(&tloc);
is_not_eq(now, 0);
is_eq(now, tloc);
}

void test_ctime()
{
char* s;

// 1999-12-31 11:59:58
time_t now = 946670398;
s = ctime(&now);
is_not_null(s);

// Hours/minutes will vary based on local time. Ignore them.
s[11] = 'H';
s[12] = 'H';
s[14] = 'm';
s[15] = 'm';
is_streq(s, "Fri Dec 31 HH:mm:58 1999\n");
}

int main()
{
plan(5);

START_TEST(time)
START_TEST(ctime)

done_testing();
}
3 changes: 2 additions & 1 deletion types/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var simpleResolveTypes = map[string]string{
"long int": "int32",
"long long": "int64",
"long long int": "int64",
"long long unsigned int": "uint64",
"long unsigned int": "uint32",
"long": "int32",
"short": "int16",
Expand All @@ -39,7 +40,6 @@ var simpleResolveTypes = map[string]string{
"unsigned short int": "uint16",
"void": "",
"_Bool": "bool",
"long long unsigned int": "uint64",

// void* is treated like char*
"void*": "[]byte",
Expand All @@ -58,6 +58,7 @@ var simpleResolveTypes = map[string]string{
"div_t": "github.com/elliotchance/c2go/noarch.DivT",
"ldiv_t": "github.com/elliotchance/c2go/noarch.LdivT",
"lldiv_t": "github.com/elliotchance/c2go/noarch.LldivT",
"time_t": "github.com/elliotchance/c2go/noarch.TimeT",

// Darwin specific
"__darwin_ct_rune_t": "github.com/elliotchance/c2go/darwin.CtRuneT",
Expand Down

0 comments on commit aaae775

Please sign in to comment.