time.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <map>
  3. #include "base/define.h"
  4. namespace ylib
  5. {
  6. namespace time
  7. {
  8. struct datetime
  9. {
  10. datetime() {
  11. year = 0;
  12. month = 0;
  13. day = 0;
  14. hour = 0;
  15. minute = 0;
  16. second = 0;
  17. }
  18. short year;
  19. char month;
  20. char day;
  21. char hour;
  22. char minute;
  23. char second;
  24. };
  25. //网络时间
  26. timestamp network_msec();
  27. //标准字符串时间转时间戳
  28. timestamp to_ts(const std::string& timestr, const std::string& formart = "%d-%d-%d %d:%d:%d");
  29. timestamp to_ts(struct ylib::time::datetime& datetime);
  30. //当前时间戳
  31. uint32 now_sec();
  32. timestamp now_msec();
  33. timestamp now_usec();
  34. /*
  35. %a 星期几的简写
  36. %A 星期几的全称
  37. %b 月分的简写
  38. %B 月份的全称
  39. %c 标准的日期的时间串
  40. %C 年份的后两位数字
  41. %d 十进制表示的每月的第几天
  42. %D 月/天/年
  43. %e 在两字符域中,十进制表示的每月的第几天
  44. %F 年-月-日
  45. %g 年份的后两位数字,使用基于周的年
  46. %G 年分,使用基于周的年
  47. %h 简写的月份名
  48. %H 24小时制的小时
  49. %I 12小时制的小时
  50. %j 十进制表示的每年的第几天
  51. %m 十进制表示的月份
  52. %M 十时制表示的分钟数
  53. %n 新行符
  54. %p 本地的AM或PM的等价显示
  55. %r 12小时的时间
  56. %R 显示小时和分钟:hh:mm
  57. %S 十进制的秒数
  58. %t 水平制表符
  59. %T 显示时分秒:hh:mm:ss
  60. %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
  61. %U 第年的第几周,把星期日做为第一天(值从0到53)
  62. %V 每年的第几周,使用基于周的年
  63. %w 十进制表示的星期几(值从0到6,星期天为0)
  64. %W 每年的第几周,把星期一做为第一天(值从0到53)
  65. %x 标准的日期串
  66. %X 标准的时间串
  67. %y 不带世纪的十进制年份(值从0到99)
  68. %Y 带世纪部分的十进制年份
  69. %z,%Z 时区名称,如果不能得到时区名称则返回空字符。
  70. %% 百分号
  71. 取时间字符串*/
  72. std::string now_time(const std::string& format = "%Y-%m-%d %H:%M:%S");
  73. std::string format(timestamp time, const std::string& format = "%Y-%m-%d %H:%M:%S");
  74. void format(timestamp time, struct ylib::time::datetime& datetime);
  75. ylib::time::datetime now_time2();
  76. //获取当前时区
  77. int32 now_zone();
  78. //获取当前GMT时间
  79. void now_gmt(std::string& gmt);
  80. //时间戳转GMT时间
  81. void to_gmt(const timestamp& timestamp, std::string& gmt);
  82. //取当前凌晨时间
  83. timestamp now_zero_sec();
  84. std::string now_zero_time();
  85. }
  86. }