/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server and client library.
 * $Id: nfstime.c,v 1.1 2001/07/20 07:41:55 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * http://n4.samba.org/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * 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.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "includes.h"

/**
 * \file nfstime.c
 *
 * Functions for managing the NFS time/date types.  These support
 * greater range and resolution than we can manage on Linux at the
 * moment.
 **/

//                  struct nfstime4 {
//                          int64_t seconds;
//                          uint32_t nseconds;
//                  }
//                  enum time_how4 {
//                          SET_TO_SERVER_TIME4 = 0,
//                          SET_TO_CLIENT_TIME4 = 1
//                  };
//                  union settime4 switch (time_how4 set_it) {
//                   case SET_TO_CLIENT_TIME4:
//                           nfstime4       time;
//                   default:
//                           void;
//                  };


n4err n4unpack_nfstime (struct n4inbuf *inb,
			struct n4nfstime *t)
{
	CHECK_ERR (n4inbuf_need (inb, 3));

	n4unpack_signed64_nocheck (inb, &t->seconds);
	n4unpack_nocheck (inb, &t->nseconds);

	n4trace ("unpacked nfstime %lld.%09d", t->seconds, t->nseconds);

	return N4OK;
}



n4err n4pack_nfstime (struct n4outbuf *outb,
		      const struct n4nfstime *t)
{
	CHECK_ERR (n4outbuf_expand (outb, 3));

	n4pack_signed64_nocheck (outb, t->seconds);
	n4pack_nocheck (outb, t->nseconds);

	return N4OK;
}



n4err n4unpack_settime (struct n4inbuf *inb,
			struct n4settime *st)
{
	CHECK_ERR (n4unpack_checked (inb, &st->set_how));
	
	switch (st->set_how) {
	case NFS4_SET_TO_SERVER_TIME:
		return N4OK;

	case NFS4_SET_TO_CLIENT_TIME:
		return n4unpack_nfstime (inb, &st->time);

	default:
		n4log_error("illegal time_how4 value %#x", st->set_how);
		return N4ERR_RPC_GARBAGE;
	}
}


n4err n4pack_settime (struct n4outbuf *outb,
		      const struct n4settime *st)
{
	if (st->set_how != NFS4_SET_TO_CLIENT_TIME
	    && st->set_how != NFS4_SET_TO_SERVER_TIME) {
		n4log_error("illegal time_how4 value %#x", st->set_how);
		return NFS4ERR_INVAL;
	}
	
	CHECK_ERR (n4pack_checked (outb, st->set_how));

	if (st->set_how == NFS4_SET_TO_CLIENT_TIME) {
		CHECK_ERR (n4pack_nfstime (outb, &st->time));
	}
	
	return N4OK;
}


n4err n4nfstime_from_unix (struct n4nfstime *nfstime,
			   time_t unix_time,
			   unsigned long usec)
{
	nfstime->seconds = unix_time;

	/* unix uses microseconds for the fractional part; NFS4 uses
	 * nanoseconds (!). */
	nfstime->nseconds = usec * 1000;

	return N4OK;
}


n4err n4nfstime_to_unix (const struct n4nfstime *nfstime,
			 time_t *t)
{
	*t = nfstime->seconds;

	return N4OK;
}
