source: SVN/cambria/redboot/packages/redboot/current/src/time_date.cxx @ 1

Last change on this file since 1 was 1, checked in by Tim Harvey, 2 years ago

restored latest version of files from server backup

Signed-off-by: Tim Harvey <tharvey@…>

File size: 4.6 KB
Line 
1//==========================================================================
2//
3//        time_date.cxx
4//
5//        RedBoot time/date commands
6//
7//==========================================================================
8//####ECOSGPLCOPYRIGHTBEGIN####
9// -------------------------------------------
10// This file is part of eCos, the Embedded Configurable Operating System.
11// Copyright (C) 2003 Gary Thomas
12//
13// eCos is free software; you can redistribute it and/or modify it under
14// the terms of the GNU General Public License as published by the Free
15// Software Foundation; either version 2 or (at your option) any later version.
16//
17// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20// for more details.
21//
22// You should have received a copy of the GNU General Public License along
23// with eCos; if not, write to the Free Software Foundation, Inc.,
24// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25//
26// As a special exception, if other files instantiate templates or use macros
27// or inline functions from this file, or you compile this file and link it
28// with other works to produce a work based on this file, this file does not
29// by itself cause the resulting work to be covered by the GNU General Public
30// License. However the source code for this file must still be made available
31// in accordance with section (3) of the GNU General Public License.
32//
33// This exception does not invalidate any other reasons why a work based on
34// this file might be covered by the GNU General Public License.
35//
36// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37// at http://sources.redhat.com/ecos/ecos-license/
38// -------------------------------------------
39//####ECOSGPLCOPYRIGHTEND####
40//==========================================================================
41//#####DESCRIPTIONBEGIN####
42//
43// Author(s):     gthomas
44// Contributors: 
45// Date:          2003-09-10
46// Description:   
47//####DESCRIPTIONEND####
48// -------------------------------------------------------------------------
49
50#include <redboot.h>
51#include <pkgconf/hal.h>
52#include <pkgconf/wallclock.h>
53
54#include <cyg/infra/diag.h>
55#include <cyg/io/wallclock.hxx>              // The WallClock API
56#include <cyg/io/wallclock/wallclock.inl>    // Helper functions (avoids LIBC)
57
58RedBoot_cmd("date", 
59            "Show/Set the time of day", 
60            "[YYYY/MM/DD HH:MM:SS]",
61            do_time_date
62    );
63
64static bool
65verify(cyg_uint32 val, int min, int max, char *id)
66{
67    if (((int)val < min) || ((int)val > max)) {
68        diag_printf("%s is out of range - must be [%d..%d]\n", id, min, max);
69        return false;
70    }
71    return true;
72}
73
74void
75do_time_date(int argc, char *argv[])
76{
77    cyg_uint32 now = Cyg_WallClock::wallclock->get_current_time();
78    cyg_uint32 year, month, mday, hour, minute, second;
79    char *sp;
80    bool ok = true;
81
82    if (argc == 1) {
83        // Just show the current time/date
84        _simple_mkdate(now, &year, &month, &mday, &hour, &minute, &second);
85        diag_printf("%04d/%02d/%02d %02d:%02d:%02d\n", 
86                    year, month, mday, hour, minute, second);
87    } else if (argc == 3) {
88        sp = argv[1];
89        if (!parse_num(sp, (unsigned long *)&year, &sp, "/") ||
90            !parse_num(sp, (unsigned long *)&month, &sp, "/") ||
91            !parse_num(sp, (unsigned long *)&mday, &sp, "/")) {
92            ok = false;
93        }
94        sp = argv[2];
95        if (!parse_num(sp, (unsigned long *)&hour, &sp, ":") ||
96            !parse_num(sp, (unsigned long *)&minute, &sp, ":") ||
97            !parse_num(sp, (unsigned long *)&second, &sp, ":")) {
98            ok = false;
99        }
100        if (ok) {
101            // Verify values make some sense, then set the hardware
102            if (year < 100) year += 2000;
103            ok = ok && verify(year, 1970, 2034, "year");
104            ok = ok && verify(month, 1, 12, "month");
105            ok = ok && verify(mday, 1, 31, "day");
106            ok = ok && verify(hour, 0, 23, "hour");
107            ok = ok && verify(minute, 0, 59, "minute");
108            ok = ok && verify(second, 0, 59, "second");
109            if (ok) {
110                now = _simple_mktime(year, month, mday, hour, minute, second);
111                Cyg_WallClock::wallclock->set_current_time(now);
112            }
113        }
114    } else {
115        ok = false;
116    }
117    if (!ok) {
118        diag_printf("usage: date [YYYY/MM/DD HH:MM:SS]\n");
119    }
120
121}
122
123// -------------------------------------------------------------------------
124// EOF time_date.cxx
Note: See TracBrowser for help on using the repository browser.