1 /* $NetBSD: stime.c,v 1.2 2000/10/15 15:33:46 bjh21 Exp $ */ 2 3 /* 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "from: @(#)touch.c 8.2 (Berkeley) 4/28/95"; 45 #endif 46 __RCSID("$NetBSD: stime.c,v 1.2 2000/10/15 15:33:46 bjh21 Exp $"); 47 #endif /* not lint */ 48 49 50 #include <err.h> 51 #include <string.h> 52 #include <time.h> 53 #include <tzfile.h> 54 55 #include "stime.h" 56 57 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 58 59 time_t 60 stime(arg) 61 char *arg; 62 { 63 struct tm *t; 64 time_t tmptime; 65 int yearset; 66 char *p; 67 /* Start with the current time. */ 68 if (time(&tmptime) == (time_t)-1) 69 err(1, "time"); 70 71 if ((t = localtime(&tmptime)) == NULL) 72 err(1, "localtime"); 73 /* [[CC]YY]MMDDhhmm[.SS] */ 74 if ((p = strchr(arg, '.')) == NULL) 75 t->tm_sec = 0; /* Seconds defaults to 0. */ 76 else { 77 if (strlen(p + 1) != 2) 78 goto terr; 79 *p++ = '\0'; 80 t->tm_sec = ATOI2(p); 81 } 82 83 yearset = 0; 84 switch (strlen(arg)) { 85 case 12: /* CCYYMMDDhhmm */ 86 t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE; 87 yearset = 1; 88 /* FALLTHOUGH */ 89 case 10: /* YYMMDDhhmm */ 90 if (yearset) { 91 t->tm_year += ATOI2(arg); 92 } else { 93 yearset = ATOI2(arg); 94 if (yearset < 69) 95 t->tm_year = yearset + 2000 - TM_YEAR_BASE; 96 else 97 t->tm_year = yearset + 1900 - TM_YEAR_BASE; 98 } 99 /* FALLTHROUGH */ 100 case 8: /* MMDDhhmm */ 101 t->tm_mon = ATOI2(arg); 102 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 103 /* FALLTHROUGH */ 104 case 6: 105 t->tm_mday = ATOI2(arg); 106 /* FALLTHROUGH */ 107 case 4: 108 t->tm_hour = ATOI2(arg); 109 /* FALLTHROUGH */ 110 case 2: 111 t->tm_min = ATOI2(arg); 112 break; 113 default: 114 goto terr; 115 } 116 117 t->tm_isdst = -1; /* Figure out DST. */ 118 tmptime = mktime(t); 119 if (tmptime == (time_t)-1) 120 terr: errx(1, 121 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 122 123 return (tmptime); 124 } 125