time.c 906 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <venti.h>
  12. int
  13. vttimefmt(Fmt *fmt)
  14. {
  15. int64_t ns;
  16. Tm tm;
  17. if(fmt->flags&FmtLong){
  18. ns = nsec();
  19. tm = *localtime(ns/1000000000);
  20. return fmtprint(fmt, "%04d/%02d%02d %02d:%02d:%02d.%03d",
  21. tm.year+1900, tm.mon+1, tm.mday,
  22. tm.hour, tm.min, tm.sec,
  23. (int)(ns%1000000000)/1000000);
  24. }else{
  25. tm = *localtime(time(0));
  26. return fmtprint(fmt, "%04d/%02d%02d %02d:%02d:%02d",
  27. tm.year+1900, tm.mon+1, tm.mday,
  28. tm.hour, tm.min, tm.sec);
  29. }
  30. }